通过评论控制器传递评论的 post_id
我有两个具有belongs_to/has_many 关系的模型。帖子有很多评论,评论属于帖子。
我需要通过 comments_controller.rb#new 传递 post_id。
def new
@post = Post.find(params[:post_id])
@comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id])
end
评论表:
<%= simple_form_for([@post, @post.comments.new]) do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= f.button :submit, "Reply" %>
<% end %>
I have two models with a belongs_to/has_many relationship. Posts have many comments, comments belong to posts.
I need to pass the post_id through comments_controller.rb#new.
def new
@post = Post.find(params[:post_id])
@comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id])
end
comment form:
<%= simple_form_for([@post, @post.comments.new]) do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= f.button :submit, "Reply" %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 params 从视图传递到控制器方法,但我认为您不能将 params 传递给控制器,然后传递给视图。
在您的
new
操作中,您可能必须声明一个@variable
并将其定义为您可以在视图中使用它的params。但是,您已经有了
@post.id
,为什么不能直接使用它呢?You can pass a params from view to a controller method but I don't think you can pass a params to a controller then to a view.
In your
new
action, you may have to declare a@variable
and define it to be your params where you can use it in your view.But, you have already have
@post.id
, why can't you just use that?