MAC
MAC

Reputation: 686

How to query data using rails?

I wanted to query data using between clause like this

select * from timerecords where empid=? and date between ? and ?

I just have to do it in rails. I have this code and I want the program to display in json format redirecting to localhost/timerecords.

  def showtimerecords
   empid=params[:empid]
   startdate=params[:date]
   enddate =params[:date]

   render json: @timerecords=Timerecords.where(empid: empid,
                                :date => startdate..enddate)
   #redirect_to @timerecords
  end

When I type http://localhost:3000/timerecords/showtimerecords/empid=2&startdate=2015-03-26&enddate=2015-03-27

It says,

(No route matches [GET] "/timerecords/showtimerecords/empid=2&startdate=2015-03-26&enddate=2015-03-27")

Here's my config/route.rb code

Rails.application.routes.draw do
 get 'timerecords/showtimerecords'
 get 'timerecords/index'
 get 'employees/index'
 get 'employees/show'
 get 'employees/new'
 get 'welcome/index'
 root :to => 'employees#new'
 resources :timerecords, :employees
 post 'employees/update_info'
 post 'timerecords/update_time'
end

Upvotes: 0

Views: 75

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You are typing it wrong, it should look like this, the parameters should start with a ?, try using the following link

http://localhost:3000/timerecords/showtimerecords?empid=2&startdate=2015-03-26&enddate=2015-03-27

Also, you are sending startdate and enddate parameters but you are accessing using date, modify your code as mentioned below

def showtimerecords
   empid=params[:empid]
   startdate=params[:startdate].to_date
   enddate =params[:enddate].to_date

   @timerecords = Timerecords.where(empid: empid,
                                :date => startdate..enddate) 

   render json: @timerecords
   redirect_to 'timerecords/index'
end

Upvotes: 3

Related Questions