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
<% if @ads.empty? %>
Hittade inga annonser.
<% else %>
<%= render @ads %>
<% end %>
<%= will_paginate @ads, :previous_label => t(:previous), :next_label
=> t(:next) %>
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:
-
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.)
-
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
- Then restart your rails server:
If Rails 3:
rails server --debugger
If < Rails 3:
script/server --debugger
- 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.