これを理解しようとしていましたが、なぜこれが機能していないのかわからないため、私は絶望的になりました。 何であれ、私がしようと、何のルートが私のリンクをマッチングされていないと私は次のエラーを取得する:パラメータがレール上で動作していない場合のルーティング
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 controller
のcreate
アクションを呼び出す必要がありPOSTリクエストを送信する必要があります!
jeeezああ、でなければなりません。私は今何時間も試してきたが、それを理解することはできなかった?私はあなたがまだ全体の "設定よりも大事なこと"に慣れているときに起こると思います。簡単な解決策のための巨大な感謝:)。 – icemax2k
私の喜びです – Ursus