错误的参数数量(给定4,预期2)

时间:2016-10-29 12:59:57

标签: ruby-on-rails ruby argument-error

这是我的控制器。

class DogsController < ApplicationController


def create
 @dog=current_admin.dogs.build(dog_params)
 if @dog.save
  redirect_to current_admin
 else
  render 'dogs/new'
 end
end

private

 def dog_params
 params.require(:dog).permit(:name, :content , :age , :personality , :breed ,:picture, :admin_id)
 end
end

这是我的观点。

    <%=form_for(@dog, html: {multipart: true}) do |f| %>

    <%= render 'shared/dog_error_messages' , f: @dog %> 

    <div class="field"> 
         <%= f.label :name , class:"form-label"  %><br />
         <%= f.text_field :name, autofocus: true  %>
    </div>
    <div class="field">
         <%= f.label :content , class:"form-label"  %><br />
         <%= f.text_area :content , cols: 70 , rows: 20 ,  placeholder: '犬ちゃんの特徴' %>
    </div>
    <div class="field">
         <%= f.label :age , class:"form-label"  %><br />
         <%= f.number_field :age , min: 0 , max: 15 , step: 1 %>
    </div>
    <div class="field">
         <%= f.label :personality , class:"form-label"  %><br />
         <%= f.select :personality , %w[オス メス] , { include_blank: '選択して下さい'} , class: 'pb' %>
    </div>
    <div class="field">
        <%= f.label :breed , class:"form-label"  %><br />
        <%= f.select :breed , %w[ゴールデンレトリーバー トイプードル] , { include_blank: '選択して下さい'}  %>
    </div>
    <div class="field">
       <%= f.label :picture , class:"form-label"  %><br />
       <%= f.file_field :picture , class:'dog-form-picture' , accept: 'image/jpeg,image/gif,image/png' %>
    </div> 
    <div class="actions">
       <%= f.submit '追加する' , class:' btn btn-primary btn-lg'  %>
    </div>

    <% end %>

   </div>
  </div>
 </div>

这是我的模特。

    class Dog < ActiveRecord::Base
        belongs_to :admin 
        has_many :likes , dependent: :destroy
        default_scope -> {order(created_at: :desc)}
        mount_uploader :picture , PictureUploader

        validates :name , presence: true
        validates :content , presence: true , length: {maximum:300}
        validates :age , presence: true
        validates :personality , presence: true
        validates :breed , presence: true
        validates :admin_id , presence: true
        validates :picture , presence: true
      end

我也是Ruby on Rails的新手。 我得到的错误是:

ArgumentError in DogsController#create wrong number of arguments (given 4, expected 2)

请告诉我为什么我的错误

def create 
  @dog=current_admin.dogs.build(dog_params)

我的代码没有传递任何参数。

1 个答案:

答案 0 :(得分:0)

请尝试@dog = current_admin.dogs.new(dog_params)