我想顯示我的索引頁上的所有內容,但是當我嘗試鏈接到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
在產生錯誤的那一行:你正在調用'substory'變量,但它只存在於'story.substories.each'循環中;你可能打算使用'new_story_substory_path(story)'(通往目前被看到/編輯的故事的一個substory的創建頁面的路徑) – MrYoshiji
你有獨立的控制器嗎? – BroiSatse
[如何鏈接到循環內的嵌套路徑路徑?](http:// stackoverflow。com/questions/29419525 /如何鏈接到一個嵌套的路線路徑內的一個循環) –