在我的Rails應用程序中,我有兩個關聯的模型,稱爲Magazine和Article。simple_form_for錯誤:undefined方法 - 關聯模型
雜誌
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 %>
而文章的控制器的創建方法是:
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
但是當我嘗試創建相關的雜誌,我得到了錯誤信息的新文章:
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"}
我怎樣才能解決這個問題
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"}
耙路線?在這種情況下傳遞給simple_form_for的正確參數是什麼?
沒錯。在我在文章的控制器中完成這些修改並在其他頁面(如文章的顯示,編輯和索引)中更改了我的鏈接之後,我解決了我的問題。非常感謝你。 – wilsonfoz 2011-05-27 12:16:01