2010-09-20 14 views
13

Ho un paio di classi che possono avere ciascuno commenti:creazione di moduli per le associazioni polimorfiche 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 

Come faccio a creare un modulo per un nuovo film-commento? Ho aggiunto

resources :movies do 
    resources :comments 
end 

alla mia routes.rb, e ho cercato new_movie_comment_path (@movie), ma questo mi dà un modulo contenente commentable_id e commentable_type [quale voglio essere popolato automaticamente, non inseriti dall'utente direttamente]. Ho anche provato a generare il modulo di me stesso:

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

(dove "testo" è un campo nella tabella Commento) , ma questo non funziona neanche.

Non sono proprio sicuro di come associare un commento a un film. Ad esempio,

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

non sembra creare un commento associato al film con id 1. (Movie.find (1) .comments restituisce un array vuoto.)

risposta

6

Come è stata creata l'associazione polimorfica nel modello, non occorre più preoccuparsi di ciò nella vista. Hai solo bisogno di farlo nel tuo controller commenti.

@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

Come si crea un modulo per inserire il commento? Non penso di volere "form_for @ movie.comments.create do | f | f.text_field: text; f.submit end", perché voglio solo creare il commento se è effettivamente inviato. E per qualche ragione, @ movie.comments.build non sembra associare il commento al film. – grautur

+0

È possibile aggiungere un pulsante 'Aggiungi commento' nella pagina Mostra film che ti reindirizzerà alla pagina di modifica del film con il campo dei commenti aggiunto ad esso. Questo può essere fatto come segue: – Rohit

+3

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

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

<% f.fields_for: commenti fanno | v | %>

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

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

3

Stai andando a deve essere più descrittivo di "... ma questo non funziona neanche," ma l'idea generale è:

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

più tipicamente:

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

L'importante è trovare prima lo @movie e creare gli oggetti associati attraverso di esso. In questo modo non dovrai preoccuparti di Commentable o tipi o altro.

Problemi correlati