【发布时间】:2014-12-01 08:04:48
【问题描述】:
我有一些看似简单的事情需要帮助,目前正在使用 Rails 4 和 Ruby 2.1.0
我正在处理工资单索引页面,用户可以从页面上的 select_tag 中选择一周。我的工资单控制器中有一个@current_week 变量,我想将参数传递给它,但是使用form_tag 这样做时遇到了一些麻烦。到目前为止,这是我所得到的:
index.html.haml
%h1 Payroll Index
%h6= "Current Week: #{@current_week}"
.week_selector
=form_tag payroll_path, action: :update_week do
=select_tag :current_week, options_from_collection_for_select(@weeks, 'id', 'start_date')
=submit_tag
payroll_controller.rb
class PayrollController < ApplicationController
def index
@weeks = Week.all
@current_week = params[:current_week] || Week.this_week.display_date
end
def update_week
@current_week = params[:current_week]
end
end
routes.rb
Project::Application.routes.draw do
resources :payroll, only: [:index, :update] do
post '/:update_week' => 'payroll#index', as: :payroll
end
...
理想情况下,我希望用户能够提交表单,只需更新控制器中的@current_week 变量,仍然停留在索引页面上。肯定有一种我缺少的简单方法可以做到这一点吗?
我在路线和形式上尝试了几种不同的组合,但没有取得太大进展。任何帮助,将不胜感激。谢谢!
更新:
按照infused的建议,我把表格改成如下:
=form_tag payrolls_path, method: :get do
=select_tag :current_week, options_from_collection_for_select(@weeks, 'id', 'start_date')
=submit_tag
并收到以下错误:
undefined local variable or method `payrolls_path' for #<#<Class:0x00000101e045a0>:0x0000010c3af450>
根据要求,这是rake routes的相关输出:
Prefix Verb URI Pattern Controller#Action
payroll_payroll POST /payroll/:payroll_id/:update_week(.:format) payroll#index
payroll_index GET /payroll(.:format) payroll#index
payroll PATCH /payroll/:id(.:format) payroll#update
PUT /payroll/:id(.:format) payroll#update
employees GET /employees(.:format) employees#index
谢谢!
【问题讨论】:
标签: ruby forms ruby-on-rails-4 parameters