Fields_for不保存更新数据

时间:2015-01-29 18:05:52

标签: ruby-on-rails fields-for

我正在尝试使用fields_for为我的用户执行编辑操作。 问题是它不会在用户中保存专业版。它不会更新专业属性。

我有2个模型,pro和`user``

用户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 %>

我可以看到params在日志中,但是,它们没有得到保存。有什么建议?谢谢!

更新:

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)

2 个答案:

答案 0 :(得分:0)

试试这个:

  

pro = @ user.build_pro(params [:pro_attributes])

答案 1 :(得分:0)

尝试更改强params方法,您需要传递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])