2015-04-02 109 views
0

在我的應用程序中,我有故事和庫。分解結構嵌套在故事中,並在故事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 %> 

這工作得很好,但我想通過以下參數第二循環內部鏈接到每個substory的編輯頁面:

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

我得到一個NameError in Stories#indexundefined local variable or method 'substory',然而這項工作罰款在子公司index.html.erb文件。

我也試過如下:

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

爲此,我得到一個NoMethodError in Stories#indexundefined 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
<% story.substories.each do |substory| %> 
    <%= substory.title %> 
    <%= substory.subplot %> 

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

<% end %> 

你剛剛犯了一個錯字。如果你在故事#索引中聲明它,@substory也會起作用

+0

謝謝!這解決了它。 – zazaalaza 2015-04-02 18:41:32

+0

我也試圖在第一個循環中加入:'<%= link_to'New substory',new_story_substory_path(substory.story,substory)%>'似乎無法使其工作。 – zazaalaza 2015-04-02 18:51:21

+0

這個問題將是第一個循環不知道什麼是substory,除非你在故事#索引中定義它,使它成爲'@ substory' 另外,在你當前的index.erb中,你並沒有真正使用你在substories_controller.rb中的大部分內容。 – pauloancheta 2015-04-02 18:55:09