2015-04-06 112 views
0

我想顯示我的索引頁上的所有內容,但是當我嘗試鏈接到new_story_substory_path時,我得到未定義的局部變量或方法substory。如何鏈接到父循環內的嵌套路由動作?

下面是代碼:

index.html.erb

<% @stories.each do |story| %> 
    <h3><p><%= story.title %></p></h3> 
    <p><%= story.plot %></p> 

    <% if story.user == current_user %> 
    <%= link_to 'Show XXX', story_path(story), class: "btn btn-success" %> 
    <%= link_to 'Edit', edit_story_path(story), class: "btn btn-success" %> 
    <%= link_to "Delete", story_path(story), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-success" %></br> 
    <% end %> 

    <% story.substories.each do |substory| %> 
    <h4><p><%= substory.title %></p></h3> 
    <p><%= substory.subplot %></p> 

    <% if story.user == current_user %> 
     <%= link_to 'Show', story_substory_path(substory.story, substory), class: "btn btn-default" %> 
     <%= link_to 'Edit', edit_story_substory_path(substory.story, substory), class: "btn btn-default" %> 
     <%= link_to "Delete", story_substory_path(substory.story, substory), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default" %> 
    <% end %> 
    <% end %> 
    <br> 

    <% if story.user == current_user %> 
    <br> 
    <br> 
    # this is where I'm getting the error: 
    <%= link_to 'New subplot', new_story_substory_path(substory.story, substory), class: "btn btn-warning" %> 
    # however if I move this up, inside the second loop, it works perfectly. 
    <br> 
    <% end %> 
<% end %> 

<br> 

<%= link_to 'New Story', new_story_path, class: "btn btn-danger" %> 

這裏是我的控制器,路線和模式:

class StoriesController < ApplicationController 
    before_action :set_story, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user!, except: [:index, :show] 

    def index 
    @stories = Story.all 
    end 

    def show 
    end 

    def new 
    @story = current_user.stories.build 
    end 

    def edit 
    end 

    def create 
    @story = current_user.stories.build(story_params) 

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

    def update 
    respond_to do |format| 
     if @story.update(story_params) 
     format.html { redirect_to root_path, notice: 'Story was successfully updated.' } 
     format.json { render :show, status: :ok, location: root_path } 
     else 
     format.html { render :edit } 
     format.json { render json: @story.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @story.destroy 
    respond_to do |format| 
     format.html { redirect_to stories_url, notice: 'Story was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def set_story 
     @story = Story.find(params[:id]) 
    end 

    def story_params 
     params.require(:story).permit(:title, :plot) 
    end 
end 

Rails.application.routes.draw do 

    devise_for :users 

    resources :stories do 
    resources :substories 
    end 

    root 'stories#index' 

end 

class Story < ActiveRecord::Base 
    has_many :substories, dependent: :destroy 
    belongs_to :user 
end 

class Substory < ActiveRecord::Base 
    belongs_to :story 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_many :stories, dependent: :destroy 
    has_many :substories, dependent: :destroy 
end 

我缺少什麼?

編輯:

下面是substories控制器:

class SubstoriesController < ApplicationController 
    before_action :set_substory, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 
    before_action :set_story 

    def index 
    @Substories = @story.substories.all 
    end 

    def show 
    end 

    def new 
    @substory = Substory.new 
    end 

    def edit 
    end 

    def create 
    @substory = Substory.new(substory_params) 
    @substory.user_id = current_user.id 
    @substory.story_id = @story.id 
    if 
     @substory.save 
     redirect_to @story 
    else 
     render 'new' 
    end 
    end 

    def update 
    respond_to do |format| 
     if @substory.update(substory_params) 
     format.html { redirect_to root_path, notice: 'Story was successfully updated.' } 
     format.json { render :show, status: :ok, location: root_path } 
     else 
     format.html { render :edit } 
     format.json { render json: @story.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @substory.destroy 
    redirect_to root_path 
    end 

    private 
    def set_story 
     @story = Story.find(params[:story_id]) 
    end 

    def set_substory 
     @substory = Substory.find(params[:id]) 
    end 

    def substory_params 
     params.require(:substory).permit(:title, :subplot) 
    end 
end 
+0

在產生錯誤的那一行:你正在調用'substory'變量,但它只存在於'story.substories.each'循環中;你可能打算使用'new_story_substory_path(story)'(通往目前被看到/編輯的故事的一個substory的創建頁面的路徑) – MrYoshiji

+0

你有獨立的控制器嗎? – BroiSatse

+0

[如何鏈接到循環內的嵌套路徑路徑?](http:// stackoverflow。com/questions/29419525 /如何鏈接到一個嵌套的路線路徑內的一個循環) –

回答

0

我從評論中得到了答案。

MrYoshi寫道:

在生產線生產的錯誤:您所呼叫的substory變量,但它僅在story.substories.each環現有的;你可能想用的new_story_substory_path(故事)(通往屬於故事substory創建頁面目前被視爲/編輯)

基本上我改了行形式:

<%= link_to 'New subplot', new_story_substory_path(substory.story, substory), class: "btn btn-warning" %> 

到:

<%= link_to 'New subplot', new_story_substory_path(story), class: "btn btn-warning" %> 

This Works!

0

我試圖把這個作爲一個評論,但我需要50點聲望這樣做。無論如何,這裏是我的解決方案:

問題是index.html來自故事控制器。此時,您實際上並未使用任何其他控制器。這是使用ROR的單頁應用程序的常見問題。事情確實如此混亂。

你說得對,你必須在第二個循環中移動你的錯誤。問題是它會顯示不止一次。那是因爲有不止一個故事。如果你想避免這種情況,我想說的解決方案就是創建一個故事#show show.html.erb。一旦你點擊故事,它會指示子故事,然後有一個情節。

+0

我試圖在我的索引頁上顯示所有內容,閱讀我的文章的第一行。感謝您試圖回答,我最終從評論中發現了它。 – zazaalaza