如何创建一个提交两个日期并输出报告的表单?

发布于 2024-09-13 19:50:04 字数 2974 浏览 4 评论 0原文

我目前有一个可以生成报告的控制器。这是控制器:

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

我希望有一个界面,可以让用户选择开始日期和结束日期,点击提交,然后返回输出,而不是硬编码 mondayfriday

我很难找到一个例子或确切地知道该怎么做。

我从一个新视图开始:

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2024-09-20 19:50:06

1) 确保您首先获得基本的 HTML 版本
2)然后将其传输到Ajax。

这个流程让每个人都参与进来并保持逻辑井井有条。

您的所有逻辑都是正确的,因此我只会为您提供 HTML 版本的代码,一旦您获得了该代码,您就可以启动远程版本。

控制器:

 def index
       @start_date = Date.strptime(params[:start_date],"%d/%m/%Y")
       @end_date = Date.strptime(params[:end_date],"%d/%m/%Y")
       @stuffs = Stuff.find(:all, :conditions => ["created_at > ? and updated_at < ?",@start_date, @end_date])
      responds_to do |format|
          format.html #view needs to be the same name as the method
      end 
 end 

查看

<% form_tag(stuffs_path, :method => :get) do %>
  <p>
    <label>Start Date</label> <%= datetime_select :start_date, params[:start_date] %> |
     <label>End Date</label> <%= datetime_select :end_date, params[:end_date] %>
  </p>
  <p><%= submit_tag "Get Stats", :disable_with => "Getting stats..." %></p>
<% end %>

routes.rb,

您需要在顶部添加一行,如下所示:

map.resources :stuffs

基本上,它的作用是创建特定的路由。
现在您可以调用stuffs_path,它将转到索引操作。

如果您不使用 REST,则 search_stuff_path 可能是:controller => :你的控制器=> :行动=>你的行动。如果您使用 REST,则需要将其作为成员添加到配置中的 paths.rb 中。如果您需要更多信息,请告诉我。

顺便说一句 - 我用东西作为你的资源的通用术语。我认为你想要的是报告,因此控制器、路由和视图将分别是 reports_path 和 map.resources :reports 。

现在您已经有了 HTML 版本,远程操作就很容易了。

这些

def index
    responds_to do |format|
        format.js render :partial => "remote_report"
        # you can use RJS or a partial. 
        # If it's one place you want to update just use a partial.
        # Create a file _remote_report.html.erb as a partial.
        # Put this in the views folder of course
    end 
end 

在您看来:

 <div id="div_id_to_update">
 <% remote_form_tag(stuffs_path, :method => :get, :update => "div_id_to_update") do %>
   <p>
     your form methods here
   </p>
   <p>your submit tag here</p>
 <% end %>
 </div>

是您需要更改以使其基于远程的部分。

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:

 def index
       @start_date = Date.strptime(params[:start_date],"%d/%m/%Y")
       @end_date = Date.strptime(params[:end_date],"%d/%m/%Y")
       @stuffs = Stuff.find(:all, :conditions => ["created_at > ? and updated_at < ?",@start_date, @end_date])
      responds_to do |format|
          format.html #view needs to be the same name as the method
      end 
 end 

view

<% form_tag(stuffs_path, :method => :get) do %>
  <p>
    <label>Start Date</label> <%= datetime_select :start_date, params[:start_date] %> |
     <label>End Date</label> <%= datetime_select :end_date, params[:end_date] %>
  </p>
  <p><%= submit_tag "Get Stats", :disable_with => "Getting stats..." %></p>
<% end %>

routes.rb

you need a line at the top like this:

map.resources :stuffs

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

def index
    responds_to do |format|
        format.js render :partial => "remote_report"
        # you can use RJS or a partial. 
        # If it's one place you want to update just use a partial.
        # Create a file _remote_report.html.erb as a partial.
        # Put this in the views folder of course
    end 
end 

In your view:

 <div id="div_id_to_update">
 <% remote_form_tag(stuffs_path, :method => :get, :update => "div_id_to_update") do %>
   <p>
     your form methods here
   </p>
   <p>your submit tag here</p>
 <% end %>
 </div>

Those are the parts you need to change to make it remote based.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文