2016-11-09 128 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

移动创建新项目到项目控制器,这解决了我的问题