0

I have a city page with a jQuery content carousel. The content of the carousel is filed by a each loop.

CityController 
    @events = @city.events.find_all_by_hot(true)
    @activities = @city.activities.find_all_by_hot(true)
    @sights = @city.sights.find_all_by_hot(true)
    @hot = @events + @activities + @sights

Class city
   has_many: events
end

class events
   belongs_to :city
   has_many :attachments, :as => :attachable    
   accepts_nested_attributes_for :attachments
end

Activities and sights models are the same

City view content slider:
  @hot.each do |a|
  a.attachments.each do |a|
  = image_tag(a.file.url, :height =>"325px", :width =>"650px" ), url_path

I want to generate links (url_path) in my each loop...how can I realize this? It cannot place the url_path of the routes because they are dymanic based on which attachment (image) is loaded.

2 Answers 2

1

Although your syntax of image_tag is incorrect you can try this

@hot.each do |hot|
  hot.attachments.each do |a|
    link_to polymorphic_path(a.attachable) do
      image_tag(a.file.url, :height => "325px", :width => "650px")
    end
  end
end

If I understand your problem correctly. Also check out the polymorphic_path helper, which is what you need.

Sign up to request clarification or add additional context in comments.

Comments

0

Am I right that the link should point to the a which can be any of events, activities, sights? as

@hot = @events + @activities + @sights

I would try creating a special controller action in the CityController

   def hottie
     @duck = Kernel.const_get(params[:type]).find_by_id(params[:id])
     redirect_to @duck
   end

then add something like

  match 'hottie/:type/:id' => 'city#hottie', as: 'hot'

which should give you a path helper that you can use as this:

<%=link_to("Open", hot_path(a.class.to_s, a.id)) %>

Addition: this is of course a bit dirty and needs some security things considered (e.g. limit it to show only special types). You could also consider moving the three classes Event, Activities and Sights into a Object Hierarchy using STI; that should eliminate the need of passing the type in the request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.