[Rails] Re: render behaviour in controller action
James Byrne wrote in post #968350:
...
> This portion
> has only two methods, new (with create of course) and show.
>
> The requirements are that only the root url and the show url are valid
> for this domain. Anything else returns to the new action. However, if
> the show url is given for the initial request, in other words the new
> action has not previously been invoked for this session, then that too
> must go to the new action. Otherwise, it displays the results of the
> generated request on the show page.
>
> What I tried to do was to check for the presence of a model in the show
> action:
>
> def show
> return new unless @model
> . . . blah blah
> end
I believe using a redirect is far better and simpler
(although it costs 2 http requests). The main advantage
I see is that than the user (a human or another computer)
will see the result under the correct URL.
So that is:
def show
unless @model
redirect_to :action => 'new' # untested
return # get out of here!
end
. . . blah blah
end
> This worked, up to a point. If the current request did not have a model
> instance then even with the show url provided on the address the new
> method was instead invoked. However, what I discovered is that in this
> event the show template is rendered from within the new method
> nonetheless. I tried explicitly setting render :action => :new inside
> the new method, and this worked for the cases redirected from show.
> However, this causes a multiple render error in other cases.
If I need to use render the form for another action or use a redirect,
I typically put a 'return' immediately after such an explicit render.
So maybe a fix for your code could be this. (but I do not prefer it,
since it will render the "new" page under the "show" URL).
def show
unless @model
new # to execute the data preparations there
render :action => 'new' # this will force using the new template
return # get out of here !
end
... # your business logic for the show
end
--
Posted via http://www.ruby-forum.com/.
--
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home