I have a Rails app where I have a partial that I want a dropdown in so when a user is selected it will do a get method to the urls /timecards/:user_id which will be the controller's show method passing in the User's id field. I'm having a hard time with my paths in the form_tag and could use some help.
Here is my partial in the view:
<%= form_tag timecard_path, :method => :get do %>
<%= select_tag options_from_collection_for_select(User.employee.order("username ASC"), :id, :username) %>
<%= submit_tag "View Employee", class: "btn btn-primary" %>
<% end %>
From rake routes I get the following output:
timecards GET /timecards(.:format) timecards#index
POST /timecards(.:format) timecards#create
new_timecard GET /timecards/new(.:format) timecards#new
edit_timecard GET /timecards/:id/edit(.:format) timecards#edit
timecard GET /timecards/:id(.:format) timecards#show
PUT /timecards/:id(.:format) timecards#update
DELETE /timecards/:id(.:format) timecards#destroy
Here is my controller: timecards_controller.rb
class TimecardsController < ApplicationController
before_filter :disallow_clients, :disallow_medics, :disallow_employee, :disallow_supervisor
def index
@clock_events = ClockEvent.includes(:user).search(params[:search])
respond_to do |format|
format.html do
@clock_events = @clock_events.paginate(:per_page => params[:per_page] || 20, :page => params[:page]).order('users.username asc').order('clock_in desc')
end
format.csv { send_data ClockEvent.to_csv(@clock_events.order('users.username asc').order('clock_in desc')) }
end
end
def new
@clock_event = ClockEvent.new
end
def create
parse_times!
@clock_event = ClockEvent.new(params[:clock_event])
if @clock_event.save
redirect_to timecard_path(@clock_event.user.id), notice: "Entry added for #{@clock_event.user.username}".html_safe
else
render :new, notice: "Time Card Entry failed to Save".html_safe
end
end
def show
@user = User.find(params[:id])
@clock_events = @user.clock_events.search(params[:search])
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.to_csv(@clock_events.order('clock_in desc')) }
format.pdf do
pdf = TimeCardPdf.new(@clock_events, @user)
send_data pdf.render, filename: "timecard-#{@user.username}",
type: "application/pdf",
disposition: "inline"
end
end
end
def edit
@user = User.find(params[:id])
@clock_events = @user.clock_events.search(params[:search]).order("clock_in ASC").paginate(:per_page => 10, :page => params[:page])
end
def update
parse_times!
@clock_event = ClockEvent.find(params[:clock_event][:id])
if @clock_event.update_attributes(params[:clock_event])
redirect_to edit_timecard_path(@clock_event.user.id), notice: "Updated Successfully".html_safe
else
redirect_to :back, notice: "Woops.".html_safe
end
end
private
def parse_times!
params[:clock_event].parse_time_select! :clock_in if params[:clock_event].has_key? 'clock_in(5i)'
params[:clock_event].parse_time_select! :clock_out if params[:clock_event].has_key? 'clock_out(5i)'
end
end
So I believe I'm calling the path properly in the form_tag
but when I load the page I get the error: No route matches {:action=>"show", :controller=>"timecards"}
Even though there is a show action in the timecards_controller.
I think something I have to set the form_tag
for an explicit url and somehow pass in the :id
of the user in the params. But I'm a bit stuck on how to do this.
So to summarize. When I have the dropdown, I select a user, click "View Employee", and that should go to the show action in timecards_controller.rb with the url of /timecards/3 (as an example). I've never used form_tag in this way before so passing a path or explicit url is a bit foreign to me.
Any help you can provide would be greatly appreciated. If you need further clarification or have questions, please ask.
Aucun commentaire:
Enregistrer un commentaire