Search code examples
ruby-on-railslink-to

Couldn't find Zone without an ID


just trying the link_to image_tag helper, and I can't get it to work.

Can someone please tell me whats wrong?

Controller

class PagesController < ApplicationController
  def home
    @categories = Category.all
    @zone = Zone.find(params[:id])
    @zones = Zone.all
    @photos = Photo.landing
  end

end

View

    <% @zones.limit(8).each do |zone| %>
        <%= link_to image_tag "category-box_#{zone.id}.jpg", zone(zone.id) %>
    <% end %>

Update

I found the Id problem came from the controller. Now the error is:

undefined method `symbolize_keys' for 1:Fixnum

Thanks!


Solution

  • You need to wrap your image_tag in brackets. Your code should look something like this:

    <% @zones.limit(8).each do |zone| %>
        <%= link_to image_tag("category-box_#{zone.id}.jpg"), spots_by_zone_path(zone.id) %>
    <% end %>
    

    OR

    You can simply use block form of link_to

    <% @zones.limit(8).each do |zone| %>
        <%= link_to spots_by_zone_path(zone.id) do %>
          <%= image_tag "category-box_#{zone.id}.jpg" %>
        <% end %>
    <% end %>