1

This is my controller.

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

And this is my view.

    <%=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>

And This is my model.

    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

I'm also pretty new to Ruby on Rails. The error I'm getting is:

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

Please tell me why I have an error in

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

My code doesn't pass any parameters.

2
  • Try something like this @admin = current_admin @dog = Dog.create(dog_params) @dog = @admin.dogs << @dog Commented Oct 29, 2016 at 13:21
  • can you please add the implementation of 'current_admin' Commented Oct 29, 2016 at 16:50

1 Answer 1

1

Try @dog = current_admin.dogs.new(dog_params) instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I can make progress!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.