0

我继承了一个 Rails 2.2.2 项目,该项目有一个没有服务器端表单验证的表单,所以我正在尝试添加它。

有没有办法将 POST 变量从下面的 submit_request 方法传递回注册方法(也在下面)?

我要做的就是用输入的内容重新填充表单,以及表单验证错误消息:

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

然后,在表单视图中获取 POSTED 值并填充表单:

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

1 回答 1

1

我猜你只是想显示注册页面,所以这样做:

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

奖励:我更正了一些代码语法以使其更轻。

于 2012-12-26T16:16:26.863 回答