2016-11-09 152 views
0

當用戶離開評論時,會創建新評論和項目。我試圖通過新評論表單將舊項目與新評論相關聯,但是無論何時創建新評論,它都會在評論表item_id部分中獲取新項目標識符,而不是在新評論表單中顯示的舊項目標識符。一次性創建多個模型

型號:

class Item < ActiveRecord::Base 
has_one :comment 

class Comment < ActiveRecord::Base 
belongs_to :Item 

評論控制器:

 def create 
    @comment = Comment.new(comment_params) 
    @comment.create_item (line_id: @comment.line_id, view_dc_id: @comment.view_dc_id, irankdez_id: @comment.item.irankdez_id, 
    outputs_id: @comment.item.outputs_id, DataKeitimo: Time.current) 


    if @comment.save 

    redirect_to line_path(@comment.line_id, line_id: @comment.line_id, view_dc_id: @comment.view_dc_id, irankdez_id: @comment.item.irankdez_id, outputs_id: @comment.item.outputs_id), :flash => {:notice => "New Item is created!"} 
else 
... 
end  
end 

評論表單視圖:

<%= simple_form_for(@comment) do |f| %> 

<%= f.hidden_field :item_id, 
:value => params[:item_id] %> #passing old item_id over params 

<%= f.hidden_field :line_id, 
:value => params[:line_id] %> 

<%= f.hidden_field :view_dc_id, 
:value => params[:view_dc_id] %> 
< 
<%= f.hidden_field :outputs_id, 
:value => params[:outputs_id] %> 

<%= f.text_field :body, :required => true %> 
<label for="textarea1">Komentaras</label> 

    <%= f.submit 'Submit', :class => 'btn' %> 

<% end %> 

請幫助我。

回答

0

移動創建新項目到項目控制器,這解決了我的問題