Rails注册没有方法错误

时间:2013-08-16 08:35:34

标签: ruby-on-rails-3

我不确定我的逻辑是否正确,但我这样做..我有管理,我可以手动添加客户,我有前端,客户可以注册。

customers_controller.rb

class CustomersController < Admin::AdminController
  def new
    @customer = Customer.new
    @customer.build_user
  end

 def create
  @customer = Customer.new(params[:customer])
  ...
 end

 ...
end

fronted_controller.rb

class FrontendController < ApplicationController
  def show_signup
    @customer = Customer.new
    @customer.build_user
    render "signup"
  end

  def signup
    @customer = Customer.new(params[:customer])
    ...
  end
end

的routes.rb

  match "signup" => "frontend#show_signup", :via => :get
  match "signup" => "frontend#signup", :via => :post

前端/ signup.html.erb

    <%= form_for @customer, :url => url_for(:controller => 'frontend', :action => 'signup'), :html => { :class => 'form-horizontal' } do |f| %>

<%= f.label :name, :class => 'control-label' %>
<%= f.text_field :name, :class => 'text_field' %>

显示/注册URL后 它给我一个错误

NoMethodError in Frontend#show_signup undefined method `name' for #<Customer id: nil ...

问题出在哪里?我认为可以从不受管理员保护的不同Controller访问Customer模型。

模型:客户从用户继承,在管理部分创建效果很好。

user.rb

belongs_to :customer, :dependent => :destroy

...

customer.rb

has_one :user
...

谢谢

1 个答案:

答案 0 :(得分:0)

好吧,我忘了它的嵌套表格,所以添加

<%= f.fields_for :user do |u| %>

signup.html.erb 解决了错误。