Skip to main content
 首页 » 编程设计

ruby-on-rails中设计不保存名字

2025年01月19日17飞鱼

我已经阅读了其他相关主题,但仍然无法弄清楚。

两个模型,用户和厨师。

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

--应用程序 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 

请您参考如下方法:

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

当我注册厨师时,它不会保存他们的名字。

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

尝试:

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