Silva_PT_SCP
Silva_PT_SCP

Reputation: 567

param is missing or the value is empty: user_club - rails

I want to call create action of controller user_clubs and I did this way:

View Clubs

<button> 
   <%= link_to "Join Club", user_clubs_path(:user_id => current_user.id, :club_id => @club.id, :join_date => Date.current), :method => :post %>
</button>

Controller user_clubs

def create
    @user_club = UserClub.new(user_club_params)

    respond_to do |format|
      if @user_club.save
        format.html { redirect_to @user_club, notice: 'User club was successfully created.' }
        format.json { render :show, status: :created, location: @user_club }
      else
        format.html { render :new }
        format.json { render json: @user_club.errors, status: :unprocessable_entity }
      end
    end
  end

def user_club_params
  params.require(:user_club).permit(:user_id, :club_id, :join_date) --->**Error here**
end

Error information

app/controllers/user_clubs_controller.rb:75:in user_club_params' app/controllers/user_clubs_controller.rb:28:increate' Request

    Parameters:

    {"_method"=>"post",
     "authenticity_token"=>"5Grhb+LIGt9B8XbnEcvg7BZQlDE935KO/aeikZoqxYs=",
     "club_id"=>"1",
     "join_date"=>"2014-11-17",
     "user_id"=>"2"

}

Clubs and UserClubs are different. Club is a model that represents a team of people and user_clubs is the model that represents the many-to-many relationship between Users and Clubs.

First, can someone explain me how the call to user_clubs_path followed by the arguments know that has to go to the action create of user_clubs controller?

In second, the objective problem, why is this an error?

Upvotes: 0

Views: 465

Answers (2)

markets
markets

Reputation: 7033

First question

Because of your routes definition, type into a terminal:

rake routes

And you'll see all generated routes and its associated helpers. First column (rake output) references the named helper: user_clubs => user_clubs_path):

Second question

You should add the parameters into user_club key, because you're requiring (by strong_parameters) this "scope" params.require(:user_club):

user_clubs_path(:user_club => {:user_id => current_user.id, :club_id => @club.id, :join_date => Date.current})

You'll receive in the controller:

{
  "_method" => "post",
  "authenticity_token" => "...",
  "user_club" => {
    "club_id" => "1",
    "join_date"=> "2014-11-17",
    "user_id"=> "2"
  }
}

Upvotes: 1

Jiř&#237; Posp&#237;šil
Jiř&#237; Posp&#237;šil

Reputation: 14412

The parameters need to be nested under the user_club key. Try this instead:

user_clubs_path(:user_club => {:user_id => current_user.id, :club_id => @club.id, :join_date => Date.current})

Upvotes: 1

Related Questions