Ruby on rails 3 错误的简单形式:未定义的方法-关联模型
在我的rails应用程序中,我有两个关联的模型,称为杂志和文章 杂志Ruby on rails 3 错误的简单形式:未定义的方法-关联模型,ruby-on-rails-3,simple-form,model-associations,Ruby On Rails 3,Simple Form,Model Associations,在我的rails应用程序中,我有两个关联的模型,称为杂志和文章 杂志 class Magazine < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :magazine end schema.rb create_table "articles", :force => true do |t| t.string "title" t
class Magazine < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :magazine
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
<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) %>
<%= 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 %>
我正在尝试从文章的新页面创建一篇与杂志关联的新文章。因此,我在杂志的展示页面中创建了一个链接,将选定的杂志传递到新文章的页面
视图/杂志/show.html.erb
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
<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) %>
<%= 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 %>
但是,当我尝试创建与该杂志关联的新文章时,我收到错误消息:
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
,并确保文章路径
存在。
似乎应该将其命名为
杂志\u文章\u路径(…)
当您有嵌套资源时,您必须确保始终在请求之间传递更高级别的资源
您的路线将是您在使用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 %>
因此,现在您的所有路由也应该引用@magazine。articles\u path不存在,并且杂志\u articles\u path()重定向到articles\u控制器的显示操作。但是,在这种情况下,传递到简单形式的正确参数是什么?没错。在我对文章的控制器做了这些修改,并更改了其他页面(如文章的显示、编辑和索引)中的链接之后,我的问题得到了解决。非常感谢你。
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