如何链接到循环内的嵌套路径路径?

在我的应用程序中,我有故事和分类。 替代品嵌套在故事和故事index.html.erb 。 我正在循环所有的故事,而我正在循环所有的变电站。

这是代码:

             

这工作正常,但我想通过在第二个循环中传递以下参数来链接到每个子目录的编辑页面:

  

NameError in Stories#index undefined local variable or method 'substory'得到一个NameError in Stories#index ,但是这在substories index.html.erb文件中工作正常。

我也尝试了以下内容:

  

NoMethodError in Stories#index获得NoMethodError in Stories#index undefined method 'story' for nil:NilClass

这是我的路线模型和控制器:

 #routes.rb resources :stories do resources :substories end #story.rb has_many :substories #substory.rb belongs_to :story 

stories_controller.erb

  before_action :set_story, only: [:show, :edit, :update, :destroy] def index @stories = Story.all end def show @substories = Substory.where(story_id: @story.id).order("created_at DESC") end def new @story = Story.new end def edit end def create @story = Story.new(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 

substories_controller.erb

 before_action :set_substory, only: [:show, :edit, :update, :destroy] before_action :set_story def index @substories = Substory.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 

我错过了什么?

 <% story.substories.each do |substory| %> <%= substory.title %> <%= substory.subplot %> <% if substory %> <%= link_to 'Edit', edit_story_substory_path(substory.story, substory) %> <% end %> <% end %> 

你刚刚写了一个错字。 @substory如果你在Stories#index上声明它也会起作用