【发布时间】:2015-08-07 14:11:35
【问题描述】:
为了学习 Rails,我正在开发一个简单的日历应用程序。首先,我创建了两个脚手架和正确的关联/路由:
- 日历
- content_items
app/models/calendar.rb
class Calendar < ActiveRecord::Base
has_many :content_items
end
app/models/content_item.rb
class ContentItem < ActiveRecord::Base
belongs_to :calendar
end
routes.rb
resources :calendars do
resources :content_items
end
controllers/content_items_controller.rb
def create
@content_item = ContentItem.new(content_item_params)
@calendar = Calendar.find(params[:calendar_id] )
respond_to do |format|
if @content_item.save
format.html { redirect_to calendar_content_item_path(@calendar,@content_item), notice: 'Content item was successfully created.' }
format.json { render :show, status: :created, location: @content_item }
else
format.html { render :new }
format.json { render json: @content_item.errors, status: :unprocessable_entity }
end
end
end
当我创建嵌套路由时,我在尝试创建新的 content_items 时开始遇到错误。每当我提交表单时,我都会收到此错误:
ContentItems 中的 NoMethodError#create
未定义的方法`content_items_path'
错误来自: views/content_items/index.html.erb
<%= form_for [@calendar, @content_item] do |f| %>
更新 使用@Gaurav GptA 下面发布的代码修复了表单问题,但导致了一个新错误。每当我访问 /calendars/1/content_items 时,我都会收到一个错误 - 但只有在创建条目之后。使用空数据库,它可以正常工作。
ActionController::UrlGenerationError 没有路由匹配 {:action=>"show", :calendar_id=>#, :controller=>"content_items", :format=>nil, :id=>nil} 缺少必需的键:[:id]
我相信这是因为 content_item 在没有 calendar_id 的情况下被保存。如何设置 content_item 与它所属的 calendar_id 一起保存?
更新 2 它现在使用 calendar_id 保存,但是当保存项目时,edit/shpw/destroy 的链接会抛出错误。
No route matches {:action=>"show", :calendar_id=>#<ContentItem id: 1, , content_type: "Test", content_text: "Tetst\r\n", calendar_id: 1, created_at: "2015-05-26 07:06:42", updated_at: "2015-05-26 07:06:42", content_image_file_name: nil, content_image_content_type: nil, content_image_file_size: nil, content_image_updated_at: nil>, :controller=>"content_items", :format=>nil, :id=>nil} missing required keys: [:id]
突出显示文件的这一部分:
<td><%= link_to 'Show', calendar_content_item_path(content_item) %></td>
GitHub 链接:https://github.com/JeremyEnglert/baked
【问题讨论】:
-
我认为问题出在这一行 "format.html { redirect_to @content_item, notice: 'Content item was created.' }" 您在 content_items_controller 中的创建操作
-
您能否更新您的问题以在终端中包含
ContentItems#create的代码和rake routes的输出 -
更新了上面的问题。
-
将其更改为:
标签: ruby-on-rails ruby associations nested-routes