非CRUD Rails路由

时间:2015-02-08 15:32:42

标签: ruby-on-rails

我构建了以下控制器和路由。我期待表单发布到路径,例如/production/:date,但我得到了这个

http://localhost:3000/production/update_week?utf8=✓&date=2015-02-17&commit=Update

控制器

def index
    @date = session[:week] ||= Date.current
  end

  def show
    @date = params[:date]
    respond_to do |format|
      format.html  
    end
  end

  def update_week
    session[:week] = params[:date]
    respond_to do |format|
      format.html { redirect_to week_path(session[:week]) }
    end
  end

表格

%p.field
  = form_tag update_week_path, method: :get do
    = label_tag "Week of:"
    = text_field_tag :date, @date, id: 'datepicker'
    = submit_tag "Update"

路线

  get 'production/:date' => 'production#show', as: :week
  get 'production' => 'production#index', as: :production
  get 'production/update_week' => 'production#update_week', as: :update_week

1 个答案:

答案 0 :(得分:1)

在此行中,指定要使用的路径:

= form_tag update_week_path, method: :get do

在您的路线中,update_week_path就是这一行:

get 'production/update_week' => 'production#update_week', as: :update_week

如果您希望转到production/:date,则需要使用此路线:

get 'production/:date' => 'production#show', as: :week

像这样:

= form_tag week_path(your_date_goes_here), method: :get do