【问题标题】:how to link to a nested route path inside a loop?如何链接到循环内的嵌套路由路径?
【发布时间】:2015-04-02 18:03:42
【问题描述】:

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

代码如下:

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

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

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

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

我还尝试了以下方法:

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

为此我得到了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

我错过了什么?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:
    <% 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 中声明 @substory 也可以使用它

    【讨论】:

    • 谢谢!这样就解决了。
    • 我也在尝试在第一个循环中添加这个:&lt;%= link_to 'New substory', new_story_substory_path(substory.story, substory) %&gt; 似乎无法使其工作。
    • 问题将是第一个循环不知道子故事是什么,除非您在 Stories#index 中定义它,这将使其成为 @substory 此外,在您当前的 index.erb 中,您是没有真正利用 substories_controller.rb 中的大部分内容。
    • 如何在 Stories#index 中定义它?
    • ``` class StroriesController
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多