simple_form_for 错误:未定义的方法 - 关联模型

发布于 2024-11-09 21:20:29 字数 4732 浏览 6 评论 0原文

在我的 Rails 应用程序中,我有两个关联的模型,称为杂志和文章。

杂志

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

我正在尝试创建一篇相关的新文章文章的新页面杂志。因此,我在杂志的显示页面中创建了一个链接,将所选杂志传递到新文章的页面。

views/magazines/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) %>

在文章的新页面中呈现的部分是:

views/articles/_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

但是当我尝试创建与杂志相关的新文章时,我收到错误消息:

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"}

rake paths

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

In my rails application I have two associated models called Magazine and Article.

Magazine

class Magazine < ActiveRecord::Base
  has_many :articles
end

Article

class Article < ActiveRecord::Base
  belongs_to :magazine
end

Routes

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

I am trying to create a new article associated to a magazine from the article's new page. So, I created a link in the magazine's show page to pass the selected magazine to the new article's page.

views/magazines/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) %>

The partial that is rendered in the article's new page is:

views/articles/_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 %>

And the create method from the article's controller is:

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

But when I try to create a new article associated to the magazine I get the error message:

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"}

rake routes

    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"}

How can I solve this problem? What are the correct parameters to pass to the simple_form_for in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-11-16 21:20:29

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

您的路线将是您使用 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。

When you have nested resources you have to assure that you always pass the higher level resource between your requests.

Your routes will be the ones you have noticed using rake routes.

The form should be like this:

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

This way now you have to use the new routes everywhere on your articles controller and articles view pages, which will include the magazine itself.

Your create action in the controller would be:

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

So now all your routes should reference @magazine too.

蓝色星空 2024-11-16 21:20:29

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

Run rake routes in your console and make sure that articles_path exists.
Seems to be it should be named magazines_articles_path(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文