在我的 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
架构.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 的正确参数是什么?