small medium large xlarge

05 Mar 2009, 18:49
mdc (2 posts)

Hello all,

I have been developing rails apps for awhile and I have done some refactoring of my old apps to comply with REST et al.

Why on earth did the team get rid of instance variables when render. Here’s what I mean (taken from the 3rd edition)

module StoreHelper
def page_title
@page_title || "Pragmatic Store"
#Now the view code simply calls the helper method.
<h3><%= page_title %></h3>

If I understand this correctly, @page_title is set by the controller.

But what happens if render :action is called for form validation? We want to use render so that we can take advantage of all of the rails validation error messages and form goodness.

With Rails 2, the controller instance variables are no longer set. I have a few solutions, but none of them are clean and involve hashes and I am DEFINITELY repeating myself.

Thanks in advance and you might want to add this in more than passing to the new book…

05 Mar 2009, 20:54
mdc (2 posts)

Ok, I think I have found a solution, but it seems silly to have to do. (Please enlighten me if you have some knowledge). The following change to the controller with produce the expected behavior (that I expect at least)…

The controller code will look like this now:

 def new
      @section_header = "Register"

  def create
    cookies.delete :auth_token
    # protects against session fixation attacks, wreaks havoc with 
    # request forgery protection.
    # uncomment at your own risk
    # reset_session
    @user =[:user])
    if @user.errors.empty?
      self.current_user = @user
      flash[:notice] = "Thanks for signing up!"
      #redirect_to :action => 'new'
      render :action => 'new'

Notice the new method being called from within create. It doesn’t smell right, but it works.

You must be logged in to comment