0

Hi I have searched many sites and videos on rails casts but cannot find an answer to this, I have a urls controller and url model. in the controller I have this

def search
@search_term = params[:q]
st = "%#{params[:q]}%"
@url = Url.where("email ILIKE ? or custref ILIKE ? or order_number ILIKE ? or url4 ILIKE ?         or trader_code ILIKE ? or trader_name ILIKE ?", st, st, st, st, st, st).order("date     DESC").page(params[:page]).per_page(25)
respond_to do |format|
  format.html
  format.json {render json: @urls}
end
end

And in the model is just this

class Url < ActiveRecord::Base

  attr_accessible :order_number, :url1, :url2, :url3, :url4, :url5, :email, :custref,         :date, :trader_code, :trader_name

def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
    Url.create! row.to_hash
end
end
belongs_to :user
end

I am then using a partial to render the form_tag for the search box.

<%= form_tag("/search", :method => "get") do %>

<%= label_tag(:q, "") %>
<%= text_field_tag(:q) %>
<%= submit_tag "Search" %>

<% end %>

my problem is that I have no idea how to validate this form_tag. I need to validate that there is a valid and full email address entered into the search and I need to validate that there is exactly 8 characters entered to search for the order_number. The rest of the validations I can figure out after but these two are the most important. I have watched videos on railscast for active_attr but it doesn't work for me. Has anybody done this before that would be able to help me out?

Thanks

user2791952
  • 67
  • 2
  • 9
  • [This post](http://stackoverflow.com/questions/5118203/how-do-i-validate-a-non-model-form-in-rails-3/5118393#5118393) looks like it has what you need. – CoolTapes Oct 22 '13 at 11:17
  • how and what do you want to validate? the input of the field? on the server? on the client? do you want to strip stuff out? – phoet Oct 22 '13 at 11:42
  • im searching two columns, email and order number so i want to make sure people type in a valid email address and the order number needs to be an exact number of characters, at the moment if you type in the first 4 characters of an order number then the results show every order number in the db – user2791952 Oct 22 '13 at 13:49
  • @user2791952 could you update your question to reflect this? You say you're searching two columns, but in your question you are searching six columns. From what I can tell the user does not specify the column to search? Is that correct? As in, you will have to determine what column they are searching by the type (email for email, integer for order num)? – Damien Roche Oct 22 '13 at 14:10
  • Question updated now, Yes I just have text telling the customer what they are able to search for above the search box. As far as the columns I have all of them just as strings as they can all be a mixture of numbers and text apart from the email being different – user2791952 Oct 22 '13 at 14:16

1 Answers1

0

Alternatively you can run your validation in search method of urls_controller

def search
  @search_term = params[:q]
  st = "%#{params[:q]}%"

  validate_search_query @search_term 

  @url = Url.where("email ILIKE ? or custref ILIKE ? or order_number ILIKE ? or url4 ILIKE ?         or trader_code ILIKE ? or trader_name ILIKE ?", st, st, st, st, st, st).order("date     DESC").page(params[:page]).per_page(25)
  # you can add if... then... block here to handle empty @url  
  respond_to do |format|
    format.html
    format.json {render json: @urls} # guess you should use @url here instead of @urls
  end
end

def validate_search_query (query)
  # validations for query
  ...
end
Leger
  • 1,184
  • 8
  • 7
  • Thanks Leger, do you have any idea what I would have to enter into those empty parts to validate the columns I spoke about in the comment above. Sorry if I sound stupid I'm just about getting used to using Rails – user2791952 Oct 22 '13 at 14:00