Ruby on rails Rails注释ID=>;无
我有一个评论表,我的用户对一部电影发表评论。评论Ruby on rails Rails注释ID=>;无,ruby-on-rails,erb,rails-routing,Ruby On Rails,Erb,Rails Routing,我有一个评论表,我的用户对一部电影发表评论。评论属于:电影,电影有很多:评论 我的问题是,当我创建一个新的注释时,这个字段必须在那里,如果我删除它,我会得到它 没有路由匹配{:action=>“show”、:controller=>“movies”、:id=>nil} 这是创建新注释的绝对路径http://localhost:3000/movies/:movie_id/comments/new 我已经检查了电影控制器中的动作,但什么都看不到。我也在Comments控制器中使用了创建操作,但仍然没
属于:电影,电影有很多:评论
我的问题是,当我创建一个新的注释时,这个字段必须在那里
,如果我删除它,我会得到它
没有路由匹配{:action=>“show”、:controller=>“movies”、:id=>nil}
这是创建新注释的绝对路径http://localhost:3000/movies/:movie_id/comments/new
我已经检查了电影控制器中的动作,但什么都看不到。我也在Comments控制器中使用了创建操作,但仍然没有
在我看来,这个字段可能会让用户非常困惑。有人能解决这个问题吗
这是我的*comments\u controller.rb*
class CommentsController < ApplicationController
# GET /comments
# GET /comments.json
before_filter :load_movie
before_filter :authenticate_user!, only: [:create,:destroy,:edit,:destroy]
...
# GET /comments/new
# GET /comments/new.json
def new
@movie = Movie.find(params[:movie_id])
@comment = @movie.comments.new
@search = Movie.search(params[:q])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @comment }
end
end
# POST /comments
# POST /comments.json
def create
@comment = Comment.new(params[:comment])
@comment.user_id = current_user.id
@search = Movie.search(params[:q])
respond_to do |format|
if @comment.save
@movie = @comment.movie
format.html { redirect_to movie_path(@movie), notice: "Comment created" }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
def load_movie
@movie = Movie.find_by_id(params[:movie_id])
end
end
*_form.html.erb*
<%= form_for(@comment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.text_field :subject %>
<%= f.text_area :body %>
<%= f.text_field :movie_id %>
</div>
<div class="form-actions">
<%= f.submit "Sumbit Review" %>
</div>
<% end %>
将为您提供如下url:
../movies/:movie_id/comments/new
删除带有电影id的文本字段
,因为这样不安全
在控制器创建操作中:
@movie = Movie.find(params[:movie_id])
@comment = @movie.comments.new(params[:comment])
@comment.user_id = current_user.id
不确定您为什么需要:
member do
get 'comments'
end
表格
使用嵌套资源时,您需要按如下方式构建表单:
<%= form_for ([@movie, @comment]) do |f| %>
<%= f.some_input %>
<% end %>
routes.rb文件请查看我也需要您的表单。请查看。我收到此错误找不到没有ID的电影
我需要获取“评论”,因为还有另一个页面。/movies/:Movie\u ID/comments
,其中显示了该电影的所有评论。您可以在那里获取评论,而无需该集合。
member do
get 'comments'
end
<%= form_for ([@movie, @comment]) do |f| %>
<%= f.some_input %>
<% end %>