rubyonrailsin

A Ruby and Rails talk

Wednesday, March 17, 2010


Re: [Rails] How do I determine the "source" action for conditionally rendering error actions and validation messages?

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

On 17 March 2010 15:41, Clay H. <ccheaton@gmail.com> wrote:
> I have a model called Stock.rb. I created an additional action, called
> dispense, in the controller so that I could render two different views
> based on the type of data entry being done. The "new" action is used
> for "adding stock" and the "dispense" action is used for "dispensing
> stock."
>
> They are very similar, but use different form partials to remove
> fields that could potentially cause confusion. Both actions use the
> "create" action when submitted.

Not answering your question directly, but you might considered an
alternative approach, which is to use the 'new' action for both adding
and dispensing, with a parameter in the url to indicate which is
required (or possibly you can already tell which action is required by
examining the existing parameters). You say the two actions are very
similar so this might make for less code and would solve the problem
you are having with the render I think.

Colin

>
> Stock.rb contains
> validates_presence_of :medicine_id, :route_id
>
> stocks_controller.rb contains
>
>  def create
>    @stock = Stock.new(params[:stock])
>    @routes = Route.find(:all, :capitalize, :order => :name)
>    @medicines = Medicine.find(:all, :capitalize, :order => :name)
>    if @stock.save
>      flash[:notice] = "Success | Sucesso | Éxito | Réussi"
>      redirect_to(:back)
>    else
>      flash[:error] = "You must select a medicine and a route."
>      redirect_to(:back)
>      #render :action => 'new'
>    end
>  end
>
> Each of the form partials contains
> <%= f.error_messages %>
> so that validation errors are shown at the top of the form if the user
> fails to select a medicine or a route.
>
> The problem I'm having is that the validation error messages are only
> shown when using render.
> render :action => 'new' shows the error messages, but if the user
> comes to this from the "dispense" action, they are returned to the
> form for the "new" action, which is used for a different purpose.
>
> I changed the code to redirect_to(:back), which returns the user to
> the proper form, but doesn't show the validation error message -- only
> the flash[:error] message that I inserted.
>
> How do I either:
> 1) Detect which action/view/template sent the user to the create
> action, so that I can conditionally return them to the proper action
> using render?
>
> or
>
> 2) Use redirect_to(:back) and still show the validation errors?
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive