Rails form_tag 显示动作

nul*_*tek 1 forms ruby-on-rails ruby-on-rails-3

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. 我在我的道路上遇到了困难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 %>
Run Code Online (Sandbox Code Playgroud)

从耙路线我得到以下输出:

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
Run Code Online (Sandbox Code Playgroud)

这是我的控制器: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
Run Code Online (Sandbox Code Playgroud)

所以我相信我正在正确地调用路径,form_tag但是当我加载页面时,我收到错误:No route matches {:action=>"show", :controller=>"timecards"}即使 timecards_controller 中有一个显示操作。

我认为我必须form_tag为显式网址设置一些内容并以某种方式传入:id在参数中用户的 。但我对如何做到这一点有些困惑。

所以总结一下。当我有下拉菜单时,我选择一个用户,单击“查看员工”,然后应该转到 timecards_controller.rb 中的显示操作,网址为 /timecards/3(作为示例)。我以前从未以这种方式使用过 form_tag,因此传递路径或显式 url 对我来说有点陌生。

max*_*max 6

一个简单的修补程序:

最简单的解决方法是将表单更改为一堆链接。

<%= User.employee.order("username ASC").each |u| %>
  <%= link_to u.username, timecard_path %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

否则,您可以使用 Javascript 简单地使表单重定向:

<%= form_tag timecodes_path, :method => :get, :id => 'timecode_employee' do %>
  <%= select_tag options_from_collection_for_select(User.employee.order("username ASC"), :id, :username) %>
  <%= submit_tag "View Employee", class: "btn btn-primary" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
$("#timecode_employee").submit(function(e){
  var form = $(this);
  // redirect to timecards/:id
  window.location = form.attr('action') + form.find('select').val();
  e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)

重新设计的技巧

通过添加底层 TimeCard 模型,可以从根本上改进您的设计。

这是一个非常常见的案例,可以告诉您原因:

客户决定他们希望经理每个月都在考勤卡上签字。

哦,天哪。现在我们需要获取ClockEvents该范围内的所有内容并在每个内容上更新“clock_events.state”。

但随后客户也想知道谁签了卡。所以你添加一个clock_events.signed_off_by_id并更新所有的时钟事件。然后他们希望三位经理签字,等等。

替代设计

请注意,这是一个固执的通用示例。

class ClockEvent < ActiveRecord::Base
  enum status: [:clocked_in, :clocked_out]
  has_many :users
  belongs_to :time_card
end

class TimeCard < ActiveRecord::Base
  belongs_to :user
  has_many :clock_events
  accepts_nested_attributes_for :clock_events
end

class User < ActiveRecord::Base
  has_many :time_cards
  has_many :clock_events, through: :time_cards
end
Run Code Online (Sandbox Code Playgroud)

TimeCard 可能会每月自动发放,或者如果您永远不想更改,而只是坚持每个用户一张 TimeCard。让我们在这里走一些传统路线:

resources :time_cards
end
resources :clock_events do
end
resources :users, shallow: true do
  resources :clock_events do
  end
  resources :time_cards do
  end
end
Run Code Online (Sandbox Code Playgroud)

现在想象一下我们有一个经典的打孔卡时钟。

我们会打卡:

POST /clock_events { user_id: 1, time_card_id: 5 }
Run Code Online (Sandbox Code Playgroud)

并打出:

PATCH /clock_events/1 { status: :clocked_out }
Run Code Online (Sandbox Code Playgroud)

这就是你的休息。

内斯托·普雷斯托

我们为每个用户的时间卡和 clock_events 嵌套了路由:

GET /users/1/time_cards
GET /users/1/clock_events
Run Code Online (Sandbox Code Playgroud)

从我们可以选择构建一个专用的,UserTimeCardController或者我们可以通过 TimeCardsController 中的用户 id 参数来确定范围。

class TimeCardsController
  def index
    @time_cards = TimeCard.all
    @time_cards = @time_cards.where(user: params[:user_id]) if params[:user_id]
    @users = scope.all
  end
end
Run Code Online (Sandbox Code Playgroud)

过滤

但是想象一下,如果我们希望经理能够过滤他在索引中看到的员工数量 - 一个好的架构应该是这样的:

class TimeCardsController
  def index
    @time_cards = TimeCard.all
    @time_cards = @time_cards.where(user: params[:user_id]) if params[:user_id] 
    if params[:filters]
       @time_cards = @time_cards.search(params[:search])
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在我们的索引页面上,我们将添加一个这样的表单:

<%= form_tag(time_cards_path, method: :get) %>
  <%= select_tag options_from_collection_for_select(User.employee.order("username ASC"), :id, :username), multiple: true %>
  <%= submit_tag "Filter", class: "btn btn-primary" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)