路由参数不在rails上工作

时间:2016-10-09 09:45:43

标签: ruby-on-rails ruby routing

我一直试图弄清楚这一点,但我变得绝望,因为我不明白为什么这不起作用。 无论我尝试什么,没有路由匹配我的链接,我得到以下错误:

  

路由错误:未初始化的常量LineItemsController

我的链接如下:

<%= button_to 'Add to template', line_items_path(template_id: @template, position_id: position) %>

所以创建的链接是:

  

http://localhost:3000/line_items?position_id=2&template_id=1

routes.rb中:

Rails.application.routes.draw do
    resources :line_items
    resources :templates
    resources :positions

line_item_controller.rb

class LineItemsController < ApplicationController
  before_action :set_line_item, only: [:show, :edit, :update, :destroy]

  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all
  end

  # GET /line_items/1
  # GET /line_items/1.json
  def show
  end

  # GET /line_items/new
  def new
    @line_item = LineItem.new
  end

  # GET /line_items/1/edit
  def edit
  end

  # POST /line_items
  # POST /line_items.json
  def create
    position = Position.find(params[:position_id])
    template = Template.find(params[:template_id])
    @line_item = LineItem.new(position, template)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to template_url}
        format.js {@current_item = @line_item}
        format.json { render action: 'show',
          status: :created, location: @line_item }
      else
        format.html { render action: 'new' }
        format.json { render json: @line_item.errors,
          status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /line_items/1
  # PATCH/PUT /line_items/1.json
  def update
    respond_to do |format|
      if @line_item.update(line_item_params)
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /line_items/1
  # DELETE /line_items/1.json
  def destroy
    @line_item.destroy
    respond_to do |format|
      format.html { redirect_to line_items_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_line_item
      @line_item = LineItem.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
#    def line_item_params
#      params.require(:line_item).permit(:position_id, :template_id)
#    end
  #...
end

根据我的理解,我的链接应发送一个POST请求,该请求应调用create的{​​{1}}操作,从而匹配路由line_item controller

感谢帮助人员!

1 个答案:

答案 0 :(得分:0)

我认为问题是控制器的文件名:

line_item_controller.rb应为line_items_controller.rb