Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 更新模型参数的链接产生缺失或空值错误_Ruby On Rails 4_Link To_Update Attributes - Fatal编程技术网

Ruby on rails 4 更新模型参数的链接产生缺失或空值错误

Ruby on rails 4 更新模型参数的链接产生缺失或空值错误,ruby-on-rails-4,link-to,update-attributes,Ruby On Rails 4,Link To,Update Attributes,我试图提供一系列链接,当选中这些链接时,它们会将相应用户的参数更新为链接提供的参数,而无需呈现表单。我遇到的唯一问题是,当它调用我得到的更新方法时:参数丢失或值为空:user 更新方法 def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to list_path flash[:success] = "User updated" else redirect_to list_pa

我试图提供一系列链接,当选中这些链接时,它们会将相应用户的参数更新为链接提供的参数,而无需呈现表单。我遇到的唯一问题是,当它调用我得到的更新方法时:参数丢失或值为空:user

更新方法

 def update
@user = User.find(params[:id])
if @user.update(user_params)
  redirect_to list_path
  flash[:success] = "User updated"
else
  redirect_to list_path
  flash[:alert] = "User not updated"
end
end

private

def user_params
  params.require(:user).permit(:name, :email, :password,
                               :password_confirmation, :title, :role, :address, :phone)
end
链接到

<% User.all.each do |user| %>
        <% if user.role == "new" %>
      <tr>
        <td><%= user.name %></td>
        <td><%= user.title %></td>
        <td><%= user.role %></td>
        <td><%= link_to "Admin", update_user_path(id: user.id, :role => "admin"), :method => :put %> / <%= link_to "Moderator", '#' %> / <%= link_to "Member", '#' %> / <%= link_to "Other", '#' %></td>
      </tr>
        <% end %>
      <% end %>
从我发现的情况来看,params.inspect在列出所有参数之前应该有类似于
[:user]=>
的内容,但我无法找出原因,也无法成功找到澄清的方法

错误:
参数丢失或值为空:user

def user_params     
  **params.require(:user).permit(:name, :email, :password,**
                               :password_confirmation, :title, :role, :address, :phone)
end
end
选择链接后记录

{"_method"=>"put",
"authenticity_token"=>"AUcV1swtGmRuDelTzLkbLd9Gmj6+phnHaSFRqrvDyETLoxNUQSaAgkes4ViWXoAZ33K5NZojVz9XdgIIsqJblA==",
"id"=>"2",
"role"=>"admin"}
Started PUT "/update_user?id=4&role=admin" for 97.78.175.155 at 2015-06-30   15:51:47 +0000
Cannot render console from 97.78.175.155! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#update as HTML
  Parameters: {"authenticity_token"=>"m+LcLOBL5NBXHQXD19o45/iV07/V1HiAXpykV+NiXIpRBtqubUB+Nn68DciNPaPT+KHwtPFRNnhgy/f16gPPWg==", "id"=>"4", "role"=>"admin"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 4]]
Unpermitted parameters: _method, authenticity_token, id
       (0.2ms)  begin transaction
          User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('qwerty@poster.com') AND "users"."id" != 4) LIMIT 1
       (0.1ms)  rollback transaction
Redirected to https://rails-tutorial-hougthonbrad.c9.io/list
Completed 302 Found in 11ms (ActiveRecord: 0.7ms)


Started GET "/list" for 97.78.175.155 at 2015-06-30 15:51:47 +0000
Cannot render console from 97.78.175.155! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#list as HTML
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  User Load (0.5ms)  SELECT "users".* FROM "users"
  Rendered users/list.html.erb within layouts/application (3.4ms)
Completed 200 OK in 172ms (Views: 165.1ms | ActiveRecord: 0.7ms)
将更新操作更改为以下内容

def update
@user = User.find(params[:id])
if @user.update(user_params)
  redirect_to list_path
  flash[:success] = "User updated"
else
  redirect_to list_path
  flash[:alert] = "User not updated"
end
end
def user_params
  params.require(:user).permit(:id, :name, :email, :password, :password_confirmation, :title, :role, :address, :phone)
end
更新#1:

查看您的参数,您需要更改
用户参数
,如下所示

def update
@user = User.find(params[:id])
if @user.update(user_params)
  redirect_to list_path
  flash[:success] = "User updated"
else
  redirect_to list_path
  flash[:alert] = "User not updated"
end
end
def user_params
  params.require(:user).permit(:id, :name, :email, :password, :password_confirmation, :title, :role, :address, :phone)
end
更新#2:

尝试将您的
链接更改为
,如下所示

<%= link_to "Admin", update_user_path(user, user: {:id => user.id, :role => "admin"}), :method => :put %>
user.id,:role=>“admin”}),:method=>:put%>

您的用户参数中有
参数。require(:user)
user
散列通常由
form\u为
生成


我建议为您的用例呈现一个隐藏了所有字段的表单,除了提交按钮

它仍在突出显示用户参数并显示相同的错误。@Houghtonbrad请用完整的错误日志更新您的帖子。我已在日志中添加了一些详细信息post@Houghtonbrad好啊您还可以发布用于更新的表单吗?如果您引用的是
表单,我将尝试更新此单个参数,而不使用表单,并根据所选链接(即管理员/版主/成员/黑名单)指示分配给它的内容,我将尝试这样做,然后回到原点。我开始认为这是我的更新方法本身。我不确定你的问题和错误是否最新。建议您编写一个单独的方法来更新用户角色,而不是依赖通用的
update
方法。这将允许您
允许
所需的特定属性。