4

I'm new to rails, and I'm struggling with handling the routing correctly. Right now I have a link called "details," and when the user clicks on it I would like the method "view_details" (within the pages controller) to be called with a location parameter that I'm passing in:

<%= link_to "Details", :controller => "pages", :action => "view_details", 
:location =>  v["coordinates"]%>

In my routes.rb file, I have:

get 'pages/view_details/:location', to: 'pages#view_details'

I am getting the following error: No route matches [GET] "/pages/view_details/latitude=37.3505570824247&longitude=-121.922104884669"

When I rake routes, I see this (with no prefix):

How can I fix this?

2
  • can you share the rake routes output?, what is prefix of the routes? Commented Jul 26, 2017 at 3:59
  • just edited the post, see the inserted picture Commented Jul 26, 2017 at 4:07

1 Answer 1

3

The problem is that you are passing a hash as a value for location parameter so, instead of just adding one parameter (i.e. location), it adds two parameters (i.e. latitud and longitude) and your routing fails.

To fix this you could set your route without location, like this:

get 'pages/view_details', to: 'pages#view_details'

Now, using the same link you have now, you will receive latitud and longitude parameters grouped in location as a query string, something similar to:

pages/view_details?location%5Blatitude%5D=37.3505570824247&location%5Blongitude%5D=121.922104884669

And you can use them in your controller with params (as with any other parameter), for example:

latitude = params[:location][:latitude]
longitud = params[:location][:longitude]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that fixed the error I was getting. However now it seems to be calling the pages#show action and displaying the pages/show view. The view_details action isn't being executed at all. Do you know what could be causing this?
nevermind, I was able to fix it using stackoverflow.com/questions/30633639/…

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.