【问题标题】:Devise not saving names设计不保存名称
【发布时间】:2014-07-14 16:53:59
【问题描述】:

我已经阅读了关于此的其他线程,但我仍然无法弄清楚。

两个模型,用户和厨师。

当我注册厨师时,它不会保存他们的名字。下面是代码。

--Application_Controller

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

 before_filter :configure_permitted_parameters, if: :devise_controller?

 protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:first_name, :email) }
  end

end

-新的厨师表格

<%= form_for(@chef) do |f|  %>

  <%= f.label :first_name %>
  <%= f.text_field :first_name %>

  <%= f.label :last_name %>
  <%= f.text_field :last_name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation %>

  <%= f.file_field :photo%>
<% end %>

--chef_controller

class ChefsController < ApplicationController
  before_action :set_chef, only: [:show, :edit]

  # GET /chefs
  def index
    @chefs = Chef.all
  end

  # GET /chefs/1
  def show

  end

  # GET /chefs/new
  def new
    @chef = Chef.new
  end

  # GET /chefs/1/edit
  def edit
  end

  # POST /chefs
  def create
    @chef = Chef.new(chef_params)

    if @chef.save
      redirect_to @chef, notice: 'Chef was successfully created.'
    else
      render action: 'new'
    end
  end

  # PATCH/PUT /chefs/1
  def update
    if @chef.update(chef_params)
      redirect_to @chef, notice: 'Chef was successfully updated.'
    else
      render action: 'edit'
    end
  end

  # DELETE /chefs/1
  def destroy
    @chef.destroy
    redirect_to chefs_url, notice: 'Chef was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
     def set_chef
      @chef = Chef.find(params[:id])
     end

    # Only allow a trusted parameter "white list" through.
    def chef_params
      params[:chef]
    end
end

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

如果你看你的问题,它会说

When I register for chefs it does not save their first_name.

所以问题来了当您创建新厨师时,因此在您的情况下,当厨师注册时,您需要允许额外的属性进行注册,而不是用于登录。

试试:

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email) }
end

【讨论】:

  • 这么愚蠢的东西,我只是忽略了它。感谢您的额外关注。
  • @Pavan 你错过了这一行When I register for chefs it does not save their first_name,答案就隐藏在那里:)
  • 是的,你是对的。但我对devise一无所知。所以它让我摸不着头脑。
【解决方案2】:

如果您想要额外的注册字段/新的设计,这是我的简单解决方案。

在应用控制器中

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


  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :last_name, 
    :email, :phone_number, :company, :address, :city, :zipcode, ])
    #in keys you list all the input you want to accept.
  end
end

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 2018-11-14
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    相关资源
    最近更新 更多