Something like this would work
def preview
@post = Post.new(params[:post])
render :action => "new" unless @post.valid?
end
Havn't tested, but should do it.
I'm new here btw, nice to see an active group in my new location..
--Dan
liquid_rails wrote:
> Hi Nolan,
>
> The only problem with this is approach is that I want to validate the
> posting before previewing it. The only way that I know of to do this
> is by saving it first, and then rendering the new action to display
> the form along with the errors. However, as suggested above, I should
> be able to call @posting.validate and do the same thing without having
> to save the post first.
>
> Thanks for your help,
>
> Cheri
>
> On Jul 15, 11:30 am, Nolan Meyers <[EMAIL PROTECTED]> wrote:
>
>> Since you only want it to save after previewing, why not just add a
>> "preview" action with hidden form fields on the page? For example:
>>
>> class PostsController < ApplicationController
>> def new
>> @post = Post.new(params[:post])
>> end
>>
>> def preview
>> @post = Post.new(params[:post])
>> render :action => 'new' unless @post.valid?
>> end
>>
>> def create
>> @post = Post.new(params[:post])
>> if @post.save
>> end
>> end
>>
>> Add this to your routes.rb:
>> map.resources :posts, :new => {:preview => :post}
>>
>> Finally, on the preview page, add hidden form fields with the post
>> values so that they will get passed when the user confirms to save the
>> record. If they want to edit it, you can either have it submit back to
>> the new page or display a hidden form.
>>
>> The benefit of taking this route is that you're only saving the
>> records after confirmation. Also, if you implement an api later the
>> preview action can just be skipped.
>>
>> - Nolan
>>
>> On Jul 15, 2008, at 11:14 AM, liquid_rails wrote:
>>
>>
>>
>>
>>> Hi Patrick,
>>>
>>> I would prefer to have the preview on a separate page, so that if the
>>> object is validated, the user is sent to the preview page, and it it
>>> is not, the user is sent back to the form by using "render :action =>
>>> 'new'", which will display the form + errors. On the preview page,
>>> there would be a choice to continue and agree to terms and conditions
>>> or to edit the post. The easiest way I see to do this is to actually
>>> create the post, and then if the user does not press continue or agree
>>> to the terms and conditions, delete the posting from the database.
>>> However, this would require a lot of unnecessary calls to the database
>>> (if there is a way to store and pass a temporary object), and would
>>> also cause posting id numbers to not be consecutive (say, if a user
>>> does not commit to the post.) I'm tempted to go with this approach
>>> because it is the easiest, however the inefficiency of this approach
>>> scares me.
>>>
>>> Cheri
>>>
>>> On Jul 15, 10:55 am, Patrick Crowley <[EMAIL PROTECTED]> wrote:
>>>
>>>> I would recommend using the standard save method here.
>>>>
>>>> If your controller is RESTFUL, it might look like this:
>>>>
>>>> class PostsController < ApplicationController
>>>>
>>>> def new
>>>> @post = Post.new
>>>> end
>>>>
>>>> def create
>>>> @post = Post.new(params[:post])
>>>> if @post.save
>>>> flash[:notice] = 'Post was successfully created.'
>>>> redirect_to(@post)
>>>> else
>>>> render :action => "new"
>>>> end
>>>> end
>>>> end
>>>>
>>>> end
>>>>
>>>> Sometimes, I like to merge these two actions into one:
>>>>
>>>> class PostsController < ApplicationController
>>>>
>>>> def new
>>>> @post = Post.new(params[:post])
>>>> if request.post?
>>>> if @post.save
>>>> flash[:notice] = 'Post was successfully created.'
>>>> redirect_to(@post)
>>>> end
>>>> end
>>>> end
>>>>
>>>> end
>>>>
>>>> ....
>>>>
>>>> If you need to have a preview right there in the form, you could
>>>> include a partial that contains the markup needed to display a post,
>>>> and pass it your @post object.
>>>>
>>>> So, below your form, you would add this:
>>>>
>>>> <% if request.post && @post %>
>>>> <%= :partial => "preview", :object => @post %>
>>>> <% end %>
>>>>
>>>> Now, depending on your validation rules, displaying something that
>>>> isn't valid could be kinda hairy, but you could find ways to work
>>>> around that.
>>>>
>>>> -- Patrick
>>>>
> >
>
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---