【问题标题】:View/Controller Balance - Rails视图/控制器平衡 - Rails
【发布时间】:2015-12-01 12:02:33
【问题描述】:

我有一个查询将Customer 添加到某个Category。它目前在视图模板中,虽然它可以工作,但有一个错误 - 客户在加载页面时被添加到类别中,在他们点击按钮之前

我认为将该逻辑移回控制器可能会解决它(使用form_for),然后在视图中呈现提交按钮。

你怎么看?以及如何使用form_for 来实现它?

= link_to "JOIN CATEGORY NOW", root_path(@product.category.add_customer(current_customer)), class: "button4"

编辑: 类别模型

class Category < ActiveRecord::Base
#Associations
belongs_to :product
has_many :customer_categories
has_many :customers, through: :customer_categories


def add_customer(customer_id)
 if customer = Customer.where(id: customer_id).first
    self.customers << customer unless self.customers.include?(customer)
end
end 
end

产品型号

class Product < ActiveRecord::Base
include ActionView::Helpers
#Callbacks
after_create do 
    Category.create product: self
end
#Associations 
has_one :category, dependent: :destroy

客户模型

class Customer < ActiveRecord::Base

#Associations
has_many :customer_categories
has_many :categories, through: :customer_categories

编辑#2:

ActionView::Template::Error (undefined method `add_customer_category' for #<#<Class:0x007fcaebdbb5b0>:0x007fcae377ab90>):

路线:

resources :categories do
member do
  get 'add_customer', to: 'categories/add_customer'
end
end

类别控制器:

def add_customer
    @product = Product.find(params[:id])
    @product.category.add_customer(current_customer.id)
end

【问题讨论】:

    标签: ruby-on-rails ruby view controller


    【解决方案1】:

    由于这一行,客户在加载页面时被添加到类别中:

    = link_to "JOIN CATEGORY NOW", root_path(@product.category.add_customer(current_customer)), class: "button4"
    

    或者更具体地说,这部分代码:

    @product.category.add_customer(current_customer)
    

    因为,当您加载页面时,视图中的 Ruby 代码会被评估,因此上述代码会被执行,并且客户会被添加到类别中。

    解决办法:

    在你的 routes.rb 中:

    resources :categories do
      member do
        get 'add_customer'
      end
    end
    

    现在,在你看来:

    = link_to "JOIN CATEGORY NOW", add_customer_category_path(@product.category)
    

    在你的CategoriesController

    def add_customer
      @category = Category.find(params[:id])
      if @category.add_customer(current_customer)
        redirect_to root_path
      else
        # Redirect user to an error page, maybe?
      end
    end
    

    【讨论】:

    • 几乎可以工作!按钮显示完美,但是当客户加入类别并加载http://localhost:3000/categories/3/add_customer 时,我得到uninitialized constant AddCustomerController
    • @liroy : 你能从你的 Rails 服务器发布错误日志吗?
    • Started GET "/categories/3/add_customer" for ::1 at 2015-12-02 05:02:42 -0800 ActionController::RoutingError (uninitialized constant AddCustomerController):
    • 当我在我的路由中省略to: 'add_customer而只写resources :categories do member do get 'add_customer' end end时,没有路由错误,而是现在显示undefined method 'add_customer' for nil:NilClass,这很奇怪,因为这个方法是在@中定义的987654333@模型。
    • @liroy : 关于您最后的评论,请按照 O 发布的分步流程进行操作。您不需要:to: 'add_customer' 在路线中。 undefined method 'add_customer' for nil:NilClass 正在发生,因为 @categorynil 请看看为什么它是 nill 而不是类别对象。另外,请在问题中发布您的 Rails 服务器中的错误日志,而不是发表评论。登录评论很难阅读和理解。
    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2013-11-17
    • 1970-01-01
    相关资源
    最近更新 更多