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

コードの正確なレイアウトは、レールのバージョンによって少し変更されています(使用しているバージョンを投稿してください-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 に答える