我正在尝试使用fields_for为我的用户执行编辑操作。问题是它不会在用户中保存专业参数。它不会更新 pro 属性。
我有 2 个模型,pro
和"用户"
用户has_one
专业版,专业版belongs_to
用户。
在用户模型中,我输入了: accepts_nested_attributes_for :pro, allow_destroy: true
在用户控制器中,我放置了:
def user_params
params.require(:user).permit(:name, :pro, :tos_agreement, :avatar,
:avatar_cache, :is_pro,:email, :password,
:password_confirmation,
pro_attributes: [
:type, :pro_name, :is_pro, :pro_image,
:is_active, :web, :user_id, :about,
:facebook, :twitter, :linkedin, :telephone])
end
def edit
@user = User.friendly.find(params[:id])
end
def update
@user = User.friendly.find(params[:id])
pro = @user.build_pro
end
最后,在我的 edit.html.erb 视图中,我添加了表单:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<ul class="accordion-submenu">
<li class="semi_padding">
<%= f.label :nombre %>
<%= f.text_field :name, :autofocus => true %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label "Algo sobre tí" %>
<%= f.text_field :description, placeholder: "Soy una persona amante de la decoración..." %>
<%= f.label "Lugar" %>
<%= f.text_field :location , placeholder: "¿De donde eres? Ej: Bilbao"%>
</li>
</ul>
</li>
<li>
<a href="javascript:void(0)" class="js-accordion-trigger">
<i class="icon-switch"> </i> Contraseña</a>
<ul class="accordion-submenu">
<li class="semi_padding">
<%= f.label :contraseña %>
<%= f.password_field :password, :autocomplete => "off" %>
<%= f.label "Confirmación de contraseña" %>
<%= f.password_field :password_confirmation %>
</li>
</ul>
</li>
<% if @user.has_role :pro %>
<li>
<a href="javascript:void(0)" class="js-accordion-trigger">
<i class="icon-store"> </i> Datos de marca PRO</a>
<ul class="accordion-submenu">
<li class="semi_padding">
<%= fields_for @user.pro do |builder| %>
<%= builder.label :facebook %>
<%= builder.text_field :facebook, placeholder: "Facebook URL" %>
<%= builder.label :twitter %>
<%= builder.text_field :twitter, placeholder: "Twitter URL" %>
<%= builder.label :linkedin %>
<%= builder.text_field :linkedin, placeholder: "Linkedin URL"%>
<% end %>
</li>
</ul>
</li>
<% end %>
我可以看到参数在日志中,但是,它们没有被保存。有什么建议吗?谢谢!
更新:
Started PUT "/users" for 127.0.0.1 at 2015-01-29 19:25:17 +0100
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+6D1uzTbQioSLgcxgnMEJOw1ADTgId8LUrCCwiQXpcg=", "user"=>{"avatar_cache"=>"", "name"=>"User 5", "email"=>"prueba5@prueba.com", "description"=>"Et doloremque repudiandae alias odit quis rerum.", "location"=>"Bilbao", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "pro"=>{"facebook"=>"TESTTTTTTT", "twitter"=>"TESTTTTTTT", "linkedin"=>"TESTTTTTTT"}, "commit"=>"Modificar"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 ORDER BY "users"."id" ASC LIMIT 1
Notification Load (0.3ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 AND "notifications"."viewed_at" IS NULL ORDER BY "notifications"."created_at" DESC LIMIT 30 [["user_id", 6]]
Notification Load (0.2ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 AND (viewed_at IS NOT NULL) ORDER BY "notifications"."created_at" DESC LIMIT 30 [["user_id", 6]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 5]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 6]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
(0.1ms) BEGIN
(0.1ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 23ms (ActiveRecord: 2.2ms)
试试这个:
pro = @user.build_pro(params[:p ro_attributes])
尝试更改强参数方法,您需要传递 id 进行更新
params.require(:user).permit(:id, :name, :pro, :tos_agreement, :avatar,
:avatar_cache, :is_pro,:email, :password,
:password_confirmation,
pro_attributes: [
:user_id, :type, :pro_name, :is_pro, :pro_image,
:is_active, :web, :user_id, :about,
:facebook, :twitter, :linkedin, :telephone])