在我的rails应用程序中,我有两个相关的模型,名为Magazine和Article。
杂志
class Magazine < ActiveRecord::Base
has_many :articles
end
文章
class Article < ActiveRecord::Base
belongs_to :magazine
end
路线
Magazineapp::Application.routes.draw do
resources :magazines do
resources :articles
end
end
schema.rb
create_table "articles", :force => true do |t|
t.string "title"
t.string "author"
t.integer "magazine_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "magazines", :force => true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
end
我正在尝试从文章的新页面创建与杂志关联的新文章。因此,我在杂志的节目页面中创建了一个链接,将选定的杂志传递到新文章的页面。
视图/杂志/ show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= @magazine.title %>
</p>
<%= link_to 'Edit', edit_magazine_path(@magazine) %> |
<%= link_to 'Back', magazines_path %> |
<%= link_to 'New Article', new_magazine_article_path(@magazine) %>
文章新页面中呈现的部分是:
视图/物品/ _form.html.erb
<%= simple_form_for([@magazine, @article]) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :title %>
<%= f.input :author %>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
文章控制器中的create方法是:
def create
@magazine = Magazine.find(params[:magazine_id])
@article = @magazine.articles.create(params[:article])
respond_to do |format|
if @article.save
format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
format.xml { render :xml => @article, :status => :created, :location => @article }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
但是当我尝试创建与杂志相关的新文章时,我收到错误消息:
Showing /home/wilson/magazineapp/app/views/articles/_form.html.erb where line #1 raised:
undefined method `articles_path' for #<#<Class:0x00000001944070>:0x00000001926ed0>
Extracted source (around line #1):
1: <%= simple_form_for([@magazine, @article]) do |f| %>
2: <%= f.error_notification %>
3:
4: <div class="inputs">
Request
Parameters:
{"magazine_id"=>"2"}
佣金路线
magazine_articles GET /magazines/:magazine_id/articles(.:format) {:action=>"index", :controller=>"articles"}
POST /magazines/:magazine_id/articles(.:format) {:action=>"create", :controller=>"articles"}
new_magazine_article GET /magazines/:magazine_id/articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_magazine_article GET /magazines/:magazine_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
magazine_article GET /magazines/:magazine_id/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /magazines/:magazine_id/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /magazines/:magazine_id/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
magazines GET /magazines(.:format) {:action=>"index", :controller=>"magazines"}
POST /magazines(.:format) {:action=>"create", :controller=>"magazines"}
new_magazine GET /magazines/new(.:format) {:action=>"new", :controller=>"magazines"}
edit_magazine GET /magazines/:id/edit(.:format) {:action=>"edit", :controller=>"magazines"}
magazine GET /magazines/:id(.:format) {:action=>"show", :controller=>"magazines"}
PUT /magazines/:id(.:format) {:action=>"update", :controller=>"magazines"}
DELETE /magazines/:id(.:format) {:action=>"destroy", :controller=>"magazines"}
我该如何解决这个问题?在这种情况下传递给simple_form_for的正确参数是什么?
答案 0 :(得分:5)
当您拥有嵌套资源时,您必须确保始终在请求之间传递更高级别的资源。
您的路线将是您使用耙路线注意到的路线。
表格应该是这样的:
<%= simple_form_for([@magazine, @article]) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :title %>
<%= f.input :author %>
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
现在,您必须在文章控制器和文章视图页面的任何位置使用新路由,其中包括杂志本身。
您在控制器中的创建操作将是:
def create
@magazine = Magazine.find(params[:magazine_id])
@article = @magazine.articles.create(params[:article])
respond_to do |format|
if @article.save
format.html { redirect_to([@magazine,@article], :notice => 'Article was successfully created.') }
format.xml { render :xml => @article, :status => :created, :location => @article }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
所以现在你所有的路线都应该引用@magazine。
答案 1 :(得分:1)
在您的控制台中运行rake routes
并确保articles_path
存在。
似乎应该将其命名为magazines_articles_path(...)