我正在从Rails 4升级到4.2。我有一个用户控制器,看起来像这样:
<!DOCTYPE html>
<head>
<title>L4-E</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Izzy Palmer">
<meta name="description" content="L4-E Music">
<meta name="keywords" content="Music, L4-E">
<meta name="robots" content="noindex, nofollow">
<link rel="stylesheet" href="question.css">
<link href="https://fonts.googleapis.com/css?family=Lato|Pontano+Sans" rel="stylesheet">
</head>
<figure>
<div class="sidebarbox" id="sidebar">
<header>
<a href="index.html">
<h1>L4-E</h1>
</a>
</header>
<img src="images/Sidebar-art.PNG" id="sidebarart" alt="Sidebar Art"><br>
<div class="dropdown">
<button class="dropbtn"><h3>Bio</h3></button>
<div class="dropdown-content">
<p>this is my bio. this is my bio. this is my bio. this is my bio. this is my bio. this is my bio.this is my bio. this is my bio. this is my bio. this is my bio. this is my bio. this is my bio. this is my bio. this is my bio. this is my bio. this
is my bio.</p>
</div>
</div>
<div class="dropdown">
<button class="dropbtn"><h3>Contacts</h3></button>
<div class="dropdown-content">
<div class="link">
<a href="#" target="_blank">
<h5>Facebook</h5>
</a>
</div>
<div class="link">
<a href="#" target="_blank">
<h5>Twitter</h5>
</a>
</div>
<div class="link">
<a href="https://soundcloud.com/l4-e" target="_blank">
<h5>Soundcloud</h5>
</a>
</div>
</div>
</div>
</div>
</figure>
我不确定这是否与strong_params有关。但据我所知class UsersController < ApplicationController
def new
end
def edit
end
protected
def create_with_token
@user.attributes = params[:user]
@user.validate_password = true
if @user.save
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(
:email, :first_name, :last_name, :password, :more_attributes
)
end
end
返回哈希值。那么为什么我会在params[:user]
上获得ArgumentError: When assigning attributes, you must pass a hash as an argument.
?
我检查了rails code并找到了:
@user.attributes = params[:user]
从这里开始,假设def assign_attributes(new_attributes)
if !new_attributes.respond_to?(:stringify_keys)
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
end
return if new_attributes.empty?
attributes = new_attributes.stringify_keys
_assign_attributes(sanitize_for_mass_assignment(attributes))
end
替换为#assign_attributes
(它实际上是alias。我可以推断#attributes
没有回复params[:user]
。但是为什么?怎么样?有什么工作?
答案 0 :(得分:0)
你的params[:user]
是ActionController :: Parameters的一个实例,我想......所以你只需要切换:
@user.attributes = params[:user]
要
@user.attributes = user_params