James Labocki
2006-Aug-18 11:59 UTC
[Rails] 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 %> <td><%=h customer.send(column.name) %></td> <% 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: <h1>New customer</h1> <%= 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 -- Posted via http://www.ruby-forum.com/.
Cayce Balara
2006-Aug-18 21:33 UTC
[Rails] Re: Understanding MVC - view customization after using scaff
> > <% for column in Customer.content_columns %> > <td><%=h customer.send(column.name) %></td> > <% 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: <td><%=h customer.name %></td> <td><%=h customer.city %></td> <td><%=h customer.state %></td> 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. :) -- Posted via http://www.ruby-forum.com/.
Cayce Balara
2006-Aug-18 21:35 UTC
[Rails] Re: Understanding MVC - view customization after using scaff
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. -- Posted via http://www.ruby-forum.com/.
Apparently Analagous Threads
- [PATCH 1/2] drm/nouveau: don't fini scheduler if not initialized
- [PATCH v2] drm/nouveau: Improve variable names in nouveau_sched_init()
- [PATCH] drm/nouveau: Improve variable names in nouveau_sched_init()
- Newbie question: scaffold not working
- back-porting 1.1.1 app to 1.0 rails server