热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

访问关联集合中的下一个记录-Accessingthenextrecordintheassociatedcollection

IamnewtoRails,tryingtowriteasimpleflashcardprogramwhereauserhasadeckofvocabulary

I am new to Rails, trying to write a simple flashcard program where a user has a deck of vocabulary cards they are cycling through...

我是Rails的新手,我试着编写一个简单的抽认卡程序,在这个程序中,用户有一副他们正在循环使用的词汇卡……

The model is a very straightforward relationship between user and card where:

该模型是用户与card之间非常直接的关系,其中:

 User  has_many :cards
 Card  belongs_to :user

The basic gist is that a user views a card on the index page and clicks a button, "flipping" over to the show page, where the other side is displayed.

基本要点是,用户在索引页上查看卡片并单击一个按钮,“翻转”到显示页,显示另一侧。

Have created and seeded my AR database, currently rendering a functional version that accesses and "flips" card #1 in my deck, but am stuck up on accessing the second, third, fourth cards, etc.

已经创建和播种了我的AR数据库,目前正在呈现一个功能版本,可以访问和“翻转”我的牌堆中的第1张牌,但是在访问第2张、第3张、第4张牌时被卡住了,等等。

I have tried many different variations on AR queries in my Card controller to get the next cards in the deck but none have worked... here's what I've got in my card controller now:

我已经在我的卡控制器中尝试了许多不同的AR查询来获得牌组中的下一张牌,但是没有一张是有效的……这是我的卡控制器里的东西:

 def index
    @card = Card.all.next
    @cards = Card.all
  end
`

Here's my card model:

这是我的名片模型:

class Card 

Here's my rake routes:

这是我的耙路线:

  Prefix Verb   URI Pattern                    Controller#Action
     root GET    /                              cards#index
    cards GET    /cards(.:format)               cards#index
         POST    /cards(.:format)               cards#create
 new_card GET    /cards/new(.:format)           cards#new
edit_card GET    /cards/:id/edit(.:format)      cards#edit
     card GET    /cards/:id(.:format)           cards#show
        PATCH    /cards/:id(.:format)           cards#update
          PUT    /cards/:id(.:format)           cards#update
       DELETE    /cards/:id(.:format)           cards#destroy
          GET    /cards/:id(.:format)           cards#show
`

..... So, because of the above, the code below is of course not doing what i want it to do, but here's my view page as of now:

.....因此,由于上面的原因,下面的代码当然没有做我想做的事情,但是这是我现在的视图页面:



<%= button_tag(type: 'button') do %>

<%= @card.word_text %>
    <%= link_to 'Tap to flip card', card_path(@card) %>
    <%= content_tag(:strong,'Tap to flip the card') %>
<% end %>



<%= button_tag(type: 'button') do %>
    <%= link_to 'New Card', cards_path(@next) %>
    <%= content_tag(:strong,'New Card') %>
<% end %>

To be honest, I am very stuck on how to create a path from my index page (which is displaying card #1, or @card) BACK to a new index page where card #2, or @next, is displayed instead... any help would be much appreciated!

老实说,我非常纠结于如何创建一个路径,从我的索引页(显示的是card #1或@card)返回到一个新的索引页,其中显示的是card #2或@next……如有任何帮助,我们将不胜感激!

1 个解决方案

#1


2  

Get the @next card by doing the following

通过以下操作获取@next卡

@card = Card.find(params[:id])
@next = Card.where('id > ?', @card.id).first
@next = Card.first if @next.nil?

Remeber that when @card is the last card in your DB, you will want to handle that as well since @next would be nil in that case, which is the reason for the third line.

请记住,当@card是DB中的最后一张卡时,您也需要处理它,因为在这种情况下,@next将是nil,这就是第三行出现的原因。

Edit: To fix your specific code, you need to modify your next method in your model like this

编辑:要修改特定的代码,需要像这样修改模型中的下一个方法

def next  # This is a method on an INSTANCE of Card, not the Class
  next_card = Card.where('id > ?', self.id).first
  next_card = Card.first if next_card.blank?
  next_card
end

This method is then called on @card, not Card, so something like this

然后这个方法被调用到@card上,而不是Card,类似这样

<%= link_to 'New Card', card_path(@card.next) %>

推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 云原生应用最佳开发实践之十二原则(12factor)
    目录简介一、基准代码二、依赖三、配置四、后端配置五、构建、发布、运行六、进程七、端口绑定八、并发九、易处理十、开发与线上环境等价十一、日志十二、进程管理当 ... [详细]
  • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
  • POCOCLibraies属于功能广泛、轻量级别的开源框架库,它拥有媲美Boost库的功能以及较小的体积广泛应用在物联网平台、工业自动化等领域。POCOCLibrai ... [详细]
  • Non-ASCIIhelponitsownisOK: ... [详细]
author-avatar
你就夜沉思
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有