As a total Ruby noob i need to do a simple thing but doing it the ruby way is not clear for me.
What is want to do is the following:
I have a devise User model with a one to one association "Account"
In the devise registration view i want to add extra form fields for this account, and that is where i am stuck.
It seems i cannot add the account fields to the view
for example this will not work:
# address is a field of Account
<%= f.text_field :address %>
How can i bring the Account model into the scope? Or is there a way to do something like this
<%= f.text_field :account['address'] %>
I have really no clue how to add account into scope or how i can access the User assoc Account properly.
Thx for the help
You need to use accepts_nested_attributes_for :account
to able to add parameters from registration form. Then whitelist these parameters by overriding devise method configure_permitted_parameters.
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [account_attributes: [:address]])
end
You might need to change which parameters needs to be whitelisted.
Then in view, you need to use fields_for
like
f.fields_for(:addresss) do |address_fields|
<%= address_fields.text_field :address %>
end
Hopefully, this solves the problem. Try to search about nested attributes if something doesn't work.
您需要从那里重写devise RegistrationsController
,然后在用户成功注册后在create
方法上进行修改以建立account
记录
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.