当用户发表评论时,会创建新评论和项目。我正在尝试通过新的评论表单将旧项目与新评论相关联,但每当创建新评论时,它会在评论表item_id部分中获取新的项目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 :(得分:0)
移动创建新项目到项目控制器,这解决了我的问题