Topic: Editing the way fields are displayed in SHOW View!

Hey i'm a newbie teaching myself for a university project. I am building a very specific but basic content management system for a small company.

I am trying to edit the Show View of my News Items page. I would like to scrap the standard code:

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

Instead i would like to edit how the information is displayed in my own way which i am struggling to do.

As things stand with the current text i dont have a choice of the order of the fields in the view and each of them appears with its database field name before it.

How do i go about producing a view similar to a list view where i choose which fields i want, where i want them and in what order? My aim is to have a detailed news item page for each story. Each would show the headline, the content, topic heading and date written.

Thank you.

Pete

Re: Editing the way fields are displayed in SHOW View!

Try this:

<h1><%= @newsitem.headline %></h1>
<p><%= @newsitem.content %></p>
<p><%= @newsitem.topic %></p>
<p><%= @newsitem.created_at %></p>

This assumes there's "headline" and "content" columns in the newsitems table. You may need to customize it further to suite your needs.

Railscasts - Free Ruby on Rails Screencasts

Re: Editing the way fields are displayed in SHOW View!

Cool thank you.

I haven't actually managed to get this working yet: do i not have to do something before this at the start of the view that states which Newsitem id has been clicked on? Something like this,...

 <% @newsitems.each do |newsitem| %>

...that appears on the list view but instead specifies which id has been selected; i think this is the main bit i am struggling with.

Re: Editing the way fields are displayed in SHOW View!

Think of the list page as displaying multiple newsitems. If you look in the list action of the controller you should see a find(:all) call. This is selecting multiple newsitems which you then loop through and display on the list page.

On the show page, however, you are only displaying one newsitem. If you look in the controller again at the show action, you should see find(params[:id]). This takes the id out of the URL and finds the single newsitems with that id. So, the code I gave above should be everything you need, is it not working? Maybe just try displaying one thing, like the headline, first, then add new columns one at a time to make sure they aren't causing a problem.

Railscasts - Free Ruby on Rails Screencasts

Re: Editing the way fields are displayed in SHOW View!

Ah yes sorry forgot to change a bit of code. Thats brilliant thank you very much!