【问题标题】:ActiveRecord::RecordNotFound in CustomersController#new Couldn't find Business without an ID客户控制器中的 ActiveRecord::RecordNotFound#new 找不到没有 ID 的业务
【发布时间】:2015-11-24 13:01:02
【问题描述】:

所以我在其他关于 SO 的问题中查看了此错误的其他实例,但似乎没有任何帮助。因此,我的身份验证系统应该允许企业注册,并允许用户在他们的企业下注册。但是,我收到“找不到没有 ID 的业务”错误。

class CreateUsers < ActiveRecord::Migration
  def change


    create_table :users do |t|

      t.references :company, foreign_key: true


      t.timestamps
      t.string :first_name
      t.string :last_name

      t.string :email
      t.string :password_digest
      t.string :remember_digest
      t.string :role

    end


class CustomersController < ApplicationController

  def new

    set_business

    @customer = @business.customers.create(user_params)

  end

  def create

    @customer = Customer.new(customer_params)
    @customer.save!
    session[:customer_id] = @customer.id
    redirect_to '/'
  rescue ActiveRecord::RecordInvalid => ex
    render action: 'new', alert: ex.message
  end

  private
  def customer_params

    params.require(:customer).permit(:first_name, :last_name, :business_no, :email, :password_digest, :business_id) 
  end

  def set_business

    @business = Business.find (params[:business_id])

  end



HTML snippet: Customer.new.html.erb

<h1>Sign Up</h1>

      <%= form_for(@customer) do |f| %>
          <%= f.label :first_name %>
          <%= f.text_field :first_name, :placeholder => "First name" %>
          <%= f.label :last_name %>
          <%= f.text_field :last_name, :placeholder => "Last name" %>
          <%= f.label :email %>
          <%= f.email_field :email, :placeholder => "Email" %>
          <%= f.label :company_id %>
          <%= f.collection_select :business_id, Business.all, :id, :name %>
          <%= f.password_field :password_digest, :placeholder => "Password" %>
          <%= f.submit "Create Account", class: "btn-submit" %>
      <% end %>



class Business < ActiveRecord::Base
  has_many :customers

end

class Customer < ActiveRecord::Base
  belongs_to :business
end

我应该如何定义@business 变量而不出现此错误,以便用户可以在他们的业务下注册?我希望他们从表单上的可用公司列表中进行选择,然后在用户注册时将它们链接到数据库中。我究竟做错了什么?我对 Ruby 很陌生,我可能需要一些很好的解释来解释为什么会这样。

感谢您的宝贵时间:)

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 activerecord rubymine


    【解决方案1】:

    您应该查看 params[] 的内容,因为没有普通的 :business_id 键,它嵌套在 :customers 哈希中:

    将您的 set_business 更改为这样的想法:

      def set_business
    
        @business = Business.find (params[:customer][:business_id])
    
      end
    

    编辑:这不起作用,因为第一次调用新方法,没有提供参数。您应该只获得填写表格的企业列表!

    def new
      @customer = Customer.new
      @businesses = Business.all
    end
    

    EDIT2:在create方法中你也不需要设置业务,因为它已经被表单设置了!

    def create
      @customer = Customer.create(customer_params)
      @customer.save
    end
    

    【讨论】:

    • 我每天都这样做:)。你能把答案标记为正确的解决方案吗?
    • 我已经完成了,但我认为我太新了,无法显示它
    【解决方案2】:

    更新controller

    customers_controller.rb

    class CustomersController < ApplicationController
      before_action :set_business
    
      def new
        @customer = @business.customers.build
      end
    
      def create
        begin
          @customer = @business.customers.new(customer_params)
          @customer.save!
          session[:customer_id] = @customer.id
          redirect_to '/'
        rescue ActiveRecord::RecordInvalid => ex 
          render action: 'new', alert: ex.message
        end
      end
    
      private
      def customer_params
        params.require(:customer).permit(:first_name, :last_name, :business_no, :email, :password_digest, :business_id) 
      end
    
      def set_business
        @business = Business.find (params[:business_id])
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多