Forum: Ruby on Rails RE: restricting list output (newbie)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
HOGANBP (Guest)
on 2005-11-27 19:53
(Received via mailing list)
Huh? :) Oh you're probably referring to scaffolding.

Well, you could just do it by hand.


Say your table looks like this:

Authors
==========
 id
 first_name
 last_name
 email


Controller:

  def list
     @authors = Author.find(:all)
     # or you might have paginate stuff there instead
  end

View:

Just write this code by hand:

<table id="authors_list">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <% for author in @authors %>
  <tr>
     <td><%=author.first_name %></td>
     <td><%=author.last_name %></td>
  </tr>
  <% end %>
</table>


Don't fear the HTML :)
patrick (Guest)
on 2005-11-27 21:42
(Received via mailing list)
i tried your solution, but it didn´t seem "clean" to me.

i thought of something like (referring to the example):
    @authors = Author.find('first_name','last_name')

of course, this code is not correct. but there should be an easier way
than
modifying the HTML by hand, right?

patrick
justin (Guest)
on 2005-11-27 22:30
(Received via mailing list)
patrick k wrote:
> i tried your solution, but it didn´t seem "clean" to me.
>
> i thought of something like (referring to the example):
>     @authors = Author.find('first_name','last_name')
>
> of course, this code is not correct. but there should be an easier way than
> modifying the HTML by hand, right?
>

No, generated HTML is just to get you started. You should expect to
replace it completely.

   Justin
james.adam (Guest)
on 2005-11-27 22:51
(Received via mailing list)
define a subset of Author.content_columns in your controller and then
use this in your scaffold loop instead of Author.content_columns
directly:

  desired_columns = ['first_name', 'last_name']
  @content_columns = Author.content_columns.select { |c|
desired_columns.include?(c.name) }


- james
patrick (Guest)
on 2005-11-27 23:07
(Received via mailing list)
nice. tried it and ... worked. thanks.

looks like this is a more appropriate way to handle the problem compared
with changing the HTML-code.

this should be part of the tuturial, wiki ... (maybe it already is, but
i
didn´t find it).
brucebalmer (Guest)
on 2005-11-28 02:29
(Received via mailing list)
Hi:

I am a newbie too so this answer may be junk.  However, here goes....
(all the techno geniuses please be nice, I am attempting to be helpful).

@authors = Author.find(:select => 'first_name, last_name,etc) etc.

However, the other answer you were given is perfectly "clean" in my
opinion and certainly gives more control.

bruce
This topic is locked and can not be replied to.