Wednesday, December 26, 2007

Ruby On RAILS, updating a record in database using form_for

This post will be of use to you only if you happen to be a newbie to Ruby and Ruby on Rails and currently working on the Shovell web application from the book "Build Your Own Ruby on Rails Web Applications" by Patrick Lenz.

This post will be helpful to you if you are trying to add a feature to the Shovell application where the users of the Shovell should be able to update/edit stories posted by them.

After completing the Shovell application as guided in the book "Build Your Own Ruby on Rails Web Applications" I wanted to add edit functionality to my stories of Shovell application and after a few hours of struggle (I am very new to Ruby and Ruby on RAILS RoR) I was successfully able to implement the edit functionally to Shovell's stories.

This is how I achieved it :)


--Display a "edit story" link next to the "Submitted by" link on the show story page.
(validation done: dispaly the "edit story" link only if the current story being displayed is submitted by the current logged in user.)

Add this portion of the code to your /views/story/show.rhtml file within the submitted_by class

<% if logged_in? %> <% if @story.user.login.to_s == @current_user.login.to_s %> <%= link_to 'edit story', :controller => 'story', :action => 'edit_story', :id => @story %> <% end %> <% end %>



The submitted_by class after adding this code looks like this

Submitted by: <%= link_to @story.user.login, :controller => 'account',:action => 'show', :id => @story.user.login %>
<% if logged_in? %> <% if @story.user.login.to_s == @current_user.login.to_s %> <%= link_to 'edit story', :controller => 'story', :action => 'edit_story', :id => @story %> <% end %> <% end %>



--Once the user clicks on the "edit story" link, take him to /views/story/edit_story.rhtml page pre-filled with all the story details and tags. Allow user to make changes and save the updates.

Create a new file named edit_story.rhtml in /views/story/ and add the following code to it.

<%= error_messages_for 'story' %>

<% form_for :story do |f| %>

Title of the story:
<%= f.text_field :name %>

link:
<%= f.text_field :link %>

Description:
<%= f.text_area :description %>

Tags:
<%= text_field_tag 'tags', @story.tag_list %>

<%= submit_tag %>

<% end %>

Add the following code to your story controller named story_controller.rb located in /controllers/story_controller.rb

def edit_story @story = Story.find(params[:id]) @story.user = @current_user if request.post? and @story.update_attributes(params[:story]) @story.tag_with params[:tags] if params[:tags] flash[:notice] = 'Story was successfully updated.' # redirect_to :action => 'edit_story' end end note: this controller part of the code should be added anywhere above the protected section.

All the code above looks self explanatory but in case you have a question please leave a comment.

Other questions that got answered by this post:
1.How to use form_for to update data in the the database in Ruby On RAILS. 2.How to use form_for to update a record in the database in ROR.
3. What method to use to update a records in the database using Ruby on RAILS.
(hint: @story.update_attributes(params[:story] )
4. How to fetch all the values of tags from a the current story and set it as value of the text_field_tag (hint: <%= text_field_tag 'tags', @story.tag_list %> )


0 comments: