【发布时间】: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