如何创建一个提交两个日期并输出报告的表单?
我目前有一个可以生成报告的控制器。这是控制器:
def last5days
#@monday = (Time.now).at_beginning_of_week
@monday = (Time.now).months_ago(1)
#@friday = 5.days.since(@monday)-1.second
@friday = (Time.now)
@sent_emails = ContactEmail.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
@made_calls = ContactCall.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
@letters_sent = ContactLetter.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
@contacts_added = Contact.all(:conditions => ['date_entered >= ? and date_entered <= ?', @monday, @friday])
#@table = ContactEmail.report_table(:all,
# :conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
#@grouping = Grouping(@table, :by => "email_id")
end
我希望有一个界面,可以让用户选择开始日期和结束日期,点击提交,然后返回输出,而不是硬编码 monday
和 friday
。
我很难找到一个例子或确切地知道该怎么做。
我从一个新视图开始:
<%= render :partial => 'form' %>
<div id = 'display'>
</div>
我希望控制器的输出通过 Ajax 显示在“显示”中。
我尝试创建表单如下:
<% remote_form_for do |f| %>
<p>
<%= f.label :start_date %><br />
<%= f.date_select_tag :start_date %>
</p>
<p>
<%= f.label :end_date %><br />
<%= f.date_select_tag :end_date %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
然后我的计划是更改控制器以接受 params[:start_date]
和 params[:end_date]
但我不这样做我想我很清楚这些部分如何组合在一起才能完成这项工作。指导?帮助?
这是我现在正在尝试的:
查看:find_start_end.html.erg
<% form_tag('return_search') do %>
<p>
<label>Start Date</label> <%= date_select('start_date', 'params') %>|
<label>End Date</label> <%= date_select :end_date, params[:end_date] %>
</p>
<p><%= submit_tag "Get Stats" %></p>
<% end %>
控制器:
def return_search
@sent_emails = ContactEmail.all(:conditions => ['date_sent >= ? and date_sent <= ?', params[:start_date], params[:end_date]])
@made_calls = ContactCall.all(:conditions => ['date_sent >= ? and date_sent <= ?', params[:start_date], params[:end_date]])
@letters_sent = ContactLetter.all(:conditions => ['date_sent >= ? and date_sent <= ?', params[:start_date], params[:end_date]])
@contacts_added = Contact.all(:conditions => ['date_entered >= ? and date_entered <= ?', params[:start_date], params[:end_date]])
respond_to do |format|
format.html #view needs to be the same name as the method
end
def find_start_end
end
I currently have a controller which produces a report. This is the controller:
def last5days
#@monday = (Time.now).at_beginning_of_week
@monday = (Time.now).months_ago(1)
#@friday = 5.days.since(@monday)-1.second
@friday = (Time.now)
@sent_emails = ContactEmail.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
@made_calls = ContactCall.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
@letters_sent = ContactLetter.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
@contacts_added = Contact.all(:conditions => ['date_entered >= ? and date_entered <= ?', @monday, @friday])
#@table = ContactEmail.report_table(:all,
# :conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday])
#@grouping = Grouping(@table, :by => "email_id")
end
Instead of hardcoding monday
and friday
, I want to have an interface where someone can select the start and end dates, hit submit, and the output gets returned.
I'm having troubling finding an example or knowing exactly what to do.
I started with a new view:
<%= render :partial => 'form' %>
<div id = 'display'>
</div>
I want the output from the controller to be displayed in "display" via Ajax.
I tried to create the form as follows:
<% remote_form_for do |f| %>
<p>
<%= f.label :start_date %><br />
<%= f.date_select_tag :start_date %>
</p>
<p>
<%= f.label :end_date %><br />
<%= f.date_select_tag :end_date %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
And then my plan was to change the controller to accept params[:start_date]
and params[:end_date]
But I don't think I quite know how the pieces fit together to make this work. Guidance? Help?
Here is what I am trying now:
VIEW: find_start_end.html.erg
<% form_tag('return_search') do %>
<p>
<label>Start Date</label> <%= date_select('start_date', 'params') %>|
<label>End Date</label> <%= date_select :end_date, params[:end_date] %>
</p>
<p><%= submit_tag "Get Stats" %></p>
<% end %>
CONTROLLER:
def return_search
@sent_emails = ContactEmail.all(:conditions => ['date_sent >= ? and date_sent <= ?', params[:start_date], params[:end_date]])
@made_calls = ContactCall.all(:conditions => ['date_sent >= ? and date_sent <= ?', params[:start_date], params[:end_date]])
@letters_sent = ContactLetter.all(:conditions => ['date_sent >= ? and date_sent <= ?', params[:start_date], params[:end_date]])
@contacts_added = Contact.all(:conditions => ['date_entered >= ? and date_entered <= ?', params[:start_date], params[:end_date]])
respond_to do |format|
format.html #view needs to be the same name as the method
end
def find_start_end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 确保您首先获得基本的 HTML 版本
2)然后将其传输到Ajax。
这个流程让每个人都参与进来并保持逻辑井井有条。
您的所有逻辑都是正确的,因此我只会为您提供 HTML 版本的代码,一旦您获得了该代码,您就可以启动远程版本。
控制器:
查看
routes.rb,
您需要在顶部添加一行,如下所示:
基本上,它的作用是创建特定的路由。
现在您可以调用stuffs_path,它将转到索引操作。
如果您不使用 REST,则 search_stuff_path 可能是:controller => :你的控制器=> :行动=>你的行动。如果您使用 REST,则需要将其作为成员添加到配置中的 paths.rb 中。如果您需要更多信息,请告诉我。
顺便说一句 - 我用东西作为你的资源的通用术语。我认为你想要的是报告,因此控制器、路由和视图将分别是 reports_path 和 map.resources :reports 。
现在您已经有了 HTML 版本,远程操作就很容易了。
这些
在您看来:
是您需要更改以使其基于远程的部分。
1) Make sure you get a basic HTML version up first
2) Then transfer this to Ajax.
This flow gets everyone on board and keeps logic organized.
All of your logic is correct so I will just give you the code for the HTML version and once you get that up you can get the remote version going.
controller:
view
routes.rb
you need a line at the top like this:
Basically what this does is create specific routes.
Now you can call
stuffs_path
and it will go to the index action.If you are not using REST the search_stuff_path could be :controller => :your_controller => :action => your_action. IF you are using REST you need to add this as a member to your routes.rb in config. If you need more info let me know.
BTW - I used stuffs to be a generic term for your resource. I think what you want would be report so the controller, routes, and views would be reports_path and map.resources :reports respectively.
So now that you got the HTML version going it's easy to get remote going.
controller
In your view:
Those are the parts you need to change to make it remote based.