Ruby on Rails
TutorialHelperMethods

« TutorialScaffolding | Tutorial | TutorialFramingOut »

In TutorialScaffolding, we saw how scaffolding can help us setup default actions as we develop our app, and we have overridden one of those defaults, show, which displays details about one person. Our show method doesn’t link back to the list action, or allow us to delete the record, which makes an unfriendly page. Let’s fix that.

Add this to the show.rhtml file:

Error in previous page: The show.rhtml does not exist. In previous versions of the Scaffolding tutorial it tells you to change the name of the view.rhtml file to show.rhtml and to change the controller accordingly, but with a parameter, like this:


def show
@person=Person.find(params“id”)
end

And create show.rhtml as a copy of view.rhtml

<a href="/friends/list">List</a>
<a href="/friends/destroy/<%=@person.id%>">Delete</a>

Hopefully by now you have an understanding of how rails rewrites URLs, but there is an easier way: use the URL Helper .

You can replace those two lines with this:

<%=link_to "List",  :action => "list" %>
<%=link_to "Delete", :action => "destroy", :id => @person.id %>

and voila! Your links are created for you.

Note: the above code probably won’t work now because the scaffold requires the POST method. If you find that the deletes don’t happen, and you have a message like Filter chain halted as [#<Proc:0×406aabc0@/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/actioncontroller/verification.rb:64>] returned false_ in your log file, try changing the Delete line to this:

<%= link_to "Delete", { :action => "destroy", :id => @person.id} ,
                      :confirm => "Are you sure?", :method => :post %>

If the link needs to specify a different controller, you can specify that as well, using :controller => “name”. Other options are also available. There are a number of other useful helpers, which you can read about in the documentation.

Some of those predefined helper methods help with forms. We need to build our own form to enter friend data, so let’s take a look at them. Start a new file in your editor, called edit.rhtml, in the views/friends directory. Put this in there:

<% form_tag( :action => "update", :id => @person.id ) do -%>
<%= hidden_field "person", "id" %>
Name:
<%= text_field "person", "name", "size" => 20 %><br/>

Street:<br />
<%= text_field "person", "street1", "maxlength" => 20 %><br/>
<%= text_field "person", "street2", "maxlength" => 20 %><br/>
	
City:
<%= text_field "person", "city", "maxlength" => 20 %><br/>

State:
<%= text_field "person", "state", "maxlength" => 2 %><br/>

Zip:
<%= text_field "person", "zip", "maxlength" => 20 %><br/>

<%= submit_tag "Save" %>
<% end -%>

Note how the form_tag and its corresponding end tags

We now override the default scaffolding edit method in friends_controller.rb – this isn’t necessary, but we will use this later in the tutorial:

def edit
  @person = Person.find(@params["id"])
end

Whaddaya know, you can edit the data!

Now we have a basic editing page! Let’s fill out the rest of the scaffolding methods in TutorialFramingOut.

« TutorialScaffolding | Tutorial | TutorialFramingOut »

category:Tutorial