views:

166

answers:

2

Hi all,

I'm looking to implement preview functionality in my posts scaffold. All I need to do is allow a user to enter information in the new view (/posts/new) and then replace the submit button with a preview button.

Once the preview button is clicked, the user is routed to the preview page (probably /posts/new/preview). If the user wants to make a change they would click 'go back' or if they are happy with the post they can then submit the post.

I found this article (http://eyedeal.team88.org/node/105) but it seems dated. Any ideas on what the best approach for this would be?

Many thanks, Tony

A: 

The link you have posted is a way, but I prefer to save object and set a boolean flag, let's say public to false (:default => false defined in migration). Then what you basically do is actually create the post and redirect to show action, where you have edit button (render edit action), post button (custom action to set public flag to true) and cancel button (which actually deletes the post) and maybe continue later button, which keeps the post and redirects to any other page, so the user can come back later and finish editing it.

When you need to show all posts, define a named_scope :visible, :conditions => ['posts.public = ?', true] and call Post.visible instead of Post.all in index and similar actions. You could also define a default_scope with conditions ['posts.public = ?', false], but bare in mind that if you want to find posts that are not visible, you will have to use #without_scope.

This way is better than the one in your link, because user can always come back later and finish editing the post and the publish it. However you will store more objects in DB and have to deal with invisible posts (don't show them by default, etc.)

Tadas Tamosauskas
I hadn't thought about that and it is a good idea. However, in this particular case I would just like to be able to preview prior to saving to the database as user accounts are still an "if" idea. Right now it's looking kind of like craigslist in that you don't need to be registered to post. Thanks for the insight!
slythic
A: 

On submit from create page, in the new action, build the object but do not save it to the database. Then render the object in its show view with a flag set in the new action to display a submit button. In your show view, always have a form with all the attributes of the object to be saved to db in hidden input fields or in display:none's. When the flag is set, you show the submit button. On submit, you go to the new_to_db action which saves the object to the db.

Kalyan M