Use acts-as-taggable-on for tagging. Based previous questions in SO i read that i must minimize logic in the view and create a helper for conditions in the view template. Here’s my code so far. (newby rails)
Routes:
resources :appartments do
collection do
get ':tag', to: 'appartments#index', as: :tag
end
My controller:
def index
if params[:tag]
@apartments = Appartment.tagged_with(params[:tag])
else
@apartments = Appartment.all
end
end
Application helper:
def render_partials_conditionally
if @apartments.include?("family")
render :partial => 'familie'
if @apartments.include?("groups")
render :partial => 'groups'
end
end
end
The idea (i assume) is that i check if the value “familie” exist in the @apartments array with the include? method
Apartment index view
= render_partials_conditionally
When i go to apartments/family the partial _family must showed, but nothing happens…no error, no partails
What am i doing wrong?
Grtz..remco
If
Appartmentis a model, @apartments should be an array of objects, so@apartments.include?("family")and@apartments.include?("groups")will never be true.Instead, the code could be like this:
However I think it is still not good enough. A better approach could be that the helper only return the name of the partial, and then render the partial in view.