Topic: scaffold

Hello,

I'm trying to take control of the pages created by the scaffold.  I got the edit, new, show, list, etc... pages.  I'm working with the show page and need to take out this part

<% for column in Site.content_columns %>
<p>
  <b><%= column.human_name %>:</b> <%=h @site.send(column.name) %>
</p>
<% end %>

and replace it with something where I have control over each thing listed.  For instance you have a product and it has a name, price, description.  With the way it is now I can't change that order that they are shown in, or style them in anyway.  How can I change this code so I have control over each part?

Thanks,

Dan

Re: scaffold

You'll want to declare each attribute specifically rather than using an automated tool. So you'd have something like this:

<% for product in @products -%>
<tr>
  <td>Name:</td>
  <td><%=h product.name %></td>
</tr>
<tr>
  <td>Price:</td>
  <td><%= number_to_currency(product.price) %></td>
</tr>
<tr>
  <td>Description:</td>
  <td><%=h product.description %></td>
</tr>
<% end -%>

Most code examples are usually pulled out of the air and not tested. Use at your own risk!

Re: scaffold

Hi,

That's bascially what I tried, I'm still getting this error message.

undefined method `each' for #<Site:0x22424a0>
Extracted source (around line #3):

1: <div id="main">
2:
3: <% for site in @site %>
4:
5: <p>Name: <%= h(site.name) %></p>
6: <p>Location: <%= h(site.location) %></p>

the detail page

<% for site in @site %>

<p>Name: <%= h(site.name) %></p>
<p>Location: <%= h(site.location) %></p>
<p>Depth: <%= h(site.depth) %></p>
<p>Rating: <%= h(site.rating) %></p>
<p>Description: <%= h(site.description) %></p>

<% end %>

and the controller

def detail
    @site = Site.find(params[:id])
  end

any ideas? 

Thanks,

Dan

Re: scaffold

You can just remove the loop since there's only one "site" and nothing to loop through.

<p>Name: <%= h(@site.name) %></p>
<p>Location: <%= h(@site.location) %></p>
<p>Depth: <%= h(@site.depth) %></p>
<p>Rating: <%= h(@site.rating) %></p>
<p>Description: <%= h(@site.description) %></p>

Railscasts - Free Ruby on Rails Screencasts

Re: scaffold

I took out the loop and now I get this error,

 undefined local variable or method `site' for #<#<Class:0x223d70c>:0x223d6e4>

As I understood it this part in the controller set that up, but perhaps not.

 def detail
    @site = Site.find(params[:id])
  end

Thanks again,

Dan

Re: scaffold

Did you copy in ryan's view code? If not you'll be referencing the wrong 'site' variable.  You should be using @site.

Re: scaffold

my bad, I apologize I missed that part of it.  It makes sense now.