samedi 11 juin 2016

Passing in a param in a form_tag to switch a view

I have a Rails app with a time card functionality. The timecard controller has standard Rails CRUD actions such as index and show.

My main concern is with the show view. From the index view there is the following code to select the User then using JS shunt to the show view like so:

timecards/index.html.erb

<%= form_tag timecards_path, :method => :get, :id => 'timecard_employee' do %>
  <%= select_tag options_from_collection_for_select((User.employee.order("full_name ASC")), :id, :full_name),
    { :include_blank => true },
    { :class => 'select' } %>
  <%= submit_tag "View Employee", class: "btn btn-primary" %>
<% end %>

<script>
$("#timecard_employee").submit(function(e){
  var form = $(this);
  // redirect to timecards/:id
  window.location = form.attr('action') + '/' + form.find('select').val();
  e.preventDefault();
});
</script>

Once on the show view for a User's timecard I have a search partial that looks for timeclock entries based off of 4 params which are date fields week 1 and week 2 starting and ending dated. These are passed into the controller to instantiate the week_1 and week_2 timeclock totals

timecards/_search.html.erb (rendered in show.html.erb)

<%= 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>
<%= 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 %>

This will give me a url like this: http://loopify.xyz:9001/timecards/232?utf8=%E2%9C%93&search%5Bstart_date_week_1%5D=2016-05-08&search%5Bend_date_week_1%5D=2016-05-14&search%5Bstart_date_week_2%5D=2016-05-15&search%5Bend_date_week_2%5D=2016-05-21

This is the show action inside of the timecards_controller.rb

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

This functionality works where I'm able to select a user from the index page, shunt to the show view and display the timecard. Once in the show view I can select the four dates within the form_tag and it will generate my two weeks worth of timeclock entries.

What I would like to do is to be able to switch to a different user from within the show view/action while persisting the date params in the request. So for instance if I'm currently viewing User id 232, I'd like a dropdown within the form_tag that is a collection of users, passes the ID of the user back into the form_tag upon submit. In essence, persisting the params of the dates, but simply switching out the user ID to render a different user's timecard.

I'm having a disconnect on how to get to get the ID passed back into the controller from the form_tag.

Originally I had injected this line inside of the form tag:

<%= select_tag "id", options_from_collection_for_select((User.employee.order("full_name ASC")), :id, :full_name) %>

However upon submission it simply appends an id=201 (or whatever ID is being passed from the form) to the end of the URL. Which looks like this:

http://loopify.xyz:9001/timecards/232?utf8=%E2%9C%93&search%5Bstart_date_week_1%5D=2016-05-08&search%5Bend_date_week_1%5D=2016-05-14&search%5Bstart_date_week_2%5D=2016-05-15&search%5Bend_date_week_2%5D=2016-05-21&id=201

So it'd passing it as a param, but I need it to actually replace the id that follows the timecards/ url.

If my explanation, narrative, and question is not clear enough please let me know how I can improve this question.

Aucun commentaire:

Enregistrer un commentaire