使用 Ruby 和 Padrino 为新创建的 post 分配类别
Assign categories to a newly created post with Ruby and Padrino
我正在开发我的轻量级 Padrino CMS,它与 Wordpress 的功能非常相似。创建新的 post 时,我希望能够将它们分配给许多现有类别。不知怎么的,我无法完成我的表格工作。
我的模型是这样的:
Post 型号
class Post < ActiveRecord::Base
belongs_to :account
has_many :categorizations
has_many :categories, :through => :categorizations
accepts_nested_attributes_for :categories
end
类别模型
class Category < ActiveRecord::Base
has_many :categorizations
has_many :posts, :through => :categorizations
belongs_to :category
end
分类模型
class Categorization < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
我还为关节创建了迁移 table
class CreateCategorizations < ActiveRecord::Migration
def self.up
create_table :categorizations do |t|
t.integer :category_id
t.integer :post_id
t.timestamps
end
end
def self.down
drop_table :categorizations
end
end
这是表格的相关部分
<% fields_for :categories do |c| %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
<%= c.label 'Category title', :class => 'control-label' %>
<div class='controls'>
<%= c.select(:id, :collection => @categories, :fields => [:title, :id], :include_blank => true, :multiple => true, :class => 'form-control input-xlarge input-with-feedback') %>
<span class='help-inline'><%= error ? c.error_message_on(:id) : "Select a category if there is a parent category" %></span>
</div>
</fieldset>
<% end %>
我不知道我错过了什么,但没有创建关联。我在创建过程中没有在控制器中提及类别,但我确实用现有类别填充了下拉列表。我想以某种方式将它们与新的 post.
相关联
如果有人能指出我正确的方向,我将不胜感激。我得到的错误是:
/admin/posts/create 处的 NoMethodError
nil:NilClass 的未定义方法“each”
文件:collection_association.rb 位置:替换行:383
表单 POST 数据包含:
POST
变量authenticity_token
值“c760c21a5d1f85bfc19e179b37d56f67”
category_active_record_relation {"id"=>["2", "3"]}
post
{"post_name"=>"测试post", "post_type"=>"博客post", "post_title"=>"Postie", "slug"=>"This is a custom set slug", "post_date"=>"2015-06-30", "post_content"=>"Lorem ipsum dolor sit amet consequtiv", "post_excerpt"=>"Lorem ipsum", "post_status"=>"已发布", "comment_status"=>"已关闭", "comment_count "=>"0"}
save_and_continue“保存并继续”
我已经设法回答了我自己的问题,解决方案相当简单,但也许有更好的解决方案,更神奇。无论如何使用 CollectionProxy API 文档很明显我可以在控制器中分配这些类别。
admin/controllers/posts.rb
只需在 if @post.save
之前包含
params[:category_active_record_relation]['id'].each do |category|
category = Category.find(category)
@post.categories << category
end
如果我要创建新类别,我可以使用 @post.categories.build(category) 方法。
希望对其他人也有帮助。
我正在开发我的轻量级 Padrino CMS,它与 Wordpress 的功能非常相似。创建新的 post 时,我希望能够将它们分配给许多现有类别。不知怎么的,我无法完成我的表格工作。
我的模型是这样的:
Post 型号
class Post < ActiveRecord::Base
belongs_to :account
has_many :categorizations
has_many :categories, :through => :categorizations
accepts_nested_attributes_for :categories
end
类别模型
class Category < ActiveRecord::Base
has_many :categorizations
has_many :posts, :through => :categorizations
belongs_to :category
end
分类模型
class Categorization < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
我还为关节创建了迁移 table
class CreateCategorizations < ActiveRecord::Migration
def self.up
create_table :categorizations do |t|
t.integer :category_id
t.integer :post_id
t.timestamps
end
end
def self.down
drop_table :categorizations
end
end
这是表格的相关部分
<% fields_for :categories do |c| %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
<%= c.label 'Category title', :class => 'control-label' %>
<div class='controls'>
<%= c.select(:id, :collection => @categories, :fields => [:title, :id], :include_blank => true, :multiple => true, :class => 'form-control input-xlarge input-with-feedback') %>
<span class='help-inline'><%= error ? c.error_message_on(:id) : "Select a category if there is a parent category" %></span>
</div>
</fieldset>
<% end %>
我不知道我错过了什么,但没有创建关联。我在创建过程中没有在控制器中提及类别,但我确实用现有类别填充了下拉列表。我想以某种方式将它们与新的 post.
相关联如果有人能指出我正确的方向,我将不胜感激。我得到的错误是:
/admin/posts/create 处的 NoMethodError nil:NilClass 的未定义方法“each” 文件:collection_association.rb 位置:替换行:383
表单 POST 数据包含:
POST
变量authenticity_token
值“c760c21a5d1f85bfc19e179b37d56f67”
category_active_record_relation {"id"=>["2", "3"]}
post {"post_name"=>"测试post", "post_type"=>"博客post", "post_title"=>"Postie", "slug"=>"This is a custom set slug", "post_date"=>"2015-06-30", "post_content"=>"Lorem ipsum dolor sit amet consequtiv", "post_excerpt"=>"Lorem ipsum", "post_status"=>"已发布", "comment_status"=>"已关闭", "comment_count "=>"0"}
save_and_continue“保存并继续”
我已经设法回答了我自己的问题,解决方案相当简单,但也许有更好的解决方案,更神奇。无论如何使用 CollectionProxy API 文档很明显我可以在控制器中分配这些类别。
admin/controllers/posts.rb
只需在 if @post.save
之前包含 params[:category_active_record_relation]['id'].each do |category|
category = Category.find(category)
@post.categories << category
end
如果我要创建新类别,我可以使用 @post.categories.build(category) 方法。
希望对其他人也有帮助。