Rails 4.2.8
user.rb
ORDER BY
IF(type='30', extra, 0) DESC,
IF(type !='30', extra, 0) ASC
customer.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :rememberable,
:validatable, :encryptable, :omniauthable, omniauth_providers: [:facebook], encryptor: :restful_authentication_sha1
attr_accessible :email, :name, :password, :password_confirmation, :is_admin, :is_master
has_one :customer
accepts_nested_attributes_for :customer
end
customers_contoller.rb
class Customer < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
customers / edit.html.erb
class CustomersController < ApplicationController
def edit
@customer = current_user.customer
end
def update
@customer = current_user.customer
@customer.update customer_params
render 'edit'
end
private
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone, user_attributes: [:email, :password, :password_confirmation])
end
end
这就是我在日志中看到的内容
<%= form_for @customer, html: { class: 'checkout-attributes profile-form' } do |f| %>
<div class="row">
<div class="col-sm-6">
<span class="help-block">First Name</span>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class="col-sm-6">
<span class="help-block">Last Name</span>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
<div class="col-sm-12">
<span class="help-block">Email</span>
<%= fields_for :user, @customer.user do |u| %>
<%= u.email_field :email, class: 'form-control' %>
<% end %>
</div>
<div class="col-sm-12">
<span class="help-block">Phone Number</span>
<%= f.text_field :phone, class: 'form-control' %>
</div>
<div class="col-sm-12">
<span class="help-block">Password</span>
<%= fields_for :user, @customer.user do |u| %>
<%= u.password_field :password, class: 'form-control' %>
<% end %>
</div>
<div class="col-sm-12">
<span class="help-block">Confirm Password</span>
<%= fields_for :user, @customer.user do |u| %>
<%= u.password_field :password_confirmation, class: 'form-control' %>
<% end %>
</div>
</div>
<% end %>
此外,我看到 customer_params 方法提供过滤(无用户部分)哈希 {“first_name”=&gt;“杰克”,“last_name”=&gt;“Drobazko”,“手机“=&gt;” 中5084427293" }
那么如何使其有效?
答案 0 :(得分:1)
根据您的日志文件:
&#34; image&#34; =&gt;&#34;&#34;,&#34;客户&#34; =&gt; {&#34; first_name&#34; =&gt;&#34;杰克&#34 ;, &#34; last_name&#34; =&gt;&#34; Drobazko&#34;,&#34; phone&#34; =&gt;&#34; 5084427293&#34;}, &#34;用户&#34; =&gt; {&#34;电子邮件&#34; =&gt;&#34; ddd @fff.com&#34;,&#34;密码&#34; =&gt; &#34; [FILTERED]&#34 ;, &#34; password_confirmation&#34; = GT;&#34; ... 强>
主表单之外的所有fields_for
输入。修复表单以将所有用户输入包装在fields_for
帮助程序中。
关于f.fields_for
和简单fields_for
的注意事项,非常重要
<%= form_for @customer, html: { class: 'checkout-attributes profile-form' } do |f| %>
# some code here
<%= f.fields_for :user, @customer.user do |u| %>
<%= u.email_field :email, class: 'form-control' %>
<%= u.password_field :password, class: 'form-control' %>
<%= u.password_field :password_confirmation, class: 'form-control' %>
<% end %>
</div>
# some code here
以下是来自Rails核心团队的精彩文章,关于使用nested attributes
。