2015-04-06 116 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