0

index.html.erb:

<%= form_tag(sheets_path, :method => "get", id: "search-form") do %>
        <%= date_field :search, params[:search], placeholder: "Procurar sheets" %>
        <%= submit_tag "Procurar", :name => nil,class: "button" %>
      <% end %>

sheets_controller.rb:

 def index
    @sheets = Sheet.where(user_id: current_user)
        if params[:search]
            @sheets = Sheet.search(params[:search], current_user.id)
        else
            @sheets = Sheet.where(user_id: current_user)
        end 
 end 

sheet.rb:

def self.search(search, userid)
  date_start = search
  date_end = '2017-07-27T00:00:00'#hardcoded, for now, implement date_start +24h later
  self.where("user_id =" + userid.to_s ).where("inicio > ?", date_start).where("inicio < ?", date_end) 
end

I make a first query let's say: 2017-07-03 it gives me the sheets where start_date is > than 2017-07-03 and displays it all ok, now let's say I query 2017-07-05 after, it gives me

  • TypeError in SheetsController#index can't quote Array

and the link: http://localhost:3000/sheets?utf8=%E2%9C%93&search%5B%5B%222017-07-03%22%5D%5D=2017-07-05

and console:

Processing by SheetsController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>{"\"2017-07-03\""=>"2017-07-05"}}
  User Load (1.0ms)  EXEC sp_executesql N'SELECT  [users].* FROM [users] WHERE [users].[id] = @0  ORDER BY [users].[id] ASC OFFSET 0 ROWS FETCH NEXT @1 ROWS ONLY', N'@0 int, @1 int', @0 = 7, @1 = 1  [["id", nil], ["LIMIT", nil]]
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.0ms)



TypeError (can't quote Array):

app/models/sheet.rb:7:in `search'

How can I fix this? what is wrong with this?

  • Why you put params[:search] inside your form ? This is what is causing the issue. You should invoke params inside your controller only (unless you need to retrieve some elements from address bar in the view, but it is bad practice) – Maxence Jul 27 '17 at 19:15
  • how do I fix it then? – lolando migando Jul 28 '17 at 10:32

1 Answers1

0

You can try replacing

<%= date_field :search, params[:search], placeholder: "Procurar sheets" %>

with

<%= date_field_tag(:search, Date.today)%>

It will show today's date as default date. I am not too sure about the placeholder though..

Maxence
  • 2,029
  • 4
  • 18
  • 37
  • but I want to search with the date inserted on the field – lolando migando Jul 28 '17 at 10:49
  • I don't use date fields, so I don't know them much, but basically it is a field to insert a date ... You mean you want a specific date in that field ? – Maxence Jul 28 '17 at 10:52
  • this worked, and can you tell me how to add +24h to the date? for the date_end – lolando migando Jul 28 '17 at 10:52
  • This is a date field. There is no notion of hours. You want to add an extra day to the default date ? – Maxence Jul 28 '17 at 10:53
  • exactly, so date start is 28/07/2017 00:00 and date finish is 29/07/2017 00:00, when you don't insert a notion of hours the default one is midnight of that day – lolando migando Jul 28 '17 at 10:55
  • In the date_field_tag is it not the starting date users have to mention ? In your controller, once you have starting date, you can retrieve records between this starting date and now ... Or maybe you have a second date field for the ending date ? – Maxence Jul 28 '17 at 10:57
  • I wanted records that have date_start on that date – lolando migando Jul 28 '17 at 11:00
  • Ok so you need only one field: the starting date. And then you retrieve all records rom this starting date until now... If your records are recorded with a date, you just have to look for records with the same date. If your records mentions hours:minutes:seconds with date, then just make a query on the date and disregard the hours. Then you have all records from the starting date.. – Maxence Jul 28 '17 at 11:02
  • date_start + 1 day – lolando migando Jul 28 '17 at 11:02
  • Yes if your user type JULY 10, but you want records from 11 JULY until now. Then do @start_date=params[:search].to_date+1.days and it will start the next day – Maxence Jul 28 '17 at 11:05
  • this thread explains how to add days to a date : https://stackoverflow.com/questions/4654457/how-to-add-10-days-to-current-time-in-rails – Maxence Jul 28 '17 at 11:05
  • no, if user inserts july 10 i want record frmo july 10 to july 11 – lolando migando Jul 28 '17 at 11:06
  • Well then in your controller just retrieve `start_date=params[:search].to_date` `end_date=params[:search].to_date+1.days` I am not great with playing with dates. You may find some useful threads about this issue – Maxence Jul 28 '17 at 11:08
  • date_end = search.to_date+1.days worked for me, thanks – lolando migando Jul 28 '17 at 11:11