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

2023-12-20

在我的应用程序中,我有故事和子故事。子故事嵌套在故事内部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) %>

I get a NameError in Stories#index undefined local variable or method 'substory',但是这在子故事index.html.erb file.

我还尝试过以下操作:

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

故事控制器.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 上声明 @substory 也可以工作

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何链接到循环内的嵌套路由路径? 的相关文章

  • BigDecimal 无法强制转换为 BigDecimal

    这应该很简单 但它却爆炸了 有任何想法吗 d BigDecimal new 2 0 YAML load a gt d to yaml TypeError BigDecimal can t be coerced into BigDecimal
  • 如何在rails中使用npm包?

    我正在尝试使用王牌编辑 https github com ajaxorg ace在我的 Ruby on Rails 应用程序中 大部分视图由 React 组件组成 我正在使用反应轨道宝石 https github com reactjs r
  • ruby on Rails,会话过期通知

    我正在使用 ruby 1 9 3 和 Rails 3 2 我的实际会话处理如下所示 会话助手 def sign in user cookies remember token value user remember token expires
  • gem install rmagick 在 OS X El Capitan 上失败

    几天前我升级到 El Capitan 并运行了 brew update brew upgrade 它更新了 imagemagick 导致 ruby 的 rmagick gem 停止工作 我想没问题 我就跑 gem install rmagi
  • 由于符号链接错误,无法在 Mac OSX 10.8.1 中安装 ruby​​-1.9.2

    首先 我尝试了常见的rvm安装 rvm安装1 9 2 但是 显示了以下错误 The provided compiler usr bin gcc is LLVM based it is not yet fully supported by r
  • 通过使用 Minitest 的 Rails,如何设置 RuboCop 在每次使用 rake 运行测试时自动运行?

    当我运行以下命令时 我想要RuboCop https github com bbatsov rubocop在测试运行之前检查我指定的应用程序目录 bundle exec rake test 我添加了以下任务lib tasks test ra
  • 在 Rails 中禁用连接池以使用 PgBouncer

    我们有一个 Ruby on Rails 4 2 8 项目 可以访问大型 PostgreSQL 数据库 我们将使用 PgBouncer 添加一个新的连接池服务器 由于 PgBouncer 将处理数据库连接池 我们是否需要关闭 Rails 自动
  • Ruby on Rails 3 - 为每个请求重新加载 lib 目录

    我正在为 Rails 3 应用程序创建一个新引擎 正如您所猜测的 该引擎位于我的应用程序的 lib 目录中 但是 我在开发它时遇到了一些问题 事实上 每次更改引擎中的某些内容时 我都需要重新启动服务器 有办法避免这种情况吗 我可以强制rai
  • 如何编写一个在安装 RubyGem 时调用的钩子?

    我想编写一个 Ruby 片段 当我的 Gem 首次安装时运行 sudo gem install mygem 能做到吗 看起来并没有真正支持 我发现了一个 post install message 属性 您应该能够在 gem 规范中设置该属性
  • 为什么 rand() 总是返回相同的数字?

    我在用 兰特 200 在我的 Rails 应用程序中 当我在控制台中运行它时 它总是返回随机数 但如果我在应用程序行中使用它 index rand 200 索引总是相同的号码 为什么会这样以及如何克服这个问题 简单的伪随机数生成器实际上生成
  • RSpec 请求规范发布一个空数组

    我目前正在 Rails 中开发 API 端点 如果我需要的数据无效 我想确保端点响应具有正确的错误状态 我需要一个 id 数组 无效值之一是空数组 Valid vendor district ids 2 4 5 6 Invalid vend
  • ||= 是什么意思? [复制]

    这个问题在这里已经有答案了 我的应用程序控制器中有一个受保护的方法 def current user current user User find by id session user id end 我想知道什么 方法 我一直在努力寻找和找
  • 将 ruby​​ 类转换为模块比使用改进更好的方法?

    Module refine http ruby doc org core 2 0 0 Module html method i refine方法接受一个类和一个块并返回一个细化模块 所以我想我可以定义 class Class def inc
  • 自定义通用 Rails 错误消息

    我们的 Rails 应用程序被设计为链接到多个客户端数据库的单个代码库 根据子域 应用程序确定要连接到哪个数据库 我们使用液体模板为每个客户定制演示文稿 我们无法为每个客户定制通用的 我们很抱歉 出了点问题 消息 谁能推荐一种方法让我们能够
  • 如何在服务调用后检查 rspec 中的数组更改?

    目标很简单 例如我们有一个数组 name ghost state rejected name donkey state rejected 运行服务调用后UpdateAllUsers 这会将所有用户更改为 accepted name ghos
  • 通过 ESI:include 设置 Cookie,如何?

    我正在尝试使用 esi 在我的网站上创建忍者缓存 这个想法是 该网站大部分是静态的 我只需要在用户是否登录时做一些花哨的事情 所以我试图在页面A上放置一个 并在页面B的应用程序中设置触发器 这样我就可以将页面 A 缓存在 varnish 上
  • Rails 3.1+ 的 Jasmine 与 Mocha JavaScript 测试 [已关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我对茉莉花有经验并且非常喜欢它 有谁有 Jasmine 和 Mocha 的经验 特别是 Rails 的经验吗 我想知道是否值得转用 我已经在 J
  • 在 ActiveAdmin 或打印解决方案中动态更改分页

    我是 Activeadmin 和 Rails 的新手 我需要一些帮助 我有一个分页模型 我想允许用户更改分页值或完全禁用它 这样它就可以打印 到打印机 所有记录 或过滤后的记录 我知道我可以在 before filter 中使用 per p
  • Rails 4 的 mobile_fu

    我正在尝试将我的应用程序从 Rails 3 2 13 切换到 Rails 4 在此过程中 我遇到了一个主要障碍 我使用 gem mobile fu 来确定用户是否来自移动设备 该 gem 需要 Railties 3 2 13 但 Rails
  • 为什么 Rails 中的区域设置充当全局(使用 Thin 时)?

    我刚刚意识到在控制器中设置区域设置的推荐 Rails 方法 before filter set locale def set locale I18n locale params locale I18n default locale end

随机推荐

  • 如何使用log4j2删除旧日志

    仅供参考 我已经在网上搜索了很多文档 我使用的是storm 0 10 0 beta1 Storm中log4j2的配置文件是worker xml 现在 我尝试使用log4j2 我正在寻找删除旧日志的方法 但我找不到 部分配置如下
  • 在“for in”循环中访问迭代器

    根据我的理解 当运行如下代码时 for i in MyObject print i 我的对象 iter 函数运行 for 循环使用它返回的迭代器来运行循环 是否可以在循环中访问此迭代器对象 它是一个隐藏的局部变量 还是类似的东西 我想做以下
  • UITextView 加载时未滚动到顶部

    当我的文本未填充 UITextView 时 它会按预期滚动到顶部 当文本超出屏幕所能容纳的范围时 UITextView 会滚动到文本的中间 而不是顶部 以下是一些可能相关的详细信息 在 viewDidLoad 中在 UITextView 的
  • 在 Django 模板中显示反向多对多

    我正在为小型销售 CRM 应用程序创建警报 通知系统 我有一个 Lead Contact 模型 用于存储客户的姓名 地址等 以及一个 Contact Notifier 模型 用于跟踪首次联系客户的时间 最后一次联系以及我们何时进行下一步联系
  • Python Tkinter 根标题不起作用

    我似乎无法给我的窗口命名 他们都有标题 Tk 我相信我的代码是正确的 所以如果这是错误的 请纠正我 from Tkinter import root Tk root title Title root mainloop 标题仍然是Tk 我可以
  • 为什么VS代码中的这个问题匹配器不起作用?

    为什么我的 ProblemMatcher 不起作用 我对正则表达式非常确定 但它没有报告任何问题 即使标准输出上有一些问题 the matcher problemMatcher owner typescript fileLocation r
  • PAR::Packer 如何工作?

    我正在使用 PAR Packer 这个问题突然出现在我的脑海中 PAR Packer 在 Perl 中如何工作 真的吗compilePerl 脚本到 exe 就像 g 将 C 源代码编译到 exe 一样 还是像 Python 中的 py2e
  • 自首次启动以来的时间

    我正在开发一个 Android 应用程序 并遇到了确定系统首次启动时间的问题 我的意思是我需要测量多少从设备首次启动起已经过了时间 我知道有关侦听 ACTION BOOT COMPLETED 并将任何内容保存在 SharedPreferen
  • C# 与 C++ - 类型、继承和 vtable

    我无法理解导致 C 和 C 之间差异的原因 首先我们有一个例子 其中基类包含一个虚函数 class Base protected int super public virtual int f 0 class Derived public B
  • 我如何使用.NET 获悉我的客户端IP?

    我需要来自whatismyip com 的客户端IP 但我认为正则表达式模式不正确 你能帮我这个图案吗 您是否阅读了获取的 HTML 中的注释 请设置您的代码以进行抓取 你的IP来自 www whatismyip com automatio
  • Matplotlib 中 X 轴的换行符导致意外的窗口大小调整/跳跃/闪烁行为

    我正在 matplotlib 中的 x 轴上绘制日期和时间 因为我想绘制尽可能多的标签 所以我在 x 标签中使用换行符 如下所示 不幸的是 当我将鼠标悬停在图表上时 这会产生调整 matplotlib 窗口大小的副作用 因为它尝试在底部打印
  • Twitter Bootstrap 的 JQgrid 样式问题

    我使用 JQgrid 来显示信息并执行 CRUD 操作 我想要一个具有 Twitter Bootstrap 外观和感觉的页面 并且 JQGrid 显示一些数据 但是如果我导入 JQGrid 的 CSS 和 Bootstrap 的 CSS 则
  • 编译器未识别出歧义

    我不得不花一些时间来查找并修复我设法在以下代码中隔离的错误 include
  • 发送带有 Unicode 的 HTML 邮件

    我修改了 python 文档中的示例 以测试电子邮件模块中的 unicode usr bin env python coding utf 8 from future import absolute import division unico
  • MySQL“SET NAMES”靠近慢查询日志的顶部

    在最近启动的一个网站上 我注意到 在数百万个查询中 成本最高的请求实际上是 SET NAMES 其平均耗时超过 2 3 秒 而各种多连接并集的查询时间远远超过该网站上实际的繁重查询 查询时间远低于 2 秒 最后 这将它放置在慢查询日志的顶部
  • 如何收集类似于 panopticlick.eff.org 的信息 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 仅在 MSCHART 折线图的数据点上显示工具提示

    我在 C 4 0 中使用 mschart 生成折线图 并创建 DataPoint 来显示点上的工具提示 但问题是 工具提示出现在线条的每个点上 但是我只想在我的数据点上 您可能已经找到了问题的解决方案 因为这篇文章已经很旧了 但我想做同样的
  • 使用 MediaRecorder API 无法在录制的视频中进行搜索

    我正在尝试使用 MediaRecorder API 构建屏幕录制 作为暗示性媒体记录的方法 var chunks var recorder new MediaRecorder stream recorder streams stream r
  • 查找数组中最长的连续子序列

    我的任务是编写一个程序 找到给定数组中最长的递增连续子序列 并打印该子序列的长度及其本身 假设数组是 int arr 3 6 5 1 9 3 2 3 4 5 1 最长的连续递增子序列是2 3 4 5 长度为4 所以这个方法的输出是 4 2
  • 如何链接到循环内的嵌套路由路径?

    在我的应用程序中 我有故事和子故事 子故事嵌套在故事内部storiesindex html erb 我在所有故事中循环 在内部我在所有子故事中循环 这是代码