using a select, assign variable and retrieve records

拈花ヽ惹草 提交于 2019-12-23 05:31:23

问题


I am attempting what I would think is simple, but I have had zero luck in finding a solution that is similar AND easily adaptable to my problem.

I have a view with a select dropdown which only contains months (January-December). I want the user to be able to choose a month and click a link_to button and retrieve a subset of records where the billDate field contains the month in question, I don't care about the day, and the records will always be the current year.

I have used the following...

model...

class Bentry < ActiveRecord::Base

attr_accessible :billDate, :timeBilled, :expenseBilled, :client_id, :paid, :billTotal, :pmtTotal, :cPmtTotal, :tPmtTotal 

belongs_to :client

... view w/ select...

<p>
<%= form_tag("/bentries#monthlysumm", method: "get") do %>
<% @months = [{"month" => "January", "id" => "1"}, {"month" => "February", "id" => "2"}, {"month" => "March", "id" => "3"}, {"month" => "April", "id" => "4"}, {"month" => "May", "id" => "5"}, {"month" => "June", "id" => "6"}, {"month" => "July", "id" => "7"}, {"month" => "August", "id" => "8"}, {"month" => "September", "id" => "9"}, {"month" => "October", "id" => "10"}, {"month" => "November", "id" => "11"}, {"month" => "December", "id" => "12"} ] %>

<%= rpt_month = select_tag( 'months', options_for_select(@months.collect {|p| [ p['month'], p['id'] ] }) ) %>

<%= link_to "<button>Get Report</button>".html_safe, bentries_path(:billDate => rpt_month) %>

<% end %>

... and, controller...

def monthlysumm
   @bentries = Bentry.joins(:client).where("billDate = ?", params[:rpt_month])
end

Having never done this before, I don't understand how to get the value from the select passed to the GET call. It appears to be passing the entire hash of months and ID's. Secondly, am I doing the query correctly in the controller? I only care to look at the month in the billDate field which is a complete date (NOT datetime), and retrieve all bills dated in the month selected.

I am very green when it comes to RoR, so if you need any further info just ask, but be specific... assume I knnow nothing! ; )

EDIT * * With aid from Nitish, I have gotten the select fixed and I am now passing the month variable to my controller. And, also with a hand from Nitish, I have tried to apply the edits to the controller referenced in the other answer he references about querying based only on the Month portion of a date field. BUT, I am still not returning any records AND I know I have entries with a "billDate" containing the month I selected.

EDIT * * NEW edited controller for the app...

# GET /bentries/january
def monthlysumm
   @bentries = Bentry.by_month(params[:billDate])    
end

AND here is the addition to the model...

def self.by_month(mon)
   where("strftime('%m', billDate) + 0 = ?", mon)
end

This generates a SQL statement like this...

SELECT "bentries".* FROM "bentries" WHERE (strftime('%m", billDate) + 0 = '2')

If I go into SQLite Manager in Firefox and type (the equivalent) SQL statement of...

SELECT * FROM bentries WHERE (strftime('%m', billDate) + 0 = 2)

It returns the expected 3 records from the "bentries" table, but yet the code in app generates NO resulting recordset.

Can anyone see where I have gone wrong in the controller or model?!?


回答1:


Rails code in the view is executed when the view is rendered. So rpt_month contains value returned by select_tag (not the value selected by user). To get the value selected by user, you will have to use client side scripting.

If I understand your problem correctly, link_to is not really required. You could just do this:

<%= form_tag(bentries_path, method: "get") do %>
  <%= select_tag(:billDate, options_for_select((1..12).map{|m| [Date::MONTHNAMES[m], m] }) ) %>
  <%= button_tag "Get Report" %>
<% end %>

In controller, you will have to modify your query to consider only month part of the date field. It is explained very well in the accepted answer this question:



来源:https://stackoverflow.com/questions/23158230/using-a-select-assign-variable-and-retrieve-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!