参数数量错误(给定 4,预期 2)
wrong number of arguments (given 4, expected 2)
这是我的控制器。
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
我对 Rails 上的 Ruby 也很陌生。
我得到的错误是:
ArgumentError in DogsController#create wrong number of arguments (given 4, expected 2)
请告诉我为什么我在
中出错
def create
@dog=current_admin.dogs.build(dog_params)
我的代码没有传递任何参数。
试试 @dog = current_admin.dogs.new(dog_params)
。
这是我的控制器。
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
我对 Rails 上的 Ruby 也很陌生。 我得到的错误是:
ArgumentError in DogsController#create wrong number of arguments (given 4, expected 2)
请告诉我为什么我在
中出错def create
@dog=current_admin.dogs.build(dog_params)
我的代码没有传递任何参数。
试试 @dog = current_admin.dogs.new(dog_params)
。