2

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

4

2 回答 2

5

当您拥有嵌套资源时,您必须确保始终在请求之间传递更高级别的资源。

您的路线将是您使用 rake 路线注意到的路线。

表格应该是这样的:

<%= 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。

于 2011-05-26T19:28:21.533 回答
1

在您的控制台中运行rake routes并确保它articles_path存在。好像应该取名magazines_articles_path(...)

于 2011-05-26T14:22:53.877 回答