Slinky
Slinky

Reputation: 5832

Accessing POST params from different controller methods

I inherited a Rails 2.2.2 project that has a form with no server-side form validation so I am trying to add it.

Is there a way to pass the POST variables from the submit_request method below back to the signup method (also below)?

All I am trying to do is repopulate the form with whatever was entered, along with the form validation error message:

class LoginController < ApplicationController

 ## The controller that displays & processes the form



 # Form view
 def signup
   @hide_logout = "YES"
 end


 #Form validator/processor
 def submit_request
   @hide_logout = "YES"
   @name = (params[:name] ? params[:name] : "")
   @email = (params[:email] ? params[:email] : "")
   ...
   ## Validate posted values here

   ## Build error message, if needed
    if(error_str !="")
     flash[:warning] = error_str
     redirect_to :controller => "login/signup" and return
    end

  end
end

Then, in the form view grab the POSTED values and populate the form:

    <%= text_field_tag("name", (@name !=nil ? @name.to_s : nil), :size => 40) %><br><br>

Upvotes: 0

Views: 135

Answers (1)

apneadiving
apneadiving

Reputation: 115521

I guess you just want to display the signup page so do:

def submit_request
  @hide_logout = "YES" #why don't you use boolean here?
  @name  = params[:name]  || ""
  @email = params[:email] || ""
  ...
  ## Validate posted values here

  ## Build error message, if needed
  unless error_str.blank?
   flash[:warning] = error_str
   render :signup and return
  end
end

Bonus: I corrected some code syntax to make it lighter.

Upvotes: 1

Related Questions