Pregunta

I have a route with an optional parameter

name GET    /:name(/:age)(.:format)         users#name

In the associated controller I want to say that if the optional age parameter isn't passed (as in a url like localhost:3000/FirstName%20Lastname/) then bring them to a route with an age such as localhost:3000/FirstName%20Lastname/25.

My attempt in the controller is here:

def name
 @user = User.find_by_name(params[:name])
 !params[:age].nil? ? gon.currentyr = params[:age] : render name_path(@user.age)
 ...
end

But this results in a random error "syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' I don't think the problem is actually syntax because when I change it to

!params[:age].nil? ? gon.currentyr = params[:age] : gon.currentyr = @user.age

It works without putting adding the age parameter to the end of the URL.

Thanks!

¿Fue útil?

Solución

Try something like this:

def name
  redirect_to users(params[:name], 25) if params[:age].blank?
  # (rest of your action)
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top