0

jQueryコンテンツカルーセルを含む都市ページがあります。カルーセルのコンテンツは、各ループによってファイルされます。

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

アクティビティと観光スポットのモデルは同じです

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

各ループでリンク(url_path)を生成したい...どうすればこれを実現できますか?ルートのurl_pathは、ロードされる添付ファイル(画像)に基づいて動的であるため、配置できません。

4

2 に答える 2

1

の構文image_tagは正しくありませんが、これを試すことができます

@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

私があなたの問題を正しく理解していれば。また、必要なものであるpolymorphic_pathヘルパーも確認してください。

于 2012-07-29T14:06:06.163 に答える
0

aリンクは、イベント、アクティビティ、観光スポットのいずれかを指している必要がありますか?なので

@hot = @events + @activities + @sights

CityControllerで特別なコントローラーアクションを作成してみます

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

次に、次のようなものを追加します

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

これにより、次のように使用できるパスヘルパーが提供されます。

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

追加:これはもちろん少し汚いので、いくつかのセキュリティ事項を考慮する必要があります(たとえば、特別なタイプのみを表示するように制限します)。STIを使用して、イベント、アクティビティ、およびサイトの3つのクラスをオブジェクト階層に移動することも検討できます。これにより、リクエストでタイプを渡す必要がなくなります。

于 2012-07-29T13:36:02.917 に答える