Understanding MVC - view customization after using scaffolds

All,

I have ordered AWDWR and am anxiously awaiting its arrival. In the
meantime I still am playing with ROR using radrails. I have a few
questions …

I create a table named customers with the following details:

Field Type Null Key Default Extra
id int(11) NO PRI auto_increment
customer_name varchar(50) NO
inbound_retention int(11) NO
outbound_retention int(11) NO
unix_admin_email varchar(50) NO
schedule_id int(11) NO
transfer_inbound_type varchar(20) NO
transfer_outbound_type varchar(20) NO
date_added timestamp YES CURRENT_TIMESTAMP

I then run the scaffold generation:

ediweb> ruby script/generate scaffold customer Customerinfo
exists app/controllers/
exists app/helpers/
exists app/views/customerinfo
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/customer.rb
create test/unit/customer_test.rb
create test/fixtures/customers.yml
identical app/views/customerinfo/_form.rhtml
create app/views/customerinfo/list.rhtml
create app/views/customerinfo/show.rhtml
create app/views/customerinfo/new.rhtml
create app/views/customerinfo/edit.rhtml
create app/controllers/customerinfo_controller.rb
create test/functional/customerinfo_controller_test.rb
create app/helpers/customerinfo_helper.rb
create app/views/layouts/customerinfo.rhtml
create public/stylesheets/scaffold.css

I understand this creates my model, controller, and view. My question
is if I want to only allow a user to see certain information in the list
and only be able to add new information to certain fields where do I
begin? I look at my list.rhtml for customerinfo and see the following
(after some other html):

<% for column in Customer.content_columns %>

<%=h customer.send(column.name) %>
<% end %>

I realize this is what prints out each line, but how do I begin
specifying only certain columns to be printed? I guess I don’t
understand the customer.send(column.name). If someone has a good
tutorial on customizing views when using scaffolds to build the initial
files please link me.

Also, under new.rhtml I see:

New customer

<%= start_form_tag :action => ‘create’ %>
<%= render :partial => ‘form’ %>
<%= submit_tag “Create” %>
<%= end_form_tag %>

<%= link_to ‘Back’, :action => ‘list’ %>

What would I edit here to only allow certain fields in the table to be
inserted? Once again, a tutorial of ROR view customization would be
greatly appreciated. I’m very new to ROR and that book is going to take
a few days to get here!

-James

<% for column in Customer.content_columns %>
<%=h customer.send(column.name) %>
<% end %>

The above is just a looped method of showing all the content columns
that exist in the Customer model. This code loops through the collection
returned by the “content_columns” method and for each one it uses the
“send” method to call the column method and return the attribute value
for that object.

Let’s assume Customer has these fields:

id < won’t show anyway, not a “content” column
name
address
city
state
zip
credit_limit

The loop above will cycle through these fields, from “name” to
“credit_limit” and for each one display the attribute value for the
current object.

If you only want some user to be able to see name, city and state. You
would just get rid of the loop, and make the scaffold code to look like:

<%=h customer.name %> <%=h customer.city %> <%=h customer.state %>

c.

What would I edit here to only allow certain fields in the table to be
inserted? Once again, a tutorial of ROR view customization would be
greatly appreciated. I’m very new to ROR and that book is going to take
a few days to get here!

The form itself is in the partial view, which is named “_form.rhtml”.
You would edit the form in that view to change what the user is allowed
to edit on the form.

Answers to all your questions start here:
http://api.rubyonrails.org/

For instance, click on that link, scroll down in the bottom-left section
and click on "render (ActionController::Base) and you will learn more
than you want to know about rendering views.

Remember that the Ruby upon which Rails is based is pure
object-oriented, so every time you see “.something”, it’s a method
that’s been defined to a class somewhere. For instance, scroll in that
same section and click on “content_columns (ActiveRecord::Base)” to find
out what that “Customer.content_columns” line is doing.

etc.

etc.

etc.

Good luck, be strong, hang with it. AWDWR will help immensely. As will
Rails Recipes. Only, next time get the PDF version and you won’t have to
wait. :slight_smile:

Incidentally, “send” is a method of Ruby’s base Object class. It just
calls the method identified by the symbol in the argument. In the Rails
case, it is calling the reader method for each attribute identified in
the “Customer.content_columns” hash.

Hi Cayce,

Cayce B. wrote:

The above is just a looped method …

Let’s assume Customer has these fields:

Nice post.

Best regards,
Bill