Opy Osegs
Opy Osegs

Reputation: 197

ActionController::UrlGenerationErr or: No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:user_id]

I'm getting the error ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:user_id] in the test for my user sign up. The user is redirected to the user's profile after sign up but my test is failing. Below are some lines of my code..

CREATE ACTION IN SESSIONS CONTROLLER

def create
  user = User.find_by(email: params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    log_in user
   params[:session][:remember_me] == '1' ? remember(user) : forget(user)
   redirect_back_or user_profile_path(user, @profile)
  else 
   flash.now[:danger] = 'Invalid email/password combination'
   render 'new'
end

CREATE ACTION IN USER CONTROLLER

def create
  @user = User.new(user_params)
  if @user.save
    log_in @user
    flash[:success] = "Welcome to the Mini Olympics"
    redirect_to user_profile_path(current_user, @profile)
  else
    render 'new'
  end
end

THE SIGNUP TEST (WITH VALID INFORMATION)

test "valid signup information" do
  get signup_path
  assert_difference 'User.count', 1 do
  post_via_redirect user_profile_path, user: { email: "[email protected]",
                     password:  "password",
                     password_confirmation: "password", 
                     profile_atrributes: {user_id: @user.id, name: "Example Test", street: "24 Fred Rd", city: "Cutlin", state: "SW", zipcode: "35478"}}
  end
  assert_template 'profiles/show'
  assert is_logged_in?
end

The test fails at the line post_via_redirect but the actually application works.

Upvotes: 0

Views: 425

Answers (1)

Baldrick
Baldrick

Reputation: 24340

In the test, the user_profile_path is missing its parameters. It should be

post_via_redirect user_profile_path(user), user: { ... }

Upvotes: 1

Related Questions