2016-04-11 13 views
0

私はtodolistとtodoitemsのネストされたリソースを作成しようとしているMVCを持っています。私は「追加藤堂項目」のURLを見上げたときRuby on Railsの*#viewの引数エラー

<%= link_to 'Add Todo Item', new_todolist_todoitem_path(@todolist) %> <-- THIS IS THE CAUSE OF THE ERROR 

、:私のroutes.rbファイルでは、私は私のtodolists/show.html.erbファイルで

resources :todolists do 
    resources :todoitems 
end 

を行っている、私は追加藤堂アイテムのリンクを追加しましたURLは私にhttp://localhost:3000/todolists/2/todoitems/newを与えましたが、それは正しいです。私rake routes

私はこれを持っている:私は、全体のコードを実行すると

class TodoitemsController < ApplicationController 
    before_action :set_todoitem, only: [:show, :edit, :update, :destroy] 
    before_action :set_todolist 

    # GET /todoitems 
    # GET /todoitems.json 
    def index 
    # @todoitems = Todoitem.all 
    end 

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

    # GET /todoitems/new 
    def new 
    @todoitem = Todoitem.new 
    end 

    # GET /todoitems/1/edit 
    def edit 
    end 

    # POST /todoitems 
    # POST /todoitems.json 
    def create 
    @todoitem = Todoitem.new(todoitem_params) 

    respond_to do |format| 
     if @todoitem.save 
     format.html { redirect_to @todoitem, notice: 'Todoitem was successfully created.' } 
     format.json { render :show, status: :created, location: @todoitem } 
     else 
     format.html { render :new } 
     format.json { render json: @todoitem.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /todoitems/1 
    # PATCH/PUT /todoitems/1.json 
    def update 
    respond_to do |format| 
     if @todoitem.update(todoitem_params) 
     format.html { redirect_to @todoitem, notice: 'Todoitem was successfully updated.' } 
     format.json { render :show, status: :ok, location: @todoitem } 
     else 
     format.html { render :edit } 
     format.json { render json: @todoitem.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /todoitems/1 
    # DELETE /todoitems/1.json 
    def destroy 
    @todoitem.destroy 
    respond_to do |format| 
     format.html { redirect_to todoitems_url, notice: 'Todoitem was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

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

    def set_todolist 
     @todolist = Todolist.find(params[:todolist_id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def todoitem_params 
     params.require(:todoitem).permit(:due_date, :task_title, :description, :done, :todolist_id) 
    end 
end 

しかし、私はというエラーを取得:

todolist_todoitems GET /todolists/:todolist_id/todoitems(.:format)   todoitems#index 
        POST /todolists/:todolist_id/todoitems(.:format)   todoitems#create 
new_todolist_todoitem GET /todolists/:todolist_id/todoitems/new(.:format)  todoitems#new 
edit_todolist_todoitem GET /todolists/:todolist_id/todoitems/:id/edit(.:format) todoitems#edit 
todolist_todoitem GET /todolists/:todolist_id/todoitems/:id(.:format)  todoitems#show 
        PATCH /todolists/:todolist_id/todoitems/:id(.:format)  todoitems#update 
        PUT /todolists/:todolist_id/todoitems/:id(.:format)  todoitems#update 
        DELETE /todolists/:todolist_id/todoitems/:id(.:format)  todoitems#destroy 

そして、私のtodoitems_controller.rbファイル内

私はこれを持っています todoitems/_form.html.erbFirst argument in form cannot contain nil or be emptyのそれ。

<%= form_for(@todoitem) do |f| %> <-- ERROR IS HERE 
    <% if @todoitem.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@todoitem.errors.count, "error") %> prohibited this todoitem from being saved:</h2> 

    <ul> 

私はそれが動作しない理由を理解しようとしてきたと私は、このヘルプページが長いことを知っているが、この問題を解決する方法での任意の提案は大歓迎されます!

答えて

1
あなたの todolist todoitem以来、最初の引数が todolist内にネストされたとして渡す必要があり

<%= form_for([@todolist, @todoitem]) do |f| %>

+0

感謝を!しかし、このエラーはどうですか? 'todoitems/new.html.erb'の#<#:0x007f8e96f87448>の未定義のローカル変数またはメソッドtodoitems_path。エラーは次のコード行です。<%= link_to 'Back'、todoitems_path%> ' – ETUDESC

+0

元の回答があなたの質問に役立った場合は、それを受け入れてください:)。もしあなたがちょうど戻ろうとしているなら、なぜこれをしないのですか? '<%= link_to 'Back'、:back%>'? – jdgray