I think you have options:
Option 1) View:
<% form_for(@comment) do|f| %>
<%= f.hidden_field :commentable_id, :value => @book.id %>
<%= f.hidden_field :commentable_type, :value => 'Book' %>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
<%= f.submit "Post Comment" %>
<% end %>
Controller:
def create
@comment = Comment.create(params[:comment])
end
Option 2)
In the controller:
def create
@book = Book.find(params[:comment][:book_id])
@comment = @book.comments.create(params[:comment].except([:comment][:book_id]))
end
I have not tested the code, but the idea should be correct
, ( , ...). , , , . , .
map.resources :books, :has_many => :comments
map.resources :magazines, :has_many => :comments
:
before_filter :find_commentable
def find_commentable
@commentable = Book.find(params[:book_id]) if params[:book_id]
@commentable = Magazine.find(params[:magazine_id]) if params[:magazine_id]
end
:
<% form_for :comment, [@commentable, @comment] do |f| %>
<%= f.label :comment %>
<%= f.text_area :comment %>
<%= f.submit %>
<% end %>
, create :
def create
@user.comments.create(params[:comment].merge(:commentable_id => @commentable.id, :commentable_type => @commentable.class.name))
redirect_to @commentable
end
, ...