设计使用简单的形式导轨4的多态关联嵌套属性(Devise polymorphic associati

2019-10-18 14:25发布

我正在与制定一个多态关联和简单,但由于某种原因,我不能让PARAMS工作

这里是我的代码:

用户:

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

  belongs_to :loginable, polymorphic: true
end

设计师:

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

end

布局:

<%= 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 %>

控制器:

  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

我PARAMS似乎忽视了用户的属性,我只看到配置文件名称出于某种原因。

关于如何解决此问题的任何想法,将不胜感激

谢谢!

Answer 1:

我设法解决我自己的问题。 所以我张贴的答案希望能挽救别人大量的时间。

我最终为用户创造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

然后使用这些创建我的关系

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

此外,如果您无法观看的嵌套形式确保使用build_方法

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


文章来源: Devise polymorphic association nested attributes with simple form using rails 4