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

时间:2015-04-02 18:03:42

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

在我的申请中,我有故事和分类。替代品嵌套在故事内和故事index.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#index undefined local variable or method 'substory',但这在子目录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

我错过了什么?

1 个答案:

答案 0 :(得分:1)

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

上声明它也会有效