While learning and testing with the very good ONLamp.com-Tutorial I want to save some code-optimisations and comments.
Please add yours.
The idea is to have an improved tutorial afterwards.
______________________________________
The idea is good: Parsing the whole list of recipies to the template and use the if-statement to filter.
_recipe_controller.rb_
def list
@category = @params‘category’
@recipes = Recipe.find_all
end
recipe\list.rhtml
<%recipes.each do |recipe| %> <% if (category == nil) || (@category == recipe.category.name)>
< end >
< end %>
But this should not have the best performance.
Better would be to parse only the information needed:
_recipe_controller.rb_
def list
category = @params['category'] if @params[:tenttype] @recipes = Recipe.find(:all, :conditions => ["category_id = ?", Category.find_by_name(params[:category]).id])
else
@recipes = Recipe.find_all
end
end
recipe\list.rhtml only
<% @recipes.each do |recipe| >
< end %>
I made the standard-layout that generates these links at the bottom of every page:
“Create new recipe Show all recipes Show all categories”
The last one directs to /category?actions=list and that works just fine, giving me the category list.
But the first two don’t work. They are looking for an index file that isn’t there. (I cannot paste the path, since the sp@m blocker is killing edits that appear to have urls. How incredibly frustrating.)
Why does the last work, but the first two do not? Is it because my apps/views/recipe directory isn’t empty?
category: Tutorial