Ruby on rails 嵌套属性使用空父数据预填充子数据
我陷入了这样一种情况:我需要用空表单为字段_提供预填充的数据。让我举例说明 关联Ruby on rails 嵌套属性使用空父数据预填充子数据,ruby-on-rails,ruby,ruby-on-rails-5.2,fields-for,Ruby On Rails,Ruby,Ruby On Rails 5.2,Fields For,我陷入了这样一种情况:我需要用空表单为字段_提供预填充的数据。让我举例说明 关联 class User < ApplicationRecord has_one :account, foreign_key: :user_id accepts_nested_attributes_for :account end Class Account belongs_to :user end 有两个问题 1) 名字和姓氏未预先填充 2) 参数运行不正常。参数中应为account_属性,而
class User < ApplicationRecord
has_one :account, foreign_key: :user_id
accepts_nested_attributes_for :account
end
Class Account
belongs_to :user
end
有两个问题1) 名字和姓氏未预先填充
2) 参数运行不正常。参数中应为account_属性,而不是account 注意:在我的情况下,@account将第一次为空,但user对象(即当前用户)已经有first_name和last_name,我需要在字段_中预先填充。另外,我需要一种更新名字和姓氏的方法
有人能告诉我哪里做错了吗。您应该会看到预填充的字段。最后,我找到了解决方案。我做错了几件事 1) 在表单_中,它应该是@user而不是@account
2) 然后在控制器中,此表单将始终将其发送到更新操作,而不是创建。原因是我将始终拥有当前用户,因此rails将自动检查对象(当前用户)是否存在,因此不会发送以创建它,而是发送以更新操作
3) 最后,当使用一对一关联时,我们需要加载父对象并构建子对象
<%= form_for @user, :url => user_path, html: { class: "abc" } do |f| %>
<div class="medium-4 columns">
<label>First Name</label>
<%= f.text_field :first_name , class: 'xyz', data: {input: 'someinput'} %>
</div>
<div class="medium-4 columns">
<label><b>Last Name<span class="invalid_message"> This field is required</span></b></label>
<%= f.text_field :last_name, class: 'xyz', data: {input: 'someinput'} %>
</div>
<%= f.fields_for :account do |account_info| %>
<div class="medium-4 medium-offset-2 columns">
<label>Phone Number</label>
<%= account_info.text_field :phone_number, class: 'xyz', data: {input: 'someinput'} %>
</div>
<% end %>
<% end %>
class UsersController < ApplicationController
def update
if current_user.update(account_params)
render json: {status: 'successfull'}
else
render json: {error: "#{@account.errors.full_messages}"}, status: 400
end
end
private
def account_params
params.require(:account).permit(:first_name, :last_name, account_attributes: [:phone_number])
end
end
class DashboardController < ApplicationController
def index
##I will always have current_user. For the account it will be blank for the first time
if current_user.account.blank?
@account = current_user
@account.build_account
end
end
end
user_path,html:{class:“abc”}do | f |%>
名字
姓氏此字段为必填字段
电话号码
类UsersController
不要做current_user.id%>
-您让恶意用户只需使用cURL或web inspector输入他们想要的任何用户id变得非常简单。在控制器中使用会话中的值,因为它们存储在加密的cookie中,更难篡改。提供的代码毫无意义。您提供了模型用户和配置文件,但控制器是AccountsController。参数也是错误的-键应该是user\u attributes
@max,对于您提到的第一点,我将在会话中传递它。关于第二点,这是我这边的错别字。该协会有一个:帐户。请看,我已经更新了我的问题,其他的都是向后的。您已在用户模型上声明接受:account
的\u嵌套\u属性\u,但您正在尝试为用户创建具有嵌套属性的帐户。这不是双向的。@max是的,你是对的,我现在找到了解决方案,让我试试
class AccountsController < ApplicationController
def create
@account = Account.new(account_params)
if @account.save
render json: {status: 'successfull'}
else
render json: {error: "#{@account.errors.full_messages}"}, status: 400
end
end
private
def account_params
params.require(:account).permit(:phone_number, :user_id, user_attributes: [:first_name, :last_name])
end
end
class DashboardController < ApplicationController
def index
##I will always have current_user. For the account it will be blank for the first time
if current_user.account.blank?
@account = Account.new
else
@account = current_user.account
end
end
end
{"utf8"=>"✓", "authenticity_token"=>"asdasdadadadadsadadadadadQpy0tA82asdaalAgJsUcNk1i/kGETfZqnuQA==", "account"=>{"user_id"=>"123", "user"=>{"first_name"=>"sd", "last_name"=>"ad"}, "phone_number"=>"1212"}}
<%= form_for @user, :url => user_path, html: { class: "abc" } do |f| %>
<div class="medium-4 columns">
<label>First Name</label>
<%= f.text_field :first_name , class: 'xyz', data: {input: 'someinput'} %>
</div>
<div class="medium-4 columns">
<label><b>Last Name<span class="invalid_message"> This field is required</span></b></label>
<%= f.text_field :last_name, class: 'xyz', data: {input: 'someinput'} %>
</div>
<%= f.fields_for :account do |account_info| %>
<div class="medium-4 medium-offset-2 columns">
<label>Phone Number</label>
<%= account_info.text_field :phone_number, class: 'xyz', data: {input: 'someinput'} %>
</div>
<% end %>
<% end %>
class UsersController < ApplicationController
def update
if current_user.update(account_params)
render json: {status: 'successfull'}
else
render json: {error: "#{@account.errors.full_messages}"}, status: 400
end
end
private
def account_params
params.require(:account).permit(:first_name, :last_name, account_attributes: [:phone_number])
end
end
class DashboardController < ApplicationController
def index
##I will always have current_user. For the account it will be blank for the first time
if current_user.account.blank?
@account = current_user
@account.build_account
end
end
end