我继承了一个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>
答案 0 :(得分: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
额外奖励:我更正了一些代码语法,使其更轻松。