2

我试图在我的用户/新页面上有一个嵌套表单,它接受用户属性和公司属性。当您提交表格时:

这是我的错误信息:

ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: companies
app/controllers/users_controller.rb:12:in `create'

这是我的表单的代码:

<%= form_for @user do |f| %>
      <%= render 'shared/error_messages', object: f.object %> 

      <%= f.fields_for :companies do |c| %>

      <%= c.label :name, "Company Name"%>
      <%= c.text_field :name %>

      <% end %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation %>
      <br>
      <% if current_page?(signup_path) %>
      <%= f.submit "Sign Up", class: "btn btn-large btn-primary" %>     Or, <%= link_to "Login", login_path %>
      <% else %>
      <%= f.submit "Update User", class: "btn btn-large btn-primary" %>
      <% end %>
<% end %>

用户控制器:

   class UsersController < ApplicationController

  def index
    @user = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.create(params[:user])
    if @user.save
      session[:user_id] = @user.id #once user account has been created, a session is not automatically created. This fixes that by setting their session id.  This could be put into Controller action to clean up duplication.
      flash[:success] = "Your account has been created!"
      redirect_to tasks_path
    else
       render 'new'
    end
  end

  def show
    @user = User.find(params[:id])
    @tasks = @user.tasks
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = @user.name.possessive + " profile has been updated"
      redirect_to @user
    else
      render 'edit'
    end

    #if @task.update_attributes params[:task]
    #redirect_to users_path
    #flash[:success] = "User was successfully updated."
    #end
end

  def destroy
    @user = User.find(params[:id])
    unless current_user == @user
      @user.destroy
      flash[:success] = "The User has been deleted."
    end
    redirect_to users_path
    flash[:error] = "Error. You can't delete yourself!"
  end

end

公司控制人

    class CompaniesController < ApplicationController

  def index
    @companies = Company.all
  end

  def new
    @company = Company.new
  end

  def edit
    @company = Company.find(params[:id])
  end

  def create
    @company = Company.create(params[:company])
    #if @company.save
      #session[:user_id] = @user.id #once user account has been created, a session is not automatically created. This fixes that by setting their session id.  This could be put into Controller action to clean up duplication.
      #flash[:success] = "Your account has been created!"
      #redirect_to tasks_path
    #else
       #render 'new'
    #end
  end

  def show
    @comnpany = Company.find(params[:id])
  end

end

用户模型

class User < ActiveRecord::Base
  has_secure_password

  attr_accessible :name, :email, :password, :password_confirmation
  has_many :tasks, dependent: :destroy
  belongs_to :company
  accepts_nested_attributes_for :company

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }
  #below not needed anymore, due to has_secure_password
  #validates :password_confirmation, presence: true  
end

公司模式

    class Company < ActiveRecord::Base
  attr_accessible :name
  has_and_belongs_to_many :users
end
4

2 回答 2

2

首先,为了调试,请在两个控制器中创建,如下所示:create! 您的日志可能会吐出更多。

然后,如果这很糟糕,请尝试构建两个对象并为每个对象分配参数的老式方法。

我还假设这就是属性,而不是保存后应该对架构有更多的内容,然后再显示。

最后,你不见了

def new
  @user = User.new
  @company = @user.companies.build
end

也打印出参数,以防万一它说了什么奇怪的东西,但添加了这一行:正如@Beerlington所说的应该也可以工作:company_attributes,也许company_attributes ...在这里吐个球。

于 2012-09-08T00:05:15.963 回答
0

添加attr_accessible :companies到您的用户模型属性列表

于 2012-09-07T21:41:17.547 回答