simple_form_for 错误:未定义的方法 - 关联模型
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您有嵌套资源时,您必须确保始终在请求之间传递更高级别的资源。
您的路线将是您使用 rake 路线注意到的路线。
表单应如下所示:
这样,您现在必须在文章控制器和文章视图页面上的任何位置使用新路由,其中包括杂志本身。
您在控制器中的创建操作将是:
所以现在您的所有路由也应该引用@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:
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:
So now all your routes should reference @magazine too.
在控制台中运行
rake paths
并确保articles_path
存在。似乎应该命名为
magazines_articles_path(...)
Run
rake routes
in your console and make sure thatarticles_path
exists.Seems to be it should be named
magazines_articles_path(...)