如何链接到循环内的嵌套路由路径? [英] how to link to a nested route path inside a loop?

查看:51
本文介绍了如何链接到循环内的嵌套路由路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有故事和子故事.子故事嵌套在故事内部和 storiesindex.html.erb 上.我在所有的故事中循环,在里面我在所有的子故事中循环.

In my application I have stories and substories. Substories are nested inside stories and on the storiesindex.html.erb. I'm looping trough all the stories, and inside I'm looping through all the substories.

代码如下:

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

  <%= link_to 'Show', story_path(story) %>
  <%= link_to 'Edit', edit_story_path(story) %>
  <%= link_to "Delete", story_path(story), method: :delete, data: { confirm: "Are you sure?" } %>

  <% story.substories.each do |substories| %>
    <%= substories.title %>
    <%= substories.subplot %>
  <% end %>

<% end %>

<%= link_to 'New Story', new_story_path %>

这很好用,但我想通过在第二个循环中传递以下参数来链接到每个子故事的编辑页面:

This works fine, but I want to link to the edit page of each substory by passing the following argument inside the second loop:

<%= link_to 'Edit', edit_story_substory_path(substory.story, substory) %>

我得到一个 NameError in Stories#index 未定义的局部变量或方法 'substory',但是这在 substories index.html 中工作正常.erb 文件.

I get a NameError in Stories#index undefined local variable or method 'substory', however this work fine in the substories index.html.erb file.

我还尝试了以下方法:

<%= link_to 'Edit', edit_story_substory_path(@substory.story, @substory) %>

为此我得到一个 NoMethodError in Stories#index undefined method 'story' for nil:NilClass

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

Here are my routes models and controllers:

#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 %>

你刚刚打错了字.如果您在 Stories#index

You just made a typo. @substory would work too if you declare it on your Stories#index

这篇关于如何链接到循环内的嵌套路由路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆