本文介绍了不允许的参数:配置文件 (NestedAttributes) - RAILS 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被其他用户多次询问,但仍然没有解决我的问题.我的 rails 应用程序出现问题,其中出现错误Unpermitted Parameters: profile".

user_controller.rb

class Admin::UsersController <应用控制器before_filter :set_user, only: [:edit, :update]before_filter :store_location, only: [:index]before_filter :require_admin定义编辑如果@user使成为别的redirect_to admin_users_path,注意:未找到用户配置文件."结尾结尾定义更新# Rails.logger.debug "===> (1)"如果@user.update(user_params)redirect_to edit_admin_user_path,注意:#{@user.profile.full_name} 帐户已更新."别的渲染编辑"结尾结尾私人的def set_user@user = User.find(params[:id])结尾定义用户参数params.require(:user).permit(:id, :username, :email, profile_attributes: [:user_id, :full_name])结尾结尾

edit.html.erb

<%= form_for :user, url: admin_user_path(@user), 方法: :patch do |f|%><div class="form-group"><%= f.label :username %><br>形式控制"%>

<%= f.fields_for :profile, @user.profile do |profile|%><div class="form-group"><%= profile.label :full_name %><br>形式控制"%>

<%结束%><div class="form-group"><%= f.submit "Save", :class =>btn btn-primary"%>

<%结束%>

User.rb

class User { :case_sensitive =>错误的 }结尾

Profile.rb

类简介

development.log

在 2014-09-10 23:18:26 +0800 为 127.0.0.1 启动 PATCH "/master/users/7"参数:{"utf8"=>"✓", "authenticity_token"=>"23oUfOBaYAmcrfhW3R11F1x53lJAT760Shv0HqkmEzw=", "user"=>{"username"=>"lisa", "profile"_name"=>"Evalisa Andriaasdasda"}}, "commit"=>"Save", "id"=>"7"}[1m[35mUser Load (0.3ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 7 LIMIT 1[1m[36mUser Load (0.3ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 6 ORDER BY `users`.`id` ASC LIMIT 1[0m不允许的参数:配置文件[1m[35m (0.2ms)[0m 开始[1m[36mUser Exists (0.4ms)[0m [1mSELECT 1 AS one FROM `users` WHERE (`users`.`username` = 'lisa' AND `users`.`id` != 7) LIMIT 1[0m[1m[35m (0.2ms)[0m 提交][1m[36mProfile Load (0.4ms)[0m [1mSELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 7 LIMIT 1[0m

我真的不知道错在哪里.请帮忙.

提前致谢!

解决方案

啊,我自己解决了问题.对于那些在那里的人,这里是解决方案:-

从 development.log 中,我注意到 "profile" =>{}.它应该是 "profile_attributes" =>{} 所以我将 edit.html.erb 表单修改为:-

<%= form_for :user, url: admin_user_path(@user), 方法: :patch do |f|%><div class="form-group"><%= f.label :username %><br>形式控制"%>

<%= f.fields_for :profile_attributes, @user.profile do |profile|%><div class="form-group"><%= profile.label :full_name %><br>形式控制"%>

<%结束%><div class="form-group"><%= f.submit "Save", :class =>btn btn-primary"%>

<%结束%>

然后,我在 development.log 中收到另一个错误:-

ActiveRecord::RecordNotSaved(无法删除现有的关联配置文件.记录在其外键设置为 nil 后无法保存.):

所以,我再次更新我的 user.rb 以具有:-

class User { :case_sensitive =>错误的 }结尾

This question has many times asking by others user but still not solved my problem here. Im having a problem with my rails apps where error "Unpermitted Parameters: profile" appear.

user_controller.rb

class Admin::UsersController < ApplicationController
  before_filter :set_user, only: [:edit, :update]
  before_filter :store_location, only: [:index]
  before_filter :require_admin

  def edit
    if @user
      render
    else
      redirect_to admin_users_path, notice: "User profile not found."
    end
  end

  def update
    # Rails.logger.debug "===> (1)"
    if @user.update(user_params)
      redirect_to edit_admin_user_path, notice: "#{@user.profile.full_name} account has been updated."
    else
      render 'edit'
    end
  end

  private

  def set_user
    @user = User.find(params[:id])
  end

  def user_params
    params.require(:user).permit(:id, :username, :email, profile_attributes: [:user_id, :full_name])
  end
end

edit.html.erb

<%= form_for :user, url: admin_user_path(@user), method: :patch do |f| %>

  <div class="form-group">
    <%= f.label :username %><br>
    <%= f.text_field :username, :class => "form-control" %>
  </div>

  <%= f.fields_for :profile, @user.profile do |profile| %>

  <div class="form-group">
    <%= profile.label :full_name %><br>
    <%= profile.text_field : full_name, :class => "form-control" %>
  </div>

  <% end %>

  <div class="form-group">
    <%= f.submit "Save", :class => "btn btn-primary" %>
  </div>

<% end %>

User.rb

class User < ActiveRecord::Base

  has_one :profile
  accepts_nested_attributes_for :profile #, update_only: true, allow_destroy: true

  validates :username, :uniqueness => { :case_sensitive => false }

end

Profile.rb

class Profile < ActiveRecord::Base

  belongs_to :user

  validates_presence_of :user_id
  validates_presence_of :full_name

end

development.log

Started PATCH "/master/users/7" for 127.0.0.1 at 2014-09-10 23:18:26 +0800
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"23oUfOBaYAmcrfhW3R11F1x53lJAT760Shv0HqkmEzw=", "user"=>{"username"=>"lisa", "profile"=>{"full_name"=>"Evalisa Andriaasdasda"}}, "commit"=>"Save", "id"=>"7"}
[1m[35mUser Load (0.3ms)[0m  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 7 LIMIT 1
[1m[36mUser Load (0.3ms)[0m  [1mSELECT  `users`.* FROM `users`  WHERE `users`.`id` = 6  ORDER BY `users`.`id` ASC LIMIT 1[0m
Unpermitted parameters: profile
[1m[35m (0.2ms)[0m  BEGIN
[1m[36mUser Exists (0.4ms)[0m  [1mSELECT  1 AS one FROM `users`  WHERE (`users`.`username` = 'lisa' AND `users`.`id` != 7) LIMIT 1[0m
[1m[35m (0.2ms)[0m  COMMIT
[1m[36mProfile Load (0.4ms)[0m  [1mSELECT  `profiles`.* FROM `profiles`  WHERE `profiles`.`user_id` = 7 LIMIT 1[0m

I really don't know where is the mistake. Please help.

Thanks in advance!

解决方案

Ah, I solved my own question. For those out there, here is the solution:-

From development.log, i notice something that is "profile" => {}. It SHOULD BE "profile_attributes" => {} so I modified my edit.html.erb form to:-

<%= form_for :user, url: admin_user_path(@user), method: :patch do |f| %>

  <div class="form-group">
    <%= f.label :username %><br>
    <%= f.text_field :username, :class => "form-control" %>
  </div>

  <%= f.fields_for :profile_attributes, @user.profile do |profile| %>

  <div class="form-group">
    <%= profile.label :full_name %><br>
    <%= profile.text_field :full_name, :class => "form-control" %>
  </div>

  <% end %>

  <div class="form-group">
    <%= f.submit "Save", :class => "btn btn-primary" %>
  </div>

<% end %>

Then, I got another error said in development.log:-

ActiveRecord::RecordNotSaved (Failed to remove the existing associated profile. The record failed to save after its foreign key was set to nil.):

So again, I update my user.rb to have:-

class User < ActiveRecord::Base

  has_one :profile
  accepts_nested_attributes_for :profile, update_only: true, allow_destroy: true

  validates :username, :uniqueness => { :case_sensitive => false }
end

这篇关于不允许的参数:配置文件 (NestedAttributes) - RAILS 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:20