Anders_P
2010-Oct-09 12:41 UTC
Newbie - Create menu and send parameters to controller from view.
Hello! I''m trying to create a menu. I have a controller that takes two attributes, like this: def category(gender, category) @ads = Ad.find_all_by_gender_and_category(gender, category).paginate(:page => params[:page]) @title = "Startsidan" render ''home'' end And in my view I have a list with menu options: <li class="li_content"><%= link_to "Accessoarer", :action => "category(''dam'', ''Accessoarer'')" %></li><br /> And my routes it look like this: match ''/dam-accessoarer'', :to => "pages#category(''dam'', ''Accessoarer'')" But it doesn''t work. I get the following error. Unknown action The action ''category(''dam'', ''Accessoarer'')'' could not be found for PagesController Any help on how to solve it would be greatly appreciated! Thanks! // Anders -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Oct-09 15:23 UTC
Re: Newbie - Create menu and send parameters to controller from view.
On Sat, Oct 9, 2010 at 7:41 AM, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Hello! > > I''m trying to create a menu. I have a controller that takes two > attributes, like this: > > def category(gender, category) > @ads = Ad.find_all_by_gender_and_category(gender, > category).paginate(:page => params[:page]) > @title = "Startsidan" > render ''home'' > end >Is this your controller code? If so you have issues as (someone correct me if this is a new Rails 3 construct that I am clueless) you should not be taking arguments in the signature. If the params gender and category are needed they should be accessed as params[:gender] params[:category]. So in your view you need something like this below. Also see http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to for info on link_to helper. <%= link_to "Accessories", :controller => "categories", :action => "show", :gender => "male", :category => "zzz" and then in your controller action def show gender = params[:gender] category = params[:category] # now do what you want with the data end David> And in my view I have a list with menu options: > > <li class="li_content"><%= link_to "Accessoarer", :action => > "category(''dam'', ''Accessoarer'')" %></li><br /> > > And my routes it look like this: > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > ''Accessoarer'')" > > But it doesn''t work. > > I get the following error. > > Unknown action > > The action ''category(''dam'', ''Accessoarer'')'' could not be found for > PagesController > > Any help on how to solve it would be greatly appreciated! > > Thanks! > // Anders > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anders_P
2010-Oct-09 16:13 UTC
Re: Newbie - Create menu and send parameters to controller from view.
Hi! Thanks for your help. But I still doesn''t get it to work. I''ve changed the controller action so it looks like: def category gender = params[:gender] category = params[:category] @ads = Ad.find_all_by_gender_and_category(gender, category).paginate(:page => params[:page]) @title = "Startsidan" render ''home'' end And in the view: <li class="li_content"><%= link_to "Accessoarer", :action => ''category'', :gender => ''female'', :category => ''Accessoarer'' %></ li><br /> When I press the link the url changes to this: http://localhost:3000/kategori?gender=female&category=Accessoarer I don''t get any errors, but it doesn''t find anything. Any idea on what might be wrong? // Anders On 9 Okt, 17:23, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> On Sat, Oct 9, 2010 at 7:41 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: > > Hello! > > > I''m trying to create a menu. I have a controller that takes two > > attributes, like this: > > > def category(gender, category) > > @ads = Ad.find_all_by_gender_and_category(gender, > > category).paginate(:page => params[:page]) > > @title = "Startsidan" > > render ''home'' > > end > > Is this your controller code? If so you have issues as (someone correct me > if this is a new Rails 3 construct that I am clueless) you should not be > taking arguments in the signature. If the params gender and category are > needed they should be accessed as params[:gender] params[:category]. > > So in your view you need something like this below. Also seehttp://apidock.com/rails/ActionView/Helpers/UrlHelper/link_tofor info on > link_to helper. > > <%= link_to "Accessories", :controller => "categories", :action => "show", > :gender => "male", :category => "zzz" > > and then in your controller action > > def show > gender = params[:gender] > category = params[:category] > # now do what you want with the data > end > > David > > > > > And in my view I have a list with menu options: > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > "category(''dam'', ''Accessoarer'')" %></li><br /> > > > And my routes it look like this: > > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > > ''Accessoarer'')" > > > But it doesn''t work. > > > I get the following error. > > > Unknown action > > > The action ''category(''dam'', ''Accessoarer'')'' could not be found for > > PagesController > > > Any help on how to solve it would be greatly appreciated! > > > Thanks! > > // Anders > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Oct-09 16:21 UTC
Re: Re: Newbie - Create menu and send parameters to controller from view.
On Sat, Oct 9, 2010 at 11:13 AM, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Hi! Thanks for your help. But I still doesn''t get it to work. I''ve > changed the controller action so it looks like: > > def category > gender = params[:gender] > category = params[:category] > @ads = Ad.find_all_by_gender_and_category(gender, > category).paginate(:page => params[:page]) > @title = "Startsidan" > render ''home'' > end > > And in the view: > > <li class="li_content"><%= link_to "Accessoarer", :action => > ''category'', :gender => ''female'', :category => ''Accessoarer'' %></ > li><br /> > > When I press the link the url changes to this: > > http://localhost:3000/kategori?gender=female&category=Accessoarer > > I don''t get any errors, but it doesn''t find anything. Any idea on what > might be wrong? >So you are rendering the ''home'' view - can you show me this? Also, describe what you are expecting to see. Have you also verified that you are getting good values on @ads and @title (which I assume you are doing something with in your view)? I would recommend if you have not used it yet to get familiar with ruby-debug. This way you can step in to your controller in action and check these attributes (of course even better is get used to writing tests for your code, but I know this might be overwhelming at first). Let me know - if you need help with ruby-debug let me know --- if so do give me the version of Rails and version of Ruby you are running.> > // Anders > > > On 9 Okt, 17:23, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > On Sat, Oct 9, 2010 at 7:41 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > wrote: > > > Hello! > > > > > I''m trying to create a menu. I have a controller that takes two > > > attributes, like this: > > > > > def category(gender, category) > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > category).paginate(:page => params[:page]) > > > @title = "Startsidan" > > > render ''home'' > > > end > > > > Is this your controller code? If so you have issues as (someone correct > me > > if this is a new Rails 3 construct that I am clueless) you should not be > > taking arguments in the signature. If the params gender and category are > > needed they should be accessed as params[:gender] params[:category]. > > > > So in your view you need something like this below. Also seehttp:// > apidock.com/rails/ActionView/Helpers/UrlHelper/link_tofor info on > > link_to helper. > > > > <%= link_to "Accessories", :controller => "categories", :action => > "show", > > :gender => "male", :category => "zzz" > > > > and then in your controller action > > > > def show > > gender = params[:gender] > > category = params[:category] > > # now do what you want with the data > > end > > > > David > > > > > > > > > And in my view I have a list with menu options: > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > "category(''dam'', ''Accessoarer'')" %></li><br /> > > > > > And my routes it look like this: > > > > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > > > ''Accessoarer'')" > > > > > But it doesn''t work. > > > > > I get the following error. > > > > > Unknown action > > > > > The action ''category(''dam'', ''Accessoarer'')'' could not be found for > > > PagesController > > > > > Any help on how to solve it would be greatly appreciated! > > > > > Thanks! > > > // Anders > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anders_P
2010-Oct-09 17:17 UTC
Re: Newbie - Create menu and send parameters to controller from view.
Hi! Here is what I think it the most relevant part of the home view, # Renders the menu <%= render "shared/categories" %> . . . # Renders ads, unless its empty <div class="right_container body_copy"> <% if @ads.empty? %> Hittade inga annonser. <% else %> <%= render @ads %> <% end %> </div> <div class="paginate body_copy"> <%= will_paginate @ads, :previous_label => t(:previous), :next_label => t(:next) %> </div> I''m getting "Hittade inga annonser" which means "Couldn''t find any ads" when I click on the link, but it shouldn''t be empty. It should render all ads that matches a certain condition, for example, :gender => ''female'', category => '' accessories''. I have checked the database and it should get two rows. It works when I have methods like this: def female_accessories @ads = Ad.find_all_by_gender_and_category(''dam'', ''Accessoarer'').paginate(:page => params[:page]) @title = "Startsidan" render ''home'' end But this isn''t very DRY, since I would need to have an action for every category. I want something slightly more dynamic. I hope i''m understandable. Tips and links regarding ruby-debug would be really nice! Thanks! // Anders On 9 Okt, 18:21, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> On Sat, Oct 9, 2010 at 11:13 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: > > Hi! Thanks for your help. But I still doesn''t get it to work. I''ve > > changed the controller action so it looks like: > > > def category > > gender = params[:gender] > > category = params[:category] > > @ads = Ad.find_all_by_gender_and_category(gender, > > category).paginate(:page => params[:page]) > > @title = "Startsidan" > > render ''home'' > > end > > > And in the view: > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > ''category'', :gender => ''female'', :category => ''Accessoarer'' %></ > > li><br /> > > > When I press the link the url changes to this: > > >http://localhost:3000/kategori?gender=female&category=Accessoarer > > > I don''t get any errors, but it doesn''t find anything. Any idea on what > > might be wrong? > > So you are rendering the ''home'' view - can you show me this? Also, describe > what you are expecting to see. Have you also verified that you are getting > good values on @ads and @title (which I assume you are doing something with > in your view)? I would recommend if you have not used it yet to get familiar > with ruby-debug. This way you can step in to your controller in action and > check these attributes (of course even better is get used to writing tests > for your code, but I know this might be overwhelming at first). > > Let me know - if you need help with ruby-debug let me know --- if so do give > me the version of Rails and version of Ruby you are running. > > > > > > > // Anders > > > On 9 Okt, 17:23, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > On Sat, Oct 9, 2010 at 7:41 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > > wrote: > > > > Hello! > > > > > I''m trying to create a menu. I have a controller that takes two > > > > attributes, like this: > > > > > def category(gender, category) > > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > > category).paginate(:page => params[:page]) > > > > @title = "Startsidan" > > > > render ''home'' > > > > end > > > > Is this your controller code? If so you have issues as (someone correct > > me > > > if this is a new Rails 3 construct that I am clueless) you should not be > > > taking arguments in the signature. If the params gender and category are > > > needed they should be accessed as params[:gender] params[:category]. > > > > So in your view you need something like this below. Also seehttp:// > > apidock.com/rails/ActionView/Helpers/UrlHelper/link_tofor info on > > > link_to helper. > > > > <%= link_to "Accessories", :controller => "categories", :action => > > "show", > > > :gender => "male", :category => "zzz" > > > > and then in your controller action > > > > def show > > > gender = params[:gender] > > > category = params[:category] > > > # now do what you want with the data > > > end > > > > David > > > > > And in my view I have a list with menu options: > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > > "category(''dam'', ''Accessoarer'')" %></li><br /> > > > > > And my routes it look like this: > > > > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > > > > ''Accessoarer'')" > > > > > But it doesn''t work. > > > > > I get the following error. > > > > > Unknown action > > > > > The action ''category(''dam'', ''Accessoarer'')'' could not be found for > > > > PagesController > > > > > Any help on how to solve it would be greatly appreciated! > > > > > Thanks! > > > > // Anders > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Oct-09 17:37 UTC
Re: Re: Newbie - Create menu and send parameters to controller from view.
On Sat, Oct 9, 2010 at 12:17 PM, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Hi! > > Here is what I think it the most relevant part of the home view, > > # Renders the menu > <%= render "shared/categories" %> > . > . > . > # Renders ads, unless its empty > <div class="right_container body_copy"> > <% if @ads.empty? %> > Hittade inga annonser. > <% else %> > <%= render @ads %> > <% end %> > </div> > > <div class="paginate body_copy"> > <%= will_paginate @ads, :previous_label => t(:previous), :next_label > => t(:next) %> > </div> > > I''m getting "Hittade inga annonser" which means "Couldn''t find any > ads" when I click on the link, but it shouldn''t be empty. It should > render all ads that matches a certain condition, for example, :gender > => ''female'', category => '' accessories''. I have checked the database > and it should get two rows. > > It works when I have methods like this: > > def female_accessories > @ads = Ad.find_all_by_gender_and_category(''dam'', > ''Accessoarer'').paginate(:page => params[:page]) > @title = "Startsidan" > render ''home'' > end >> But this isn''t very DRY, since I would need to have an action for >> every category. I want something slightly more dynamic. >Right - this is going to be a very ugly controller in no time. But fix the problem at hand and then you can play with making it dry. Google the term ''named scopes'' in google and this may help. If nothing else, move this kind of logic into a model, it should not be in a controller. Controller ideally just handles requests and routing of them, business logic should be in models or other classes.> > I hope i''m understandable. > > Tips and links regarding ruby-debug would be really nice! >This is probably a good place to pick up ruby debug. Do this: 1) gem install ruby-debug (note if you are using Rails 3 and Ruby 1.9.2 you might need: gem install ruby-debug19. This is what I am using.) 2) Now you can put a breakpoint wherever you want by typing ''debugger'' where you want the execution to stop --- so for example: def female_accessories debugger @ads = Ad.find_all_by_gender_and_category(''dam'', ''Accessoarer'').paginate(:page => params[:page]) @title = "Startsidan" render ''home'' end 3) Then restart your rails server: If Rails 3: rails server --debugger If < Rails 3: script/server --debugger 4) Now execute your page - load the page. The execution will stop when you get to the break point. Go to your server console and you should see some familiar code and line number. From there you can type in whatever ruby you want to debug (like irb). Type ''n'' to go to the next line, ''c'' to continue. Using this you should be able to figure out what is going on in your code. Great tool to have, I use it often several times a day.> > Thanks! > // Anders > > > > On 9 Okt, 18:21, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > On Sat, Oct 9, 2010 at 11:13 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > wrote: > > > Hi! Thanks for your help. But I still doesn''t get it to work. I''ve > > > changed the controller action so it looks like: > > > > > def category > > > gender = params[:gender] > > > category = params[:category] > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > category).paginate(:page => params[:page]) > > > @title = "Startsidan" > > > render ''home'' > > > end > > > > > And in the view: > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > ''category'', :gender => ''female'', :category => ''Accessoarer'' %></ > > > li><br /> > > > > > When I press the link the url changes to this: > > > > >http://localhost:3000/kategori?gender=female&category=Accessoarer > > > > > I don''t get any errors, but it doesn''t find anything. Any idea on what > > > might be wrong? > > > > So you are rendering the ''home'' view - can you show me this? Also, > describe > > what you are expecting to see. Have you also verified that you are > getting > > good values on @ads and @title (which I assume you are doing something > with > > in your view)? I would recommend if you have not used it yet to get > familiar > > with ruby-debug. This way you can step in to your controller in action > and > > check these attributes (of course even better is get used to writing > tests > > for your code, but I know this might be overwhelming at first). > > > > Let me know - if you need help with ruby-debug let me know --- if so do > give > > me the version of Rails and version of Ruby you are running. > > > > > > > > > > > > > // Anders > > > > > On 9 Okt, 17:23, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > > On Sat, Oct 9, 2010 at 7:41 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > > > wrote: > > > > > Hello! > > > > > > > I''m trying to create a menu. I have a controller that takes two > > > > > attributes, like this: > > > > > > > def category(gender, category) > > > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > > > category).paginate(:page => params[:page]) > > > > > @title = "Startsidan" > > > > > render ''home'' > > > > > end > > > > > > Is this your controller code? If so you have issues as (someone > correct > > > me > > > > if this is a new Rails 3 construct that I am clueless) you should not > be > > > > taking arguments in the signature. If the params gender and category > are > > > > needed they should be accessed as params[:gender] params[:category]. > > > > > > So in your view you need something like this below. Also seehttp:// > > > apidock.com/rails/ActionView/Helpers/UrlHelper/link_tofor info on > > > > link_to helper. > > > > > > <%= link_to "Accessories", :controller => "categories", :action => > > > "show", > > > > :gender => "male", :category => "zzz" > > > > > > and then in your controller action > > > > > > def show > > > > gender = params[:gender] > > > > category = params[:category] > > > > # now do what you want with the data > > > > end > > > > > > David > > > > > > > And in my view I have a list with menu options: > > > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > > > "category(''dam'', ''Accessoarer'')" %></li><br /> > > > > > > > And my routes it look like this: > > > > > > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > > > > > ''Accessoarer'')" > > > > > > > But it doesn''t work. > > > > > > > I get the following error. > > > > > > > Unknown action > > > > > > > The action ''category(''dam'', ''Accessoarer'')'' could not be found for > > > > > PagesController > > > > > > > Any help on how to solve it would be greatly appreciated! > > > > > > > Thanks! > > > > > // Anders > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Ruby on Rails: Talk" group. > > > > > To post to this group, send email to > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > . > > > > > To unsubscribe from this group, send email to > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > e@googlegroups.com> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anders_P
2010-Oct-09 19:07 UTC
Re: Newbie - Create menu and send parameters to controller from view.
Thanks for your tips, I think I will follow your suggestion and create a new model ''Category'', think it will get a lot cleaner. Thanks. // Anders On 9 Okt, 19:37, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote:> On Sat, Oct 9, 2010 at 12:17 PM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: > > Hi! > > > Here is what I think it the most relevant part of the home view, > > > # Renders the menu > > <%= render "shared/categories" %> > > . > > . > > . > > # Renders ads, unless its empty > > <div class="right_container body_copy"> > > <% if @ads.empty? %> > > Hittade inga annonser. > > <% else %> > > <%= render @ads %> > > <% end %> > > </div> > > > <div class="paginate body_copy"> > > <%= will_paginate @ads, :previous_label => t(:previous), :next_label > > => t(:next) %> > > </div> > > > I''m getting "Hittade inga annonser" which means "Couldn''t find any > > ads" when I click on the link, but it shouldn''t be empty. It should > > render all ads that matches a certain condition, for example, :gender > > => ''female'', category => '' accessories''. I have checked the database > > and it should get two rows. > > > It works when I have methods like this: > > > def female_accessories > > @ads = Ad.find_all_by_gender_and_category(''dam'', > > ''Accessoarer'').paginate(:page => params[:page]) > > @title = "Startsidan" > > render ''home'' > > end > > > But this isn''t very DRY, since I would need to have an action for > > > every category. I want something slightly more dynamic. > > Right - this is going to be a very ugly controller in no time. But fix the > problem at hand and then you can play with making it dry. Google the term > ''named scopes'' in google and this may help. If nothing else, move this kind > of logic into a model, it should not be in a controller. Controller ideally > just handles requests and routing of them, business logic should be in > models or other classes. > > > > > I hope i''m understandable. > > > Tips and links regarding ruby-debug would be really nice! > > This is probably a good place to pick up ruby debug. > > Do this: > 1) gem install ruby-debug > (note if you are using Rails 3 and Ruby 1.9.2 you might need: gem install > ruby-debug19. This is what I am using.) > > 2) Now you can put a breakpoint wherever you want by typing ''debugger'' where > you want the execution to stop --- so for example: > > def female_accessories > debugger > @ads = Ad.find_all_by_gender_and_category(''dam'', > ''Accessoarer'').paginate(:page => params[:page]) > @title = "Startsidan" > render ''home'' > end > > 3) Then restart your rails server: > > If Rails 3: > rails server --debugger > > If < Rails 3: > script/server --debugger > > 4) Now execute your page - load the page. The execution will stop when you > get to the break point. Go to your server console and you should see some > familiar code and line number. From there you can type in whatever ruby you > want to debug (like irb). Type ''n'' to go to the next line, ''c'' to continue. > > Using this you should be able to figure out what is going on in your code. > Great tool to have, I use it often several times a day. > > > > > > > Thanks! > > // Anders > > > On 9 Okt, 18:21, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > On Sat, Oct 9, 2010 at 11:13 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > > wrote: > > > > Hi! Thanks for your help. But I still doesn''t get it to work. I''ve > > > > changed the controller action so it looks like: > > > > > def category > > > > gender = params[:gender] > > > > category = params[:category] > > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > > category).paginate(:page => params[:page]) > > > > @title = "Startsidan" > > > > render ''home'' > > > > end > > > > > And in the view: > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > > ''category'', :gender => ''female'', :category => ''Accessoarer'' %></ > > > > li><br /> > > > > > When I press the link the url changes to this: > > > > >http://localhost:3000/kategori?gender=female&category=Accessoarer > > > > > I don''t get any errors, but it doesn''t find anything. Any idea on what > > > > might be wrong? > > > > So you are rendering the ''home'' view - can you show me this? Also, > > describe > > > what you are expecting to see. Have you also verified that you are > > getting > > > good values on @ads and @title (which I assume you are doing something > > with > > > in your view)? I would recommend if you have not used it yet to get > > familiar > > > with ruby-debug. This way you can step in to your controller in action > > and > > > check these attributes (of course even better is get used to writing > > tests > > > for your code, but I know this might be overwhelming at first). > > > > Let me know - if you need help with ruby-debug let me know --- if so do > > give > > > me the version of Rails and version of Ruby you are running. > > > > > // Anders > > > > > On 9 Okt, 17:23, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > > > On Sat, Oct 9, 2010 at 7:41 AM, Anders_P <and...@andersplanting.se> > > > > wrote: > > > > > > Hello! > > > > > > > I''m trying to create a menu. I have a controller that takes two > > > > > > attributes, like this: > > > > > > > def category(gender, category) > > > > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > > > > category).paginate(:page => params[:page]) > > > > > > @title = "Startsidan" > > > > > > render ''home'' > > > > > > end > > > > > > Is this your controller code? If so you have issues as (someone > > correct > > > > me > > > > > if this is a new Rails 3 construct that I am clueless) you should not > > be > > > > > taking arguments in the signature. If the params gender and category > > are > > > > > needed they should be accessed as params[:gender] params[:category]. > > > > > > So in your view you need something like this below. Also seehttp:// > > > > apidock.com/rails/ActionView/Helpers/UrlHelper/link_tofor info on > > > > > link_to helper. > > > > > > <%= link_to "Accessories", :controller => "categories", :action => > > > > "show", > > > > > :gender => "male", :category => "zzz" > > > > > > and then in your controller action > > > > > > def show > > > > > gender = params[:gender] > > > > > category = params[:category] > > > > > # now do what you want with the data > > > > > end > > > > > > David > > > > > > > And in my view I have a list with menu options: > > > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > > > > "category(''dam'', ''Accessoarer'')" %></li><br /> > > > > > > > And my routes it look like this: > > > > > > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > > > > > > ''Accessoarer'')" > > > > > > > But it doesn''t work. > > > > > > > I get the following error. > > > > > > > Unknown action > > > > > > > The action ''category(''dam'', ''Accessoarer'')'' could not be found for > > > > > > PagesController > > > > > > > Any help on how to solve it would be greatly appreciated! > > > > > > > Thanks! > > > > > > // Anders > > > > > > > -- > > > > > > You received this message because you are subscribed to the Google > > > > Groups > > > > > > "Ruby on Rails: Talk" group. > > > > > > To post to this group, send email to > > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > . > > > > > > To unsubscribe from this group, send email to > > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > e@googlegroups.com> > > > > > > . > > > > > > For more options, visit this group at > > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to rubyonrails-talk@googlegroups.com > > . > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > e@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscrib e@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Oct-09 19:24 UTC
Re: Re: Newbie - Create menu and send parameters to controller from view.
Glad to help. Definitely if you have not, at least for your models try to start writing some tests -- I use rspec now but honestly if you are new to tdd you could use test/unit just fine and get used to the process. The way I see it is in the past before I wrote tests, I would write code and then spend time debugging. I found that writing tests as I go actually formalizes the debugging process I would go through, and then of course the result is a test suite you can run whenever you want --- and at least you will know the things you are testing for still work! I promise you will get addicted. On Sat, Oct 9, 2010 at 2:07 PM, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Thanks for your tips, I think I will follow your suggestion and create > a new model ''Category'', think it will get a lot cleaner. > > Thanks. > // Anders > > On 9 Okt, 19:37, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > On Sat, Oct 9, 2010 at 12:17 PM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > wrote: > > > Hi! > > > > > Here is what I think it the most relevant part of the home view, > > > > > # Renders the menu > > > <%= render "shared/categories" %> > > > . > > > . > > > . > > > # Renders ads, unless its empty > > > <div class="right_container body_copy"> > > > <% if @ads.empty? %> > > > Hittade inga annonser. > > > <% else %> > > > <%= render @ads %> > > > <% end %> > > > </div> > > > > > <div class="paginate body_copy"> > > > <%= will_paginate @ads, :previous_label => t(:previous), > :next_label > > > => t(:next) %> > > > </div> > > > > > I''m getting "Hittade inga annonser" which means "Couldn''t find any > > > ads" when I click on the link, but it shouldn''t be empty. It should > > > render all ads that matches a certain condition, for example, :gender > > > => ''female'', category => '' accessories''. I have checked the database > > > and it should get two rows. > > > > > It works when I have methods like this: > > > > > def female_accessories > > > @ads = Ad.find_all_by_gender_and_category(''dam'', > > > ''Accessoarer'').paginate(:page => params[:page]) > > > @title = "Startsidan" > > > render ''home'' > > > end > > > > > But this isn''t very DRY, since I would need to have an action for > > > > > every category. I want something slightly more dynamic. > > > > Right - this is going to be a very ugly controller in no time. But fix > the > > problem at hand and then you can play with making it dry. Google the term > > ''named scopes'' in google and this may help. If nothing else, move this > kind > > of logic into a model, it should not be in a controller. Controller > ideally > > just handles requests and routing of them, business logic should be in > > models or other classes. > > > > > > > > > I hope i''m understandable. > > > > > Tips and links regarding ruby-debug would be really nice! > > > > This is probably a good place to pick up ruby debug. > > > > Do this: > > 1) gem install ruby-debug > > (note if you are using Rails 3 and Ruby 1.9.2 you might need: gem install > > ruby-debug19. This is what I am using.) > > > > 2) Now you can put a breakpoint wherever you want by typing ''debugger'' > where > > you want the execution to stop --- so for example: > > > > def female_accessories > > debugger > > @ads = Ad.find_all_by_gender_and_category(''dam'', > > ''Accessoarer'').paginate(:page => params[:page]) > > @title = "Startsidan" > > render ''home'' > > end > > > > 3) Then restart your rails server: > > > > If Rails 3: > > rails server --debugger > > > > If < Rails 3: > > script/server --debugger > > > > 4) Now execute your page - load the page. The execution will stop when > you > > get to the break point. Go to your server console and you should see some > > familiar code and line number. From there you can type in whatever ruby > you > > want to debug (like irb). Type ''n'' to go to the next line, ''c'' to > continue. > > > > Using this you should be able to figure out what is going on in your > code. > > Great tool to have, I use it often several times a day. > > > > > > > > > > > > > Thanks! > > > // Anders > > > > > On 9 Okt, 18:21, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > > On Sat, Oct 9, 2010 at 11:13 AM, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > > > wrote: > > > > > Hi! Thanks for your help. But I still doesn''t get it to work. I''ve > > > > > changed the controller action so it looks like: > > > > > > > def category > > > > > gender = params[:gender] > > > > > category = params[:category] > > > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > > > category).paginate(:page => params[:page]) > > > > > @title = "Startsidan" > > > > > render ''home'' > > > > > end > > > > > > > And in the view: > > > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > > > ''category'', :gender => ''female'', :category => ''Accessoarer'' %></ > > > > > li><br /> > > > > > > > When I press the link the url changes to this: > > > > > > >http://localhost:3000/kategori?gender=female&category=Accessoarer > > > > > > > I don''t get any errors, but it doesn''t find anything. Any idea on > what > > > > > might be wrong? > > > > > > So you are rendering the ''home'' view - can you show me this? Also, > > > describe > > > > what you are expecting to see. Have you also verified that you are > > > getting > > > > good values on @ads and @title (which I assume you are doing > something > > > with > > > > in your view)? I would recommend if you have not used it yet to get > > > familiar > > > > with ruby-debug. This way you can step in to your controller in > action > > > and > > > > check these attributes (of course even better is get used to writing > > > tests > > > > for your code, but I know this might be overwhelming at first). > > > > > > Let me know - if you need help with ruby-debug let me know --- if so > do > > > give > > > > me the version of Rails and version of Ruby you are running. > > > > > > > // Anders > > > > > > > On 9 Okt, 17:23, David Kahn <d...-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org> wrote: > > > > > > On Sat, Oct 9, 2010 at 7:41 AM, Anders_P < > and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> > > > > > wrote: > > > > > > > Hello! > > > > > > > > > I''m trying to create a menu. I have a controller that takes two > > > > > > > attributes, like this: > > > > > > > > > def category(gender, category) > > > > > > > @ads = Ad.find_all_by_gender_and_category(gender, > > > > > > > category).paginate(:page => params[:page]) > > > > > > > @title = "Startsidan" > > > > > > > render ''home'' > > > > > > > end > > > > > > > > Is this your controller code? If so you have issues as (someone > > > correct > > > > > me > > > > > > if this is a new Rails 3 construct that I am clueless) you should > not > > > be > > > > > > taking arguments in the signature. If the params gender and > category > > > are > > > > > > needed they should be accessed as params[:gender] > params[:category]. > > > > > > > > So in your view you need something like this below. Also > seehttp:// > > > > > apidock.com/rails/ActionView/Helpers/UrlHelper/link_tofor info on > > > > > > link_to helper. > > > > > > > > <%= link_to "Accessories", :controller => "categories", :action > => > > > > > "show", > > > > > > :gender => "male", :category => "zzz" > > > > > > > > and then in your controller action > > > > > > > > def show > > > > > > gender = params[:gender] > > > > > > category = params[:category] > > > > > > # now do what you want with the data > > > > > > end > > > > > > > > David > > > > > > > > > And in my view I have a list with menu options: > > > > > > > > > <li class="li_content"><%= link_to "Accessoarer", :action => > > > > > > > "category(''dam'', ''Accessoarer'')" %></li><br /> > > > > > > > > > And my routes it look like this: > > > > > > > > > match ''/dam-accessoarer'', :to => "pages#category(''dam'', > > > > > > > ''Accessoarer'')" > > > > > > > > > But it doesn''t work. > > > > > > > > > I get the following error. > > > > > > > > > Unknown action > > > > > > > > > The action ''category(''dam'', ''Accessoarer'')'' could not be found > for > > > > > > > PagesController > > > > > > > > > Any help on how to solve it would be greatly appreciated! > > > > > > > > > Thanks! > > > > > > > // Anders > > > > > > > > > -- > > > > > > > You received this message because you are subscribed to the > Google > > > > > Groups > > > > > > > "Ruby on Rails: Talk" group. > > > > > > > To post to this group, send email to > > > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > > . > > > > > > > To unsubscribe from this group, send email to > > > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > > > e@googlegroups.com> > > > > > > > . > > > > > > > For more options, visit this group at > > > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Ruby on Rails: Talk" group. > > > > > To post to this group, send email to > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > . > > > > > To unsubscribe from this group, send email to > > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com><rubyonrails-talk%2Bunsubscrib > > > e@googlegroups.com> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org><rubyonrails-talk%2Bunsubscrib > e@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.