未定义的方法`model_name' for NilClass:Class(has_one a belongs_to relationship)

时间:2012-11-04 21:45:52

标签: ruby-on-rails ruby-on-rails-3.2

您好我尝试为我的user_information模型创建创建视图,但是我收到此错误:

NoMethodError in User_informations#new

undefined method `model_name' for NilClass:Class

以下是用户模型:

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  has_one :user_information
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }, confirmation: true, unless: Proc.new { |a| !a.new_record? && a.password.present? }

    def send_password_reset
        self.password_reset_token = SecureRandom.urlsafe_base64
        self.password_reset_at = Time.zone.now
        self.password = self.password
        self.save!(:validate => false )
        UserMailer.password_reset(self).deliver
    end

    def reset_password_token
        self.password_reset_token = nil
        self.password_reset_at = nil
        save!
    end

  private

    def create_remember_token
        if self.new_record?
            self.remember_token = SecureRandom.urlsafe_base64
        end
    end

end

这里是user_information模型:

class UserInformation < ActiveRecord::Base
  belongs_to :user
  attr_accessible :address, :address2, :business, :descripcion, :identification_number, :mobile_cell, :name, :phone_number
  validates :address, presence: true, length: { :maximum => 250 } 
  validates :address2, length: { :maximum => 250 } 
  validates :descripcion, presence: true, length: { :maximum => 300 } 
  validates :identification_number, presence: true
  validates :phone_number, presence: true, length: { :is => 11 } 
  validates :mobile_cell, :length => { :is => 11 }
  validates :user_id, presence: true
end

和视图:

<%= form_for(@user_information) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= f.text_field  :name %>
    <%= f.text_field  :descripcion %>
    <%= f.text_field  :address %>
  <%= f.button "<i class=\"icon-user icon-white\"></i> Salvar".html_safe, :tabindex => 3,  class: "btn btn-warning", :style => "width:220px;margin-bottom:5px;" %>

<% end %>

控制器

class UserInformationsController < ApplicationController

    def index
    end

    def create
        @user_information = current_user.userinformations.build(params[:user_information])
        if @user_information.save
            flash[:success] = "Micropost created!"
            redirect_to redirect_to
        else

        end
    end

end

任何人都可以帮助我理解。


路线

  resources :users do
      member do
        resource :user_information
      end
  end
  resources :sessions, only: [:new, :create, :destroy]
  resources :password_resets

  root to: 'users#new'

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

1 个答案:

答案 0 :(得分:1)

您必须在控制器中定义@user_information,如下所示:

class UserInformationsController < ApplicationController

   def new
     @user_information = UserInformation.new
   end

end

此外,此行不正确:

@user_information = current_user.userinformations.build(params:user_information)

它应该是:

@user_information = current_user.build_user_information(params[:user_information])