2011-05-26 33 views
2

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

回答

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了。

+0

没错。在我在文章的控制器中完成这些修改并在其他页面(如文章的显示,编辑和索引)中更改了我的链接之后,我解决了我的问题。非常感谢你。 – wilsonfoz 2011-05-27 12:16:01

1

在您的控制台中运行rake routes并确保articles_path存在。 似乎应该被命名为magazines_articles_path(...)

+0

articles_path不存在,magazines_articles_path()重定向到articles_controller的show操作。但是,在这种情况下传递给simple_form_for的正确参数是什么? – wilsonfoz 2011-05-26 16:50:59