对于我的一生,我不知道发生了什么。我正在将Rails 5与React一起使用。我似乎无法更新我的个人资料。
更新路线:
/users/:user_id/profile(.:format) profiles#update
在我的react组件中,形式为:
<form data-abide="" encType="multipart/form-data" action={url} method="patch">
<input type="text" name="profile[first_name]" value={this.state.firstName}/>
<button className="button expanded" type="submit">Update</button>
</form>
如果将form方法设置为“ post”,它将转到该create
方法并替换不需要的列,因此将其更改为patch
。
配置文件控制器:
...
skip_before_filter :verify_authenticity_token # The only way to get this to work with React
def create
@profile = current_user.build_profile(profile_params)
if @profile.save
#...
else
#...
end
end
def update
@profile = current_user.profile
if @profile.update_attributes(profile_params)
#...
else
#...
end
end
private
def profile_params
params.require(:profile).permit(:first_name)
end
...
有什么不对?
编辑:
我得到它的工作,post
然后将form方法设置为:
def create
update
end
有没有一种方法可以使表格update
直接调用该方法?如注释中所示,表单仅支持GET / POST,不支持PATCH。
使表单方法POST,向表单添加一个隐藏字段:
<input type="hidden" name="_method" value="patch" />
当您使用表单助手时,这就是Rails的工作方式(请参阅http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html),它将覆盖实际用于提交表单的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句