2010-09-20 8 views
13

Ich habe ein paar Klassen, die jeweils Kommentare haben:Erstellen von Formularen für polymorphe Verbände in Rails

class Movie < ActiveRecord::Base 
    has_many :comments, :as => :commentable 
end 

class Actor < ActiveRecord::Base 
    has_many :comments, :as => :commentable 
end 

class Comment < ActiveRecord::Base 
    belongs_to :commentable, :polymorphic => true 
end 

Wie kann ich ein Formular für einen neuen Film-Kommentar erstellen? Ich fügte hinzu,

resources :movies do 
    resources :comments 
end 

meine routes.rb und versuchte new_movie_comment_path (@movie), aber das gibt mir ein Formular mit commentable_id und commentable_type [denen ich mag automatisch ausgefüllt werden, nicht direkt vom Benutzer eingegeben]. Ich habe versucht, auch die Form selbst zu schaffen:

form_for [@movie, Comment.new] do |f| 
    f.text_field :text 
    f.submit 
end 

(wo „Text“ ist ein Feld, in dem Kommentar Tabelle) aber das funktioniert auch nicht.

Ich bin mir nicht sicher, wie man überhaupt einen Kommentar mit einem Film verbindet. Zum Beispiel

c = Comment.create(:text => "This is a comment.", :commentable_id => 1, :commentable_type => "movie") 

scheint nicht einen Kommentar zu erstellen, um den Film im Zusammenhang mit ID 1. (Movie.find (1) .comments ein leeres Array zurück.)

Antwort

6

Da Sie die polymorphe Assoziation in Ihrem Modell erstellt haben, müssen Sie sich in der Ansicht nicht mehr darum kümmern. Sie müssen dies nur in Ihrem Kommentar-Controller tun.

@movie = Movie.find(id) # Find the movie with which you want to associate the comment 
@comment = @movie.comments.create(:text => "This is a comment") # you can also use build 
# instead of create like @comment = @movie.comments.create(:text => "This is a comment") 
# and then @comment.save 
# The above line will build your new comment through the movie which you will be having in 
# @movie. 
# Also this line will automatically save fill the commentable_id as the id of movie and 
# the commentable_type as Movie. 
+3

Wie erstelle ich ein Formular, um den Kommentar einzugeben? Ich glaube nicht, dass ich möchte "form_for @ movie.comments.create do | f | f.text_field: text; f_mit_ende", weil ich den Kommentar nur erstellen möchte, wenn er tatsächlich übergeben wird. Und aus irgendeinem Grund scheint @ movie.comments.build den Kommentar nicht mit dem Film zu verbinden. – grautur

+0

Sie können die Schaltfläche "Kommentar hinzufügen" auf der Seite "Film anzeigen" hinzufügen, die Sie zur Seite "Film bearbeiten" mit dem hinzugefügten Kommentarfeld weiterleitet. Dies kann wie folgt geschehen: – Rohit

+3

<% form_for (@movie) do | f | %> <% = f.error_messages%>

<% = f.label: name%>
<% = f.text_field: name%>

<% f.fields_for: Kommentare zu tun | v | %>

<% = v.label: Kommentar%> <% = v.text_area: Kommentar%>

<% end %> <% end %> – Rohit

3

Du wirst mehr beschreibend sein als „... aber das funktioniert auch nicht,“ aber die allgemeine Idee ist:

@movie.comments.create(:text => params[:movie][:comment][:text]) 

Typischerer:

@movie.comments.create(params[:comment]) # or params[:movie][:comment] 

Das Wichtigste ist, dass Sie zuerst @movie finden und damit verbundene Objekte erstellen. Auf diese Weise müssen Sie sich keine Gedanken über Commentable oder Typen oder irgendetwas machen.