How to validate user input in RoR?
Solution 1
First, don't add meaningless messages to validations, default error messages are good.
Second, change your code to something like this in 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
and view use form helper:
<% form_for @pool do |f| %>
<%= f.error_messages %>
<%= f.label :question, "Enter your question:" %><br>
<%= f.text_field :question %><br>
<%= submit_tag "Send" $>
<% end %>
This way you have validation in mode, and in controller you only need to check if model can be saved. It not, then in your view form can display error_messages for that model.
For displaying flash messages in layout place:
<% if flash[:notice] -%>
<p class="notice"><%= flash[:notice] %></p>
<% end -%>
<% if flash[:error] -%>
<p class="error"><%= flash[:error] %></p>
<% end -%>
Solution 2
I'd check out this post, for one thing. ActiveForm can help quite a bit here. However, if you want to roll this yourself you could easily add some validation in the controller as the poster here did in his updated version of the code.
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
Then you just need to display the flash[:errors] in your .erb if it exists.
I'd also have a look at something like this.
wokena
Updated on August 12, 2022Comments
-
wokena over 1 year
I really don't know advice with validating user input. I'm begining with RoR. I read many pages about this issues, but I never get, what I want. Before RoR, I programmed in Java. My problem is: How I can do validate empty field and show error messages? Here are code fragments:
polls_controller.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
polls.rb
class Polls < ActiveRecord::Base has_many :options validates_presence_of :question, :message => 'Something is wrong...' end
create.html.erb
<p> <% form_tag polls_path do %> <%= label_tag :question, "Enter your question:" %><br> <%=text_field_tag :question, params[:question] %> <%=submit_tag "Send" %> <% end %> </p>
-
hgmnz over 14 yearsDoing Model.new(params[:model]) is typical (and correct) in Rails apps, but be careful of mass assignment attacks. Please add
attr_accessor :question, :author_ip
to your Pool model to avoid the attack. -
Shane Bauer over 14 yearsYou could get rid of the if statement for flash by doing this. <%- flash.each do |name, msg| -%> <%= content_tag :div, msg, :class => "#{name}" %> <%- end -%>