【问题标题】:Routing with parameters not working on rails带有参数的路由在轨道上不起作用
【发布时间】:2016-10-09 09:45:42
【问题描述】:

我一直在尝试解决这个问题,但我变得绝望,因为我不明白为什么这不起作用。 无论我尝试什么,都没有与我的链接匹配的路线,我收到以下错误:

路由错误:未初始化的常量 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 请求,该请求应该调用 line_item controllercreate 操作,从而匹配路由 POST /line_items(.:format) line_items#create

感谢大家的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby routing


    【解决方案1】:

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

    line_item_controller.rb 应该是line_items_controller.rb

    【讨论】:

    • 哦,天哪,这确实有效。我已经尝试了几个小时,但无法弄清楚?我想当您仍然习惯于整个“约定优于配置”的事情时,就会发生这种情况。非常感谢您提供的简单解决方案:)。
    • 我的荣幸先生 :)
    猜你喜欢
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2016-05-23
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多