简体   繁体   中英

Passing post_id for comment through comments controller

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 %>

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM