【问题标题】:rendering existing categories in view, rails在视图中呈现现有类别,rails
【发布时间】:2015-10-25 12:35:27
【问题描述】:

如何在我的视图中呈现现有类别的列表?

我的类别表有一个:name 列。

产品视图

 = f.select :category_name, Category.all.map{|s| [s.name]}

类别.rb

class Category < ActiveRecord::Base
    has_many :categoricals
    validates :name, uniqueness: { case_sensitive: false }, presence: true
    acts_as_tree order: "name"
end

产品.rb

class Product < ActiveRecord::Base
   include ActionView::Helpers
   include Categorizable
end

更新:

完整的产品形式

= simple_form_for @product, html: { multipart: true } do |f|
        = f.input :title, placeholder: "Product Name", required: false, label: false
        = f.input :description, placeholder: "Description", required: false, label: false
        = f.select :category_name, Category.all.map{|s| s.name}

        = f.input :image, label: false


    = f.button :submit, class: "button"

更新#2: 产品控制器

def update
    if @product.update(product_params)
        redirect_to @product, notice: "Your Product was successfully updated!"
    else
        render 'edit'
    end
end

分类模型

class Categorical < ActiveRecord::Base
belongs_to :category
belongs_to :categorizable, polymorphic: true

validates_presence_of :category, :categorizable
end

分类模块

    module Categorizable 
  extend ActiveSupport::Concern

  included do
    has_many :categoricals, as: :categorizable
    has_many :categories, through: :categoricals
  end

  def add_to_category(category)
    self.categoricals.create(category: category)
  end

  def remove_from_category(category)
    self.categoricals.find_by(category: category).maybe.destroy
  end


 module ClassMethods
  end
end

【问题讨论】:

    标签: ruby-on-rails database view


    【解决方案1】:

    我不明白为什么人们不使用collection_select 而不是select

    = f.collection_select :category_id, Category.all, :id, :name
    

    看来真正的问题在于你们的关系:

    #app/models/category.rb
    class Category < ActiveRecord::Base
       has_many :products
    end
    
    #app/models/product.rb
    class Product < ActiveRecord::Base
       belongs_to :category #-> requires category_id column in products table
    end
    

    这将允许您引用:category_id,如下所示:

    = simple_form_for @product, html: { multipart: true } do |f|
        = f.input :title, placeholder: "Product Name", required: false, label: false
        = f.input :description, placeholder: "Description", required: false, label: false
        = f.collection_select :category_id, Category.all, :id, :name
        = f.input :image, label: false
    
        = f.button :submit, class: "button"
    

    【讨论】:

    • 我也更喜欢collection_select而不是select
    • 好的。一个问题,它列出了所有很棒的类别,但是当我选择相关的类别时,它不会更新产品属性。
    • 当您说“不更新产品属性”时,您是指传递给控制器​​的参数,还是发送到数据库的参数?
    • 好的。因此,出于某种原因,当我在产品表中创建一个“category_name”列时,它检索的是类别 ID 编号而不是名称!我真的认为这与可分类模块有关。如果您有其他类别模型,请告诉我...谢谢!
    • 您将category_name 放入哪个表?如果是Category,应该就是name吧?
    【解决方案2】:

    试试:

    = f.select :category_name, Category.all.map{|s| s.name}
    

    为了完成这项工作,您的category_name 表中必须存在category_name 列。

    【讨论】:

    • 我得到 'undefined method `category_name'' 如何让 rails 知道:name 在类别表中?
    • 您能在产品视图中显示完整的表单代码吗?
    • 您的products 表中是否有category_name 列?
    • 不。只是按照建议添加了 category_id。
    • 这就是您收到该错误的原因。无论如何,Rich Peck 的回答显示了实现目标的更好方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多