Devise polymorphic association nested attributes with simple form using rails 4

左心房为你撑大大i 提交于 2020-01-24 20:20:27

问题


I am making a polymorphic association with devise and simple for but for some reason i cant get the params to work

here is my code:

User:

class User < ActiveRecord::Base
  devise :database_authenticatable,
         :rememberable, :trackable, :validatable

  belongs_to :loginable, polymorphic: true
end

Designer:

class Designer < ActiveRecord::Base
  has_one :user, as: :loginable
  accepts_nested_attributes_for :user

end

Layout:

<%= simple_form_for [:admin, @designer] , :html => { :class => 'form-horizontal' } do |f| %>
    <% if f.error_notification %>
        <div class="alert alert-error fade in">
          <a class="close" data-dismiss="alert" href="#">&times;</a>
          <%= f.error_notification %>
          <% if @designer.errors.any? %>
              <ul>
                <% @designer.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
              </ul>
          <% end %>

        </div>
    <% end %>

    <div class="control-group">
      <%= f.label :profile_name, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :profile_name, :class => 'text_field' %>
      </div>
    </div>

    <%= f.simple_fields_for users do |u| %>

        <div class="control-group">
          <%= u.label :email, :class => 'control-label' %>
          <div class="controls">
            <%= u.text_field :email, :class => 'text_field' %>
          </div>
        </div>

        <div class="control-group">
          <%= u.label :password, :class => 'control-label' %>
          <div class="controls">
            <%= u.password_field :password, :class => 'text_field' %>
          </div>
        </div>

        <div class="control-group">
          <%= u.label :type, :class => 'control-label' %>
          <div class="controls">
            <%= u.input :role, :label => false do %>
                <%= u.select :role_id, Role.all.map { |r| [r.name, r.id] } %>
            <% end %>
          </div>
        </div>

        <div class="control-group">
          <%= u.label :firstname, :class => 'control-label' %>
          <div class="controls">
            <%= u.text_field :firstname, :class => 'text_field' %>
          </div>
        </div>
    <% end %>


    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
    </div>
<% end %>

Controller:

  def create
    @user = User.new(designer_params)
    @designer = Designer.new(designer_params)
    @user.loginable = @designer

    respond_to do |format|
      if @user.save! && @designer.save!
        format.html { redirect_to admin_designers_path, notice: 'Designer was successfully created.' }
        format.json { render action: 'show', status: :created, location: admin_designer_path(@designer) }
      else
        format.html { render action: 'new' }
        format.json { render json: [designer: @designer.errors, user: @user.errors], status: :unprocessable_entity }
      end
    end
  end

  def designer_params
    params.permit(:profile_name, :user, user_attributes: [:email, :password, :password, :firstname, :lastname, :address, :postalcode, :city, :country, :role, :role_id])
  end

My params seems to ignore the user attributes, i only see profile name for some reason.

Any ideas on how to fix this would be greatly appreciated

Thanks!


回答1:


I managed to resolve my own issues. so i am posting the answer hoping to save someone else lots of time.

i ended up creating another private method for the user params

  def designer_params
    params.require(:designer).permit(:profile_name, user_attributes: [:email, :password, :password, :firstname, :lastname, :address, :postalcode, :city, :country, :role, :role_id])
  end
  def user_params
    params[:designer][:user_attributes].permit(:email, :password, :password, :firstname, :lastname, :address, :postalcode, :city, :country, :role, :role_id)
  end

and then using those to create my relationship

  def create
    @designer = Designer.new(designer_params)
    @user = User.new(user_params)
    @user.loginable = @designer
    @designer.save!
  end

also if you are having trouble viewing the nested form make sure to use the build_ method

  def new
    @designer = Designer.new
    @user = @designer.build_user
  end


来源:https://stackoverflow.com/questions/18666007/devise-polymorphic-association-nested-attributes-with-simple-form-using-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!