0
ユーザーがコメントを残すと、新しいコメントとアイテムが作成されます。古いコメントを新しいコメントフォームに関連付けようとしていますが、新しいコメントが作成されるたびに新しいコメントフォームに表示されている古いコメントではなく、新しいコメントID item_idセクションに新しいアイテムIDが追加されます。複数のモデルを1つのアクションで作成する
モデル:
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 %>
は私を助けてください。