link_to custom action/route...

113 views
Skip to first unread message

Mark Locklear

unread,
Jul 8, 2013, 3:12:00 PM7/8/13
to ashevi...@googlegroups.com
So, I have a somewhat complicated route that looks like this...

http://localhost:3000/rubrics/55/results/by_cred

In my config/routes I have:   match 'rubrics/:id/results/by_cred' => 'results#by_cred'

...and the action itself is pretty basic:

  def by_cred
    @rubric = Rubric.find_by_id(params[:id])
    @results = Result.find_all_by_rubric_id(params[:id])
  end

However, when I run 'rake routes' I don't get anything in the path column...

<no handy link_to helper here>        /rubrics/:id/results/by_cred(.:format)         results#by_cred

The route/action works great if I manually type it in the browser, however I want to add a link to this route in the view. How can I do that?




--
J. Mark Locklear
-Philippians 4:13 gives you the muscle, but YOU have to flex it!

"One machine can do the work of fifty ordinary persons. No machine can do the work of one extraordinary individual."
-- Elbert Hubbard

Tony Beninate

unread,
Jul 8, 2013, 3:16:13 PM7/8/13
to ashevi...@googlegroups.com
Try changing `match` to `get`?


--
You received this message because you are subscribed to the Google Groups "Asheville Ruby Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asheville-rb...@googlegroups.com.
To post to this group, send email to ashevi...@googlegroups.com.
Visit this group at http://groups.google.com/group/asheville-rb.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark Locklear

unread,
Jul 8, 2013, 3:21:24 PM7/8/13
to ashevi...@googlegroups.com
No change. Route still works in the browser, but no change in 'rake routes'.

JGrubb

unread,
Jul 8, 2013, 3:25:46 PM7/8/13
to ashevi...@googlegroups.com
match 'rubrics/:id/results/by_cred' => 'results#by_cred', :as => route_name
will give you route_name_path and route_name_url helpers. 

Phillip Miller

unread,
Jul 8, 2013, 3:34:50 PM7/8/13
to ashevi...@googlegroups.com
Is Rubric a resource? What version of Rails? It looks like results is a nested resource under rubrics. 

Rails 3 
resources :rubric do 
  resources :results do 
    member { :get => :by_cred}
  end
end

If not, I believe Tony's approach is the next best practice but to get a named route you need to supply :as => 'by_cred' to the get method. 

Mark Locklear

unread,
Jul 8, 2013, 3:38:12 PM7/8/13
to ashevi...@googlegroups.com
Here is config/routes, and 'rake routes'...

http://pastie.org/8122127

Levi Kennedy

unread,
Jul 8, 2013, 3:38:28 PM7/8/13
to ashevi...@googlegroups.com
You need to add on an "as" to the end.

:as => "results_by_cred"

Should do the trick. You will end up with a results_by_cred_path helpers this that you pass a result id to.

The other option is to nest the results resource inside of the rubric resource and add a "collection" resource to it. The rails guides on routing should point you in the right direction for that. I'd type out the code but I'm on my iPhone.

Levi

Sent from my iPhone

Phillip Miller

unread,
Jul 8, 2013, 3:44:39 PM7/8/13
to ashevi...@googlegroups.com
Mark, my code above should work with your nested routes setup without the extra get statement if you replace member with collection as Levi has mentioned. Member would require a Results id. 

Mark Locklear

unread,
Jul 8, 2013, 3:44:42 PM7/8/13
to ashevi...@googlegroups.com
Boom! That worked Levi. Thanx!


On Mon, Jul 8, 2013 at 3:38 PM, Levi Kennedy <lcke...@gmail.com> wrote:

John Grubb

unread,
Jul 8, 2013, 3:22:57 PM7/8/13
to ashevi...@googlegroups.com
match 'rubrics/:id/results/by_cred' => 'results#by_cred', :as => ROUTE_NAME

will give you route_name_path and route_name_url helpers.

John Grubb
404.213.2327
Reply all
Reply to author
Forward
0 new messages