提交表单后我收到了一个奇怪的错误。一直试图解决这个问题几个小时..
No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]
参数是:
{"utf8"=>"✓",
"authenticity_token"=>"w0D7XmX2X2/ZMU19T6RlMvWCEClXnCFFOR+4EdIFvWg=",
"comment_item"=>{"item_id"=>"",
"name"=>"kaljdf",
"body"=>"yet another comment test"},
"commit"=>"Post Comment",
"matter_id"=>"3",
"item_id"=>"141"}
我有以下型号:
class Matter < ActiveRecord::Base
has many :discoveries
delegate :items, to: :discoveries
end
class Discovery < ActiveRecord::Base
belongs_to :matter
scope :items, -> { where(type: 'Item') }
end
class Item < Discovery
has_many :comment_items
end
class CommentItem < ActiveRecord::Base
belongs_to :item
end
控制器:
class ItemsController < DiscoveriesController
def show
@item = Item.find(params[:id])
@comment_item = CommentItem.new
end
def edit
@item = Item.find(params[:id])
end
def new
@item = Item.new
end
end
class CommentItemsController < ApplicationController
before_action :set_comment_item, only: [:show, :edit, :update, :destroy]
def new
@item = Item.find(params[:item_id])
@comment_item = @item.comment_item.new
end
def create
@item = Item.find(params[:item_id])
@comment_item = @item.comment_items.new(comment_item_params)
if @comment_item.save
flash[:notice] = 'Comment was successfully created'
redirect_to matter_item_url(matter_id: params[:matter_id])
else
flash[:notice] = "Error creating comment: #{@comment.errors}"
redirect_to matter_item_url(@matter, @item)
end
end
def destroy
@comment_item = CommentItem.find(params[:id])
@comment_item.destroy
redirect_to(@comment_item.item)
end
private
def set_comment_item
@comment_item = CommentItem.find(params[:id])
end
def comment_item_params
params.require(:comment_item).permit(:name, :body, :item_id, :matter_id)
end
end
项目资源的展示操作:
<p>
<strong>Matter:</strong>
<%= @item.matter_id %>
</p>
<p>
<strong>Content:</strong>
<%= @item.content %>
</p>
<hr />
<%= form_for @comment_item, url: matter_item_comment_items_path(matter_id: @item.matter, item_id: @item.id) do |f| %>
<% if @comment_item.errors.any? %>
<ul>
<% @comment_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.hidden_field :item_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Post Comment" %>
</p>
<% end %>
<%= render :partial => 'comment_item', :collection => @item.comment_items %>
<%= link_to 'Edit', edit_matter_item_path(id: @item.id) %> |
<%= link_to 'Back', matter_items_path %>
路线
resources :items do
resources :comment_items
end
resources :matters do
resources :items do
resources :comment_items
end
end
在控制台中查看CommentItems时,我发现评论实际上已添加到具有正确ID的模型中,但它们似乎并未作为参数传递。我错过了?
我已经审核了Rails 4 form_for double nested comments和Rails 3.2 - Nested Resource Passing ID,但我没有多少运气..
我非常感谢你的帮助!
答案 0 :(得分:0)
No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]
您的请求将转到ItemsController而不是CommentItemsController
参见:controller =&gt; &#34;项目&#34;