chris Frisina
chris Frisina

Reputation: 19688

Passing params to the controller rails

I have a few links:

<%= link_to "", '../captures#photos' %>
<%= link_to "", '../captures#videos' %>
<%= link_to "", '../captures#audios' %>

They pass to the captures_controller.rb

How do I parse the paramaters (photos / videos / audios) from not going to index in the controller?

Currently in the controller I have:

def photos
  logger.debug 'photos'
end
def videos
  logger.debug 'videos'
end
def audios
  logger.debug 'audios'
end

def index
  logger.debug 'index'
end

But all of the following urls log "index"?

routes.rb:

# these were when I was trying the links without the hashes
match 'captures/photos' => 'captures#photos'
match 'captures/videos' => 'captures#videos'
match 'captures/audios' => 'captures#audios'

resources :captures
resources :photos
resources :audios
resources :videos

Upvotes: 0

Views: 466

Answers (4)

pmain8
pmain8

Reputation: 166

In rails 4.0 you can attach and pass the attributes through the GET request in your controller.

Upvotes: 1

user24359
user24359

Reputation:

You don't use the controller#action notation in link_to. You have to either give it a path, like /captures/photos or specify some arguments explicitly. Try this:

<%= link_to "", :controller => "captures", :action => "photos" %>

I'd also do this in routes.rb to simplify things:

resources :captures
  get :photos
  get :photo_booths
  get :videos
  get :audios
end

which will let you do this:

<%= link_to "", photos_captures_path %>

Finally, based purely on the limited code you've shown us, it actually sounds like you should have resources named photos, audios, etc, each with their own controllers, by using "captures" as namespace and making separate controllers for each type:

namespace :captures do
  resources :photos
  resources :photo_booths
  #etc
end

Then each one has an index action that serves the current function of each method in your controller. Then your links just look like:

<%= link_to "", captures_photos_path %>

Upvotes: 0

Marc Baumbach
Marc Baumbach

Reputation: 10473

This will heavily depend on your routes defined in the routes.rb file, but regardless you can't use the hash portion (Anything that comes after the #) as part of your routes. In fact browsers won't even send it along to the server anyway. Most likely you want your URLs to look like:

../captures
../captures/photos
../captures/videos
../captures/audios

Typically it's best to not specify your URLs in your link_to functions like this either. You want to use your routing helper functions to provide them in case your routes ever change.

I'd suggest reading the Ruby on Rails Guide to Routes. That will provide a lot of insight into how to use the functions built into Rails to get the most out of your routes.

Upvotes: 0

t56k
t56k

Reputation: 7001

In routes.rb:

get "/captures/:format"

And in whichever captures_controller.rb methods you need it in:

@format = params[:format]

Then anywhere in your relevant view @format will output either photos, videos, or audios.

Upvotes: 0

Related Questions