Saturday, January 5, 2008

A little bit of RAILS for Build Your Own Ruby on Rails Web Applications readres

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 functionality to Shovells'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: display the "edit story" link only if the current story being displayed is submitted by the current logged in user.)


Add this 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 %>
<span id="edit_story_link">
<%= link_to 'edit story', :controller => 'story',
:action => 'edit_story', :id => @story %>
</span>
<% end %>
<% end %>



the submitted_by class after adding this code looks like this



<p class="submitted_by">
Submitted by:
<span><%= link_to @story.user.login,
:controller => 'account',:action => 'show',
:id => @story.user.login %></span>

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



--Once the user clicks on the "edit story" link, take him to /views/story/edit_story.rhtml page prefilled 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| %>

<p>
Title of the story:<br />
<%= f.text_field :name %>
</p>
<p>
link:<br />
<%= f.text_field :link %>
</p>

<p>
Description:<br />
<%= f.text_area :description %>
</p>

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

<p>
<%= submit_tag %>
</p>
<% 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.'
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 me a comment.


Other Questions that got answered by this post:
1. How to update a record in the database using form_for in Ruby on RAILS ?
2. How to use form_for to update data in database in Ruby on RAILS?
3. What method to use in the controller to update a record in the database?
4. How to fetch all the values of tags associated with a story and assign those values to a text_field_tag?

If you downloaded the word doc and the code din't work from there, try removing the extra pre tags or try the code from this post.

2 comments:

Unknown said...

Awesome ! It works !
Thanks a lot. I had started pulling my last hair.

Rahul said...

Hi Christian, thanks a ton for leaving comment. It feels now that my effort didn't go waste :)