2012-11-12 121 views
2

我有一个HTML表单在我的Rails查看显示形式的错误

<form id="download_form" action="download"> 
    Username: <input type="text" name="username"> <br/> 
    <input type="submit" id="submit_download_form" value="Submit"> 
</form> 

在Rails控制器,如果username长度大于10,我想控制器返回到相同的页面,并请注意用户通过<p id="notice"><%= notice %></p>,长度不能超过10.我该怎么办?

顺便说一句,username而不是在任何模型中的属性。这只是这种形式的一个领域。

回答

1

您可以通过方法length获得username长度:

if params[:username].length > 10 
    @notice = "the length can't be more than 10" 
    render 'download' 
end 

然后在您的视图中,可以使用@notice

<p id="notice"><%= @notice %></p> 
相关问题