Creating multiple views for a database table .. routing not working

28 views
Skip to first unread message

Sonali Katara

unread,
Oct 27, 2014, 9:17:38 PM10/27/14
to rubyonra...@googlegroups.com

I have a single table in which I store favourite dishes of users. A user can have more than one entries in the table. I have generated a view to show all the entries in the table but how do I create a view to show just the list of users. I don't have a table saving the users so I have to use DISTINCT to get the names of the users from my one and only table

I appraoched this by creating a new route
/foodsIlike/user_list

get '/foodsIlike/user_list' => 'foodsIlike#user_list', :as => 'user_list'

instead of going to the user_list it tries to find a record with :id=user_list as that is what it would do to show one record

Vivek Sampara

unread,
Oct 28, 2014, 3:47:57 AM10/28/14
to rubyonra...@googlegroups.com
You need to mention :on => :collection on the route. 

get '/foodsIlike/user_list' => 'foodsIlike#user_list', :as => 'user_list', :on => :collection 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a0639623-2ef0-468b-bc7d-7856f1e1c12c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Oct 28, 2014, 4:36:08 AM10/28/14
to rubyonra...@googlegroups.com
On 28 October 2014 01:17, Sonali Katara <sonali...@gmail.com> wrote:
> I have a single table in which I store favourite dishes of users. A user can
> have more than one entries in the table. I have generated a view to show all
> the entries in the table but how do I create a view to show just the list of
> users. I don't have a table saving the users so I have to use DISTINCT to
> get the names of the users from my one and only table

The clue is in the question. You say that a user can have more than
one entry in the table. What that implies is that you consider a user
to be a significant object in your system, and that you have a number
of them, therefore you should have a users table. Then to get the
dishes for a user you would just use something like
current_user.dishes and your problem would be solved.

ggest working right through a good tutorial such as railstutorial.org
( which is free to use online). That tutorial includes the concept of
users and will show you the basics of rails.

Colin

Matt Jones

unread,
Oct 29, 2014, 10:31:33 AM10/29/14
to rubyonra...@googlegroups.com
I'd also recommend reconsidering your schema like the other replies, but the bigger problem here is route order. You're getting this behavior because you have the route that matches `/foodsIlike/:id` is *before* the one that matches `/foodsIlike/user_list` in config/routes.rb

--Matt Jones

Reply all
Reply to author
Forward
0 new messages