【发布时间】:2015-06-27 12:53:22
【问题描述】:
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标识字段。我在form_tag 的路径上遇到了困难,我需要一些帮助。
这是我的部分观点:
<%= 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 %>
从 rake 路线我得到以下输出:
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
这是我的控制器: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
所以我相信我在 form_tag 中正确调用了路径,但是当我加载页面时出现错误:No route matches {:action=>"show", :controller=>"timecards"} 即使 timecards_controller 中有显示操作。
我认为我必须为显式 url 设置 form_tag 并以某种方式在参数中传递用户的 :id。但我对如何做到这一点有点坚持。
所以总结一下。当我有下拉菜单时,我选择一个用户,单击“查看员工”,这应该转到 timecards_controller.rb 中的显示操作,URL 为 /timecards/3(作为示例)。我以前从未以这种方式使用过 form_tag,所以传递路径或显式 url 对我来说有点陌生。
【问题讨论】:
-
能否请您发布您的控制器的代码?
-
@ArslanAli 我已经更新了我的控制器代码,感谢您的关注!
标签: ruby-on-rails forms ruby-on-rails-3