vendredi 10 juin 2016

Passing in a param in a form_tag to switch a view

I have a Rails app with a time card functionality. In the timecard show view I have a search partial like this.

<%= form_tag timecard_path(params[:id]), :method => 'get' do %>
<strong>Week 1:</strong>
<%= text_field_tag "search[start_date_week_1]", params[:search].try(:[], :start_date_week_1), :placeholder => 'Start Date', required: true, :class => 'input-large search-query ', id: 'start_date_select_1' %>
        to
<%= text_field_tag "search[end_date_week_1]", params[:search].try(:[], :end_date_week_1), :placeholder => 'End Date', required: true, :class => 'input-large search-query', id: 'end_date_select_1'   %>

<strong>Week 2:</strong>
<%= text_field_tag "search[start_date_week_2]", params[:search].try(:[], :start_date_week_2), :placeholder => 'Start Date', required: true, :class => 'input-large search-query ', id: 'start_date_select_2' %>
        to
<%= text_field_tag "search[end_date_week_2]", params[:search].try(:[], :end_date_week_2), :placeholder => 'End Date', required: true, :class => 'input-large search-query', id: 'end_date_select_2'   %>
</br>
<%= select_tag "id", options_from_collection_for_select((User.employee.order("full_name ASC")), :id, :full_name) %>
<%= submit_tag "Search", :name => nil, :class => 'btn' %> <%= link_to "Print Timesheet", timecard_path(params.merge(format: "pdf")) , :class => 'btn btn-info' %><%#= link_to "Export To CSV", timecard_path(params.merge(format: "csv")), :class => "btn btn-info" %> <%= link_to "New Entry", new_timecard_path, class: "btn btn-medium btn-warning" %>
<% end %>
</br>
<hr>

This will give me a url of: http://ift.tt/1PQUHQA

What I'm wanting to do in this show view is using the select_tag somehow select the User and hit search again and have it pass the ID of the user back to the form_tag. So in essence if I have a user ID of 232 in the URL and I select John doe with a user id of 233 I'd want the ID passed into the controller to pull that user while still persisting the dates params.

This is what the show action in timecards controller looks like. I'm still working on it, so forgive the conditionals.

 def show
     @user = User.find(params[:id])
     if params[:search].present?
       @clock_events = @user.clock_events.completed.search(params[:search])
       @week_1 = @user.clock_events.payroll_week_1(Date.parse("#{params[:search][:start_date_week_1]}"), Date.parse("#{params[:search][:end_date_week_1]}")).completed
       @week_2 = @user.clock_events.payroll_week_1(Date.parse("#{params[:search][:start_date_week_2]}"), Date.parse("#{params[:search][:end_date_week_2]}")).completed
     else
       @clock_events = @user.clock_events.completed  
       @week_1 = @user.clock_events.payroll_week_1(Time.zone.now, Time.zone.now).completed
       @week_2 = @user.clock_events.payroll_week_1(Time.zone.now, Time.zone.now).completed
     end
      respond_to do |format|
        format.html do
          @clock_events = @clock_events.paginate(:per_page => params[:per_page] || 5, :page => params[:page]).order('clock_in DESC')
        end
        format.csv { send_data ClockEvent.timecard_to_csv(@week_1.order('clock_in desc'), @week_2.order('clock_in desc'), @user) }
        format.pdf do
          pdf = TimeCardEmployeePdf.new(@week_1, @week_2, @user)
          send_data pdf.render, filename: "timecard-#{@user.username}",
                                type: "application/pdf",
                                disposition: "inline"
         end
      end
  end

So basically after I've already visited the show page from the index page where I select a user it works, but within the show view I'd like to be able to switch to another user and pass the User id to the form_tag while persisting date params to quickly switch between User timecards without having to re-enter all those dates.

Any help with this is appreciated.

Aucun commentaire:

Enregistrer un commentaire