2016-10-09 24 views
0

これを理解しようとしていましたが、なぜこれが機能していないのかわからないため、私は絶望的になりました。 何であれ、私がしようと、何のルートが私のリンクをマッチングされていないと私は次のエラーを取得する:パラメータがレール上で動作していない場合のルーティング

Routing Error: uninitialized constant 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_items(.:format) line_items#create

感謝のマッチング、line_item controllercreateアクションを呼び出す必要がありPOSTリクエストを送信する必要があります!

答えて

0

私は問題があなたのコントローラのファイル名だと思う:

line_item_controller.rbは、実際に動作することをline_items_controller.rb

+0

jeeezああ、でなければなりません。私は今何時間も試してきたが、それを理解することはできなかった?私はあなたがまだ全体の "設定よりも大事なこと"に慣れているときに起こると思います。簡単な解決策のための巨大な感謝:)。 – icemax2k

+0

私の喜びです – Ursus

関連する問題