ActiveModel :: ForbiddenAttributesError Ror4

时间:2018-03-21 15:27:16

标签: ruby-on-rails-4 sorcery

将旧的rails应用升级到4.2.10。我正在使用巫术宝石。该应用程序在RoR 3.2.8中运行良好

第45行---> @user = User.new(params [:user])

提取的来源(第45行):

      # POST /users.json
       def create
        @user = User.new(params[:user])

         respond_to do |format|
          if @user.save



  # PUT /users/1
  # PUT /users/1.json
  def update
   @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end

def create method

def create
       @user = User.new(params[:user])

        respond_to do |format|
          if @user.save
            format.html { render action: "thanks", notice: 'User was 
    successfully created.' }
            format.json { render json: @user, status: :created, 
location: @user }
          else
             format.html { render action: "new" }
             format.json { render json: @user.errors, status: 
 :unprocessable_entity }
         end
        end
      end

1 个答案:

答案 0 :(得分:1)

在rails 4或更高版本中,您需要使用强参数,如下所示

def permit_user_params
  params.require(:user).permit(:name, :email, :etc)
end

并将此允许参数用作

def create
       @user = User.new(permit_user_params)

        respond_to do |format|
          if @user.save
            format.html { render action: "thanks", notice: 'User was 
    successfully created.' }
            format.json { render json: @user, status: :created, 
location: @user }
          else
             format.html { render action: "new" }
             format.json { render json: @user.errors, status: 
 :unprocessable_entity }
         end
        end
      end

希望它会对你有所帮助。