Ruby on rails 如何在RoR中验证用户输入?
我真的不知道验证用户输入的建议。我从罗开始。我读了很多关于这个问题的文章,但我从未得到我想要的。在RoR之前,我用Java编程。我的问题是:如何验证空字段并显示错误消息?以下是代码片段: 轮询控制器.rbRuby on rails 如何在RoR中验证用户输入?,ruby-on-rails,validation,Ruby On Rails,Validation,我真的不知道验证用户输入的建议。我从罗开始。我读了很多关于这个问题的文章,但我从未得到我想要的。在RoR之前,我用Java编程。我的问题是:如何验证空字段并显示错误消息?以下是代码片段: 轮询控制器.rb class PollsController < ApplicationController def create @poll = Polls.new @poll.question = params[:question] @poll.author_ip = request
class PollsController < ApplicationController
def create
@poll = Polls.new
@poll.question = params[:question]
@poll.author_ip = request.remote_ip
end
def show
end
def new
end
def edit
end
end
class Polls < ActiveRecord::Base
has_many :options
validates_presence_of :question, :message => 'Something is wrong...'
end
<p>
<% form_tag polls_path do %>
<%= label_tag :question, "Enter your question:" %><br>
<%=text_field_tag :question, params[:question] %>
<%=submit_tag "Send" %>
<% end %>
</p>
class PollsController
polls.rb
class PollsController < ApplicationController
def create
@poll = Polls.new
@poll.question = params[:question]
@poll.author_ip = request.remote_ip
end
def show
end
def new
end
def edit
end
end
class Polls < ActiveRecord::Base
has_many :options
validates_presence_of :question, :message => 'Something is wrong...'
end
<p>
<% form_tag polls_path do %>
<%= label_tag :question, "Enter your question:" %><br>
<%=text_field_tag :question, params[:question] %>
<%=submit_tag "Send" %>
<% end %>
</p>
类轮询“出现问题…”
结束
create.html.erb
class PollsController < ApplicationController
def create
@poll = Polls.new
@poll.question = params[:question]
@poll.author_ip = request.remote_ip
end
def show
end
def new
end
def edit
end
end
class Polls < ActiveRecord::Base
has_many :options
validates_presence_of :question, :message => 'Something is wrong...'
end
<p>
<% form_tag polls_path do %>
<%= label_tag :question, "Enter your question:" %><br>
<%=text_field_tag :question, params[:question] %>
<%=submit_tag "Send" %>
<% end %>
</p>
首先,不要在验证中添加无意义的消息,默认错误消息很好
第二,在controller中将代码更改为如下内容:
def new
@pool = Pool.new
end
def create
@pool = Pool.new(params[:pool])
if @pool.save
flash[:notice] = "Some text indicating it was created"
redirect_to pool_path(@pool)
else
flash[:error] = "Something is wrong while validating"
render :new
end
end
和视图:
这样,您就可以在模式中进行验证,而在控制器中,您只需要检查模型是否可以保存。如果没有,则可以在视图窗体中显示
用于在布局位置显示:
<% if flash[:notice] -%>
<p class="notice"><%= flash[:notice] %></p>
<% end -%>
<% if flash[:error] -%>
<p class="error"><%= flash[:error] %></p>
<% end -%>
包括
<% if flash[:notice] %>
<%= flash[:notice] %>
<% end %>
在网页的某个地方,但最好在app/views/layouts/application.html.erb
中
有关更多信息,请参阅RoR教程。首先,我想看看帖子。ActiveForm在这里可以起到很大的作用。
但是,如果您想自己进行此操作,您可以轻松地在控制器中添加一些验证,正如这里的海报在其更新版本的代码中所做的那样
def results
if params[:name] && !params[:name].blank?
@name = params[:name]
else
raise MyApp::MissingFieldError
end
if params[:age] && !params[:age].blank? && params[:age].numeric?
@age = params[:age].to_i
else
raise MyApp::MissingFieldError
end
rescue MyApp::MissingFieldError => err
flash[:error] = "Invalid form submission: #{err.clean_message}"
redirect_to :action => 'index'
end
然后只要在.erb中显示flash[:errors],如果它存在的话
我还想看看类似的东西。Doing Model.new(params[:Model])在Rails应用程序中是典型的(而且是正确的),但要小心大规模分配攻击。请将
attr\u accessor:question,:author\u ip
添加到池模型中以避免攻击。这样做可以删除flash的if语句。“#{name}”%>