Rails 3:尝试按照 Railscast #17 向我的帖子添加流派
我完全按照 Railscast 的描述进行操作,但使用“流派”而不是“类别”,但每当我发帖时,genre_id
显示为 NULL
,我收到警告:无法批量分配属性genre_ids
我的帖子模型attr_accessible
中只有genre_id
,因为这是属性的名称,但是什么时候我将其更改为 genre_ids
。我收到错误 ActiveRecord::StatementInvalid in PostsController#create 找不到表“genres_posts”
帖子表单
<% for genre in Genre.find(:all) %>
<div>
<%= check_box_tag "post[genre_ids][]", genre.id, @post.genre.include?(genre) %>
<%= genre.name %>
</div>
<% end %>
帖子模型
class Post < ActiveRecord::Base
has_and_belongs_to_many :genres
end
I've followed along exactly like the Railscast describes but using "genre" instead of "category," but whenever I make a post the genre_id
shows as NULL
and I'm getting a WARNING: can't mass assign attribute genre_ids
I only had genre_id
in my post model attr_accessible
because that's the name of the attribute, however when I change it to genre_ids
. I get the errorActiveRecord::StatementInvalid in PostsController#create Could not find table 'genres_posts'
post form
<% for genre in Genre.find(:all) %>
<div>
<%= check_box_tag "post[genre_ids][]", genre.id, @post.genre.include?(genre) %>
<%= genre.name %>
</div>
<% end %>
post model
class Post < ActiveRecord::Base
has_and_belongs_to_many :genres
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有答案,但您应该检查以下内容:
在您的代码中,行
<%= check_box_tag "post[genre_ids][]",genre.id, @post。 Genre.include?(genre) %>
应包含genres
,而不是genre
:<%= check_box_tag "post[genre_ids][]", Genre.id, @post.genres.include?(genre) %>
posts
和genres
的 id。因此,该表应命名为genres_posts
,并包含列genre_id
和post_id
。因此,根据您之前所做的操作,您可以按照所有步骤来实现工作示例应用程序。祝你好运!
I don't have an answer, but here are the things you should check:
In your code, the line
<%= check_box_tag "post[genre_ids][]", genre.id, @post.genre.include?(genre) %>
should includegenres
, notgenre
:<%= check_box_tag "post[genre_ids][]", genre.id, @post.genres.include?(genre) %>
posts
andgenres
in one table. The table should be named thereforegenres_posts
, and include the columnsgenre_id
andpost_id
.So depending on what you have done before, you may follow all the steps to reach a working example application. Good luck!