设计不允许的params错误(Devise unpermitted params error)
我在我的rails应用程序中设置了Devise。 我还在我的应用程序控制器中设置了一个设计参数清理器。 但是当我使用自定义参数注册时,我的rails本地服务器日志中出现了“未允许的参数错误”。 奇怪的问题是,这一次是在工作......现在它似乎被打破了。 我查看了我的Devise配置和文档,并正确设置了Sanitizer。
Application_Controller:
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. include Pundit include Redcarpet protect_from_forgery with: :exception before_action :authenticate_user! rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :lastname, :username) } devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :lastname, :username) } end end
Rails服务器日志:
Started POST "/users" for ::1 at 2016-06-05 16:02:57 -0700 Processing by Devise::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"PhMqJOEO7fLyUJiWjHcnu+wyB0EQwDeCV9m6XsT5kZ/IyVZ9ZUpwLc26sNbRZleh6xz7V90bvA+yqUBkDkhMmA==", "user"=>{"firstname"=>"Austin", "lastname"=>"Thesing", "username"=>"austinthesing", "email"=>"me@me.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} Unpermitted parameters: firstname, lastname, username (0.1ms) begin transaction User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'austin@designxdevelop.com' LIMIT 1 SQL (0.4ms) INSERT INTO "users" ("email", "encrypted_password", "role", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["email", "me@me.com"], ["encrypted_password", "$2a$11$KQLR6rLh0qrMfzX90FToP.Yo.i0wmfPkvvw7JOKQIn4smtzk9HJkm"], ["role", 0], ["created_at", "2016-06-05 23:02:57.825074"], ["updated_at", "2016-06-05 23:02:57.825074"], ["confirmation_token", "HMkLUzBwoXB7NZxvYJCA"], ["confirmation_sent_at", "2016-06-05 23:02:57.825333"]] (0.6ms) commit transaction Rendered devise/mailer/confirmation_instructions.html.erb (4.9ms)
I setup Devise in my rails application. I also setup a devise parameter sanitizer in my application controller. But when I sign up using the custom parameters I get a 'unpermitted parameter error' in my rails local server log. The weird issue is that this was working at one time.. and now it seems to be broken. I have looked through my Devise configuration and the Documentation and I set up the the Sanitizer properly.
Application_Controller:
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. include Pundit include Redcarpet protect_from_forgery with: :exception before_action :authenticate_user! rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :lastname, :username) } devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :lastname, :username) } end end
Rails Server Log:
Started POST "/users" for ::1 at 2016-06-05 16:02:57 -0700 Processing by Devise::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"PhMqJOEO7fLyUJiWjHcnu+wyB0EQwDeCV9m6XsT5kZ/IyVZ9ZUpwLc26sNbRZleh6xz7V90bvA+yqUBkDkhMmA==", "user"=>{"firstname"=>"Austin", "lastname"=>"Thesing", "username"=>"austinthesing", "email"=>"me@me.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} Unpermitted parameters: firstname, lastname, username (0.1ms) begin transaction User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'austin@designxdevelop.com' LIMIT 1 SQL (0.4ms) INSERT INTO "users" ("email", "encrypted_password", "role", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["email", "me@me.com"], ["encrypted_password", "$2a$11$KQLR6rLh0qrMfzX90FToP.Yo.i0wmfPkvvw7JOKQIn4smtzk9HJkm"], ["role", 0], ["created_at", "2016-06-05 23:02:57.825074"], ["updated_at", "2016-06-05 23:02:57.825074"], ["confirmation_token", "HMkLUzBwoXB7NZxvYJCA"], ["confirmation_sent_at", "2016-06-05 23:02:57.825333"]] (0.6ms) commit transaction Rendered devise/mailer/confirmation_instructions.html.erb (4.9ms)
原文:https://stackoverflow.com/questions/37647668
最满意答案
通过展示哈姆斯在评论中谈论的事情:
class UserData extends Component { constructor () { super() this.state = { resultsItems: [] } } componentDidMount () { // Simulate API response const resultsItems = [ { title: 'foo' }, { title: 'bar' }, { title: 'wombat' } ] this.setState({ resultsItems }) } render () { return ( <div> {this.state.resultsItems.map(item => <ResultsItem item={item} />)} </div> ) } } function ResultsItem ({ item }) { return <li>{item.title}</li> }
但是, Chris对错误消息的原因的回答是正确的:第一个渲染尝试使用空对象而不是数组失败。
By way of demonstrating the sort of thing Hamms is talking about in their comment:
class UserData extends Component { constructor () { super() this.state = { resultsItems: [] } } componentDidMount () { // Simulate API response const resultsItems = [ { title: 'foo' }, { title: 'bar' }, { title: 'wombat' } ] this.setState({ resultsItems }) } render () { return ( <div> {this.state.resultsItems.map(item => <ResultsItem item={item} />)} </div> ) } } function ResultsItem ({ item }) { return <li>{item.title}</li> }
However, Chris' answer is correct as to the cause of the error message: the first render tries to use an empty object and not an array, which fails.
相关问答
更多-
使用一些简单的辅助函数,这绝对是可能的: const data = [{tx_id:33,last_time:456,best_lap:3455,best_time:32432,diff:32,laps:[{lap:1,time:1234},{lap:2,time:32323},{lap:3,time:3242332}]},{tx_id:34,last_time:123,best_lap:32234,best_time:335343,diff:10,laps:[{lap:1,time:1234},{lap: ...
-
如何在React中更新/删除this.state数组中的对象(How do I update/delete objects inside this.state array in React)[2023-04-12]
如果你真的不想使用不可变数据,那么你可以直接改变你喜欢的属性,并在发生突变后调用this.forceUpdate()而不是setState 。 如果要继续处理不可变数据,则添加到该数组将返回包含新对象的新数组。 如果你删除了那个人,那么你将返回一个新的空数组。 如果您要在数组中就地修改项目,那么您将使用concat / slice构建一个新数组以获取所有未更改的元素,然后添加更改的元素,然后在更改的元素索引之后添加所有内容。 有关避免阵列突变的更深入讨论,请查看此演讲 。 If you really don ... -
在setZoneCategoriesReducer函数中,添加action.zoneCategories作为返回状态的元素,可能包含您实际需要的8757个元素。 传播它而不是直接添加。 let setZoneCategoriesReducer = function (zoneCategories = [], action) { // ... return [ ...zoneCategories, ...action.zoneCategories ] / ...
-
在ProductsList的render() ,将数组映射更改为: {this.props.products.map((product, index) =>
)} 然后在您的Product中将updateField()更改为: updateField(...props){ this.props.products.updateProduct(t ... -
通过展示哈姆斯在评论中谈论的事情: class UserData extends Component { constructor () { super() this.state = { resultsItems: [] } } componentDidMount () { // Simulate API response const resultsItems = [ { title: 'foo' }, { title: ...
-
您想要检查obj是否有一个dt_txt ,其中包含您在filter给出的函数中要查找的日期: let result = paramsid.filter(obj => { return obj.dt_txt.includes("2018-06-28"); }); You want to check if the obj has a dt_txt that includes the date you are looking for in the function given to filter: let ...
-
如果你的状态对象是一个数组,你必须从索引中选择正确的对象,创建一个重复的对象并分配新值,然后用新值设置状态。 updateLabel=(text, index)=>{ let allInputs = [...this.state.input]; let selectedInput = {...allInputs[index]}; selectedInput.label = text; allInputs[index] = selectedInput; this.setState({input:al ...
-
我没有运行/测试这个,但尝试这样的事情 API调用似乎返回对象列表。 如果是这样,只需在xhr完成后设置状态并将加载设置为false一次。 在反应render()是你可以迭代你的列表。 最简单的方法是使用'.map()'。 然后您只需为列表中的每个对象返回反应元素。 也让我们将'component'重命名为'list' class NewBlogs extends React.Component { constructor(props) { this.state = { ...
-
我认为问题是你的College组件直接输出来自college_sports数组的对象,如: const college_sports = this.state.college.college_sports
-
{ college_sports.map(sport=>
- {sport} ) }
-
您的代码最有可能正常工作,但我怀疑您对componentDidMount的理解不正确。 在最初呈现组件之后,仅调用componentDidMount一次。 更新道具时不会调用它。 您可能想要查看componentDidUpdate 。 通常,我会避免使用自定义对象进行状态 - 尤其是共享的可变对象 - 而是使用普通对象。 Your code is most likely working, but I suspect that your understanding of componentDidMount i ...