Topic: categories

Hello,

I'm wondering how I can break this down so that I can format each category that gets listed, such as change the color or put an image next to it.

<% for category in @categories %>
    <%= link_to category.name, :action => "list", :category => "#{category.name}" %><br />
<% end %>

Thanks,

Dan

Re: categories

Well you should probably add fields to the database with the image filename and hex colour. Then you can just do:

<p style="color:#<%= category.color %>"><%= category.name %></p>

Same with images.

Last edited by shadow (2006-09-05 13:31:45)

Re: categories

interesting, I didn't think of using the database to do it.  I will try that!  thanks

Re: categories

Otherwise your just going to end up with a large template with lots of if tags.

Re: categories

I got the database all set up but there's a syntax error somewhere in that line you gave me and I don't know enough about putting css into ruby to know where it is.

compile error
/Users/djohnson/work/scuba/public/../config/../app/views/main_page/index.rhtml:16: parse error, unexpected tIDENTIFIER, expecting kDO or '{' or '('
_erbout.concat "    "; _erbout.concat(( link_to <p style="color:#<%= category.color ).to_s); _erbout.concat "\">"; _erbout.concat(( category.name ).to_s); _erbout.concat "</p>, :action => \"list\", :category => \"\#{category.name}\" %><br />\n"
                                                        ^
/Users/djohnson/work/scuba/public/../config/../app/views/main_page/index.rhtml:16: parse error, unexpected $undefined., expecting ')'
_erbout.concat "    "; _erbout.concat(( link_to <p style="color:#<%= category.color ).to_s); _erbout.concat "\">"; _erbout.concat(( category.name ).to_s); _erbout.concat "</p>, :action => \"list\", :category => \"\#{category.name}\" %><br />\n"
                                                                                                              ^
/Users/djohnson/work/scuba/public/../config/../app/views/main_page/index.rhtml:17: parse error, unexpected kEND, expecting ')'
end ; _erbout.concat "\n"
    ^
/Users/djohnson/work/scuba/public/../config/../app/views/main_page/index.rhtml:50: parse error, unexpected kEND, expecting ')'

thanks,

Dan

Re: categories

Could you post your code? I don't see an error in shadow's line.

Railscasts - Free Ruby on Rails Screencasts

Re: categories

Here is the code with the lines before and after it just for good measure.

<div id="left">
<% for category in @categories %>
    <%= link_to <p style="color:#<%= category.color %>"><%= category.name %></p>, :action => "list", :category => "#{category.name}" %><br />
<% end %>
<p class="list-actions"><%= link_to "Show all Sites", :action => "list" %></p>

Re: categories

Oh, you are missing quotes around the HTML inside the link_to tag, but that won't do what you want because you have ruby embedded in that. Try this instead:

<%= link_to category.name, { :action => "list", :category => "#{category.name}" }, :style => "color: \##{category.color}" %>

Railscasts - Free Ruby on Rails Screencasts