2

这可能是非常基本的,但我似乎无法弄清楚。

本质上,当我尝试使用表单创建新用户并且用户详细信息已经存在且不是唯一的时,我收到以下错误:

ArgumentError in UsersController#create

too few arguments

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:61:in `format'
app/controllers/users_controller.rb:61:in `create'

这是我create在我的行动user_controller.rb

  # POST /users
  # POST /users.xml
  def create
      @user = User.new(params[:user])

        if @user.save
          flash[:notice] = 'User successfully created' and redirect_to :action=>"index"
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
      end
    end

这是我的user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :username, :password, :password_confirmation, :remember_me

  validates :email, :username, :presence => true, :uniqueness => true
end

这也是我的表格:

<%= simple_form_for(@user) do |f| %>
  <div class="field">
    <%= f.input :username %>
  </div>
  <div class="field">
    <%= f.input :email %>
  </div>
    <div class="field">
      <%= f.input :password %>
    </div>
    <div class="field">
      <%= f.input :password_confirmation %>
    </div>
  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>
4

2 回答 2

6

format在这种情况下是什么?

没有respond_to障碍,把它放回去!您正在引用其他一些format.

于 2012-06-10T15:30:52.633 回答
4

代码的确切布局通过不同的 rails 版本有所改变(请发布您使用的版本 - 检查 Gemfile)。

此示例适用于 rails 3+(它是使用 3.2.5 生成的,但应适用于所有 3+ 或至少 3.1+ 版本)

def create
  @user = User.new(params[:user])

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'Blob was successfully created.' }
      format.json { render json: @user, status: :created, location: @user}
    else
      format.html { render action: "new" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

ps simple_form 的好选择,它让生活更轻松!

于 2012-06-10T15:37:02.840 回答