使用select,assign变量和检索记录 [英] using a select, assign variable and retrieve records
问题描述
我正在尝试我觉得很简单,但是我没有找到一个类似的解决方案,很容易适应我的问题。一个选择下拉列表的视图只包含几个月(1月至12月)。我希望用户能够选择一个月,并点击一个link_to按钮,并检索billDate字段包含相关月份的记录子集,我不在乎当天,记录将始终为当前年份
我使用了以下...
模型...
class Bentry< ActiveRecord :: Base
attr_accessible:billDate,:timeBilled,:expensesBilled,:client_id,:paid,:billTotal,:pmtTotal,,cPmtTotal,:tPmtTotal
belongs_to:client
...查看w / select ...
< p>
<%= form_tag(/ bentries#monthlysumm,方法:get)do%>
<%@months = [{month=> 1月,id=> 1},{month=> 二月,id=> 2},{month=> March,id=> 3},{month=> 四月,id=> 4},{month=> May,id=> 5},{month=> 六月,id=> 6},{month=> 7月,id=> 7},{month=> 8月,id=> 8},{month=> 九月,id=> 9},{月=> 十月,id=> 10},{month=> 11月,id=> 11},{month=> 十二月,id=> 12}]%>
<%= rpt_month = select_tag('months',options_for_select(@ months.collect {| p | [p ['month'],p ['id']]))%> ;
<%= link_to< button>获取报告< / button>。html_safe,bentries_path(:billDate => rpt_month)%>
<%end%>
。和控制器...
def monthlysumm
@bentries = Bentry.joins(:client).where billDate =?,params [:rpt_month])
end
之前,我不明白如何从select中获取值传递给GET调用。它似乎是传递了几个月的全部哈希和ID。其次,我正在控制器中正确查询?我只关心在billDate字段中的一个完整日期(NOT datetime)的月份,并检索所选月份的所有帐单。
我很绿色,当谈到RoR,所以如果你需要任何进一步的信息只是问,但具体...假设我没有任何东西! ; )
编辑* *
在Nitish的帮助下,我已经选择了修正,我现在将月份变量传递给我的控制器而且,还有来自Nitish的手,我已经尝试将编辑应用于另一个答案中引用的控制器,他引用仅基于日期字段的月份部分的查询。但是,我还没有收到任何记录,我知道我有一个包含我选择的月份的billDate条目。
编辑* *
新编辑的应用程序控制器...
#GET / bentries / january
def monthlysumm
@bentries = Bentry.by_month(params [:billDate])
end
这里是模型的添加...
def self.by_month(mon)
where( strftime('%m',billDate)+ 0 =?,mon)
end
这会生成一个这样的SQL语句...
SELECTbentries* FROMbentriesWHERE(strftime %m,billDate)+ 0 ='2')
如果我进入Firefox的SQLite Manager并输入(等效)SQL语句...
SELECT * FROM bentries WHERE(strftime('%m',billDate )+ 0 = 2)
它返回预期来自bentries表的3条记录,但是应用程序中的代码没有生成记录集。
任何人都可以看到我在控制器或模型中出错的地方? ?
视图中的Rails代码在呈现视图时执行。所以rpt_month包含select_tag返回的值(而不是用户选择的值)。要获取用户选择的值,您将不得不使用客户端脚本。
如果我正确地了解您的问题,则 不是真的需要。你可以这样做:
<%= form_tag(bentries_path,method:get)do%>
<%= select_tag(:billDate,options_for_select((1..12).map {| m | [Date :: MONTHNAMES [m],m]}))%>
<%= button_tag获取报告%>
<%end%>
在控制器中,您将不得不修改您的查询,仅考虑日期字段的月份部分。在接受的答案这个解释很好问题:
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?!?
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:
这篇关于使用select,assign变量和检索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!