rails undefined方法状态

时间:2016-02-10 09:56:15

标签: ruby-on-rails devise

这是一个非常简单的问题我正在使用设计进行身份验证...所以我正在尝试修改我的注册。我正在尝试在我的注册上添加一个单选按钮,但它给了我错误。用户的未定义方法“status”:0x00000001a66368 。所以我想知道为什么我不能在我的注册页面上使用单选按钮
[注册/ new.html.erb]

<h2>Sign up</h2>

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>

      <div><%= f.label :email %><br />
      <%= f.email_field :email, autofocus: true %></div>

      <div><%= f.label :password %> <% if @validatable %><i>(<%= @minimum_password_length %> characters minimum)</i><% end %><br />
        <%= f.password_field :password, autocomplete: "off" %></div>


      <div><%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off" %></div>

        <div >
            <%= f.label 'InActive' do %>
                    <%= f.radio_button :status,'In Active'%>
            <% end %> 
        </div>


      <div><%= f.submit "Sign up" %></div>
    <% end %>

    <%= render "devise/shared/links" %>


[user.rb]

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
      has_many :posts
      has_many :comments

  validates :status,presence:true
  validates :name, presence: true 



  def self.find_role(id)
    User.find(id).roles
  end   


end

2 个答案:

答案 0 :(得分:3)

您需要在用户表中添加状态字段。

rails g migration add_user_status_to_user user_status:boolean
rake db:migrate

然后,在应用程序控制器中,使用强参数

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << [:user_status]
  end
end

答案 1 :(得分:2)

rails generate migration add_status_to_user status:boolean
rake db:migrate

您需要通过传递强参数来向注册页面添加其他新属性。

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up).push(:status)
  end
end