ruby on rails
If you are working with a Ruby on Rails project, your requirements may include some interactivity with the HTML generated by your view templates. If so, you have a few choices for how to implement this interactivity.
如果您正在使用Ruby on Rails项目,那么您的需求可能包括与视图模板生成HTML的交互性。 如果是这样,您可以选择几种方式来实现这种交互性。
For example, you could implement a JavaScript framework like React or Ember. If your requirements include handling state on the client side, or if you are concerned about performance issues associated with frequent queries to the server, then choosing one of these frameworks may make sense. Many Single Page Applications (SPAs) take this approach.
例如,您可以实现一个JavaScript框架,例如React或Ember 。 如果您的要求包括在客户端上处理状态,或者您担心与服务器频繁查询相关的性能问题,那么选择这些框架之一可能是有意义的。 许多单页应用程序(SPA)都采用这种方法。
However, there are several considerations to keep in mind when implementing a framework that manages state and frequent updates on the client side:
但是,在实现用于管理客户端状态和频繁更新的框架时,需要牢记一些注意事项:
As an alternative, the team at Basecamp (the same team that wrote Rails) has created Stimulus.js, which they describe as “a modest JavaScript framework for the HTML you already have.” Stimulus is meant to enhance a modern Rails application by working with server-side generated HTML. State lives in the Document Object Model (DOM), and the framework offers standard ways of interacting with elements and events in the DOM. It works side by side with Turbolinks (included in Rails 5+ by default) to improve performance and load times with code that is limited and scoped to a clearly defined purpose.
作为替代方案, Basecamp的团队(与编写Rails的团队相同)创建了Stimulus.js ,他们将其描述为“用于现有HTML的适度JavaScript框架”。 Stimulus旨在通过使用服务器端生成HTML来增强现代的Rails应用程序。 状态存在于文档对象模型(DOM)中 ,并且该框架提供了与DOM中的元素和事件进行交互的标准方式。 它与Turbolinks (默认情况下包含在Rails 5+中)并肩工作,以限制代码的范围和明确定义的目的,从而提高性能和加载时间。
In this tutorial, you will install and use Stimulus to build on an existing Rails application that offers readers information about sharks. The application already has a model for handling shark data, but you will add a nested resource for posts about individual sharks, allowing users to build out a body of thoughts and opinions about sharks. This piece runs roughly parallel to How To Create Nested Resources for a Ruby on Rails Application, except that we will be using JavaScript to manipulate the position and appearance of posts on the page. We will also take a slightly different approach to building out the post model itself.
在本教程中,您将安装并使用Stimulus在现有的Rails应用程序上构建,该应用程序为读者提供有关鲨鱼的信息。 该应用程序已经有一个用于处理鲨鱼数据的模型,但是您将为有关单个鲨鱼的帖子添加一个嵌套资源,从而允许用户建立有关鲨鱼的思想和观点。 这段代码与如何为Ruby on Rails应用程序创建嵌套资源大致平行,除了我们将使用JavaScript来操纵页面上帖子的位置和外观外。 我们还将采用略有不同的方法来构建邮政模型本身。
To follow this tutorial, you will need:
要遵循本教程,您将需要:
A local machine or development server running Ubuntu 18.04. Your development machine should have a non-root user with administrative privileges and a firewall configured with ufw
. For instructions on how to set this up, see our Initial Server Setup with Ubuntu 18.04 tutorial.
运行Ubuntu 18.04的本地计算机或开发服务器。 您的开发机器应具有具有管理特权的非root用户,以及使用ufw
配置的防火墙。 有关如何进行此设置的说明,请参阅我们的《 Ubuntu 18.04初始服务器设置》教程。
Node.js and npm installed on your local machine or development server. This tutorial uses Node.js version 10.16.3 and npm version 6.9.0. For guidance on installing Node.js and npm on Ubuntu 18.04, follow the instructions in the “Installing Using a PPA” section of How To Install Node.js on Ubuntu 18.04.
安装在本地计算机或开发服务器上的Node.js和npm 。 本教程使用Node.js 10.16.3版和npm 6.9.0版。 有关在Ubuntu 18.04上安装Node.js和npm的指导,请遵循如何在Ubuntu 18.04上安装Node.js的“使用PPA安装”部分中的说明。
Ruby, rbenv, and Rails installed on your local machine or development server, following Steps 1-4 in How To Install Ruby on Rails with rbenv on Ubuntu 18.04. This tutorial uses Ruby 2.5.1, rbenv 1.1.2, and Rails 5.2.3.
遵循在Ubuntu 18.04上如何使用rbenv安装Ruby on Rails的 步骤1-4中的步骤 ,将Ruby, rbenv和Rails安装在本地计算机或开发服务器上 。 本教程使用Ruby 2.5.1 ,rbenv 1.1.2和Rails 5.2.3 。
SQLite installed, and a basic shark information application created, following the directions in How To Build a Ruby on Rails Application.
按照如何构建Ruby on Rails应用程序中的指示安装SQLite,并创建基本的shark信息应用程序 。
Our first step will be to create a nested Post
model, which we will associate with our existing Shark
model. We will do this by creating Active Record associations between our models: posts will belong to particular sharks, and each shark can have multiple posts.
我们的第一步将是创建一个嵌套的Post
模型 ,将其与我们现有的Shark
模型关联。 我们将通过在模型之间创建Active Record 关联来做到这一点:帖子将属于特定的鲨鱼,每个鲨鱼可以有多个帖子。
To get started, navigate to the sharkapp
directory that you created for your Rails project in the prerequisites:
首先,在先决条件中导航到为Rails项目创建的sharkapp
目录:
To create our Post
model, we’ll use the rails generate
command with the model
generator. Type the following command to create the model:
要创建我们的Post
模型,我们将在model
生成器中使用rails generate
命令。 键入以下命令来创建模型:
With body:text
, we’re telling Rails to include a body
field in the posts
database table — the table that maps to the Post
model. We’re also including the :references
keyword, which sets up an association between the Shark
and Post
models. Specifically, this will ensure that a foreign key representing each shark entry in the sharks
database is added to the posts
database.
通过body:text
,我们告诉Rails在posts
数据库表(映射到Post
模型的表)中包含body
字段。 我们还包括:references
关键字,它在Shark
和Post
模型之间建立了关联。 具体来说,这将确保将代表sharks
数据库中每个鲨鱼条目的外键添加到posts
数据库。
Once you have run the command, you will see output confirming the resources that Rails has generated for the application. Before moving on, you can check your database migration file to look at the relationship that now exists between your models and database tables. Use the following command to look at the contents of the file, making sure to substitute the timestamp on your own migration file for what’s shown here:
运行命令后,将看到输出确认Rails为应用程序生成的资源。 在继续之前,您可以检查数据库迁移文件以查看模型与数据库表之间现在存在的关系。 使用以下命令查看文件的内容,并确保将您自己的迁移文件上的时间戳替换为此处显示的内容:
cat db/migrate/20190805132506_create_posts.rb
猫数据库/迁移/ 20190805132506 _create_posts.rb
You will see the following output:
您将看到以下输出:
Output
class CreatePosts < ActiveRecord::Migration[5.2]
def change
create_table :posts do |t|
t.text :body
t.references :shark, foreign_key: true
t.timestamps
end
end
end
As you can see, the table includes a column for a shark foreign key. This key will take the form of model_name_id
— in our case, shark_id
.
如您所见,该表包括一个用于显示鲨鱼外键的列。 该密钥将采用model_name _id
的形式,在本例中为shark _id
。
Rails has established the relationship between the models elsewhere as well. Take a look at the newly generated Post
model with the following command:
Rails在其他地方也建立了模型之间的关系。 使用以下命令查看新生成的Post
模型:
Output
class Post < ApplicationRecord
belongs_to :shark
end
The belongs_to
association sets up a relationship between models in which a single instance of the declaring model belongs to a single instance of the named model. In the case of our application, this means that a single post belongs to a single shark.
belongs_to
关联在模型之间建立关系,在该模型中,声明模型的单个实例属于命名模型的单个实例。 在我们的应用程序中,这意味着单个职位属于单个鲨鱼。
Though Rails has already set the belongs_to
association in our Post
model, we will need to specify a has_many
association in our Shark
model as well in order for that relationship to function properly.
尽管Rails已经在我们的Post
模型中设置了belongs_to
关联,但我们仍需要在Shark
模型中指定has_many
关联,以使该关系正常运行。
To add the has_many
association to the Shark
model, open app/models/shark.rb
using nano
or your favorite editor:
要将has_many
关联添加到Shark
模型, app/models/shark.rb
使用nano
或您喜欢的编辑器打开app/models/shark.rb
:
Add the following line to the file to establish the relationship between sharks and posts:
将以下行添加到文件中,以建立鲨鱼和职位之间的关系:
class Shark < ApplicationRecord
has_many :posts
validates :name, presence: true, uniqueness: true
validates :facts, presence: true
end
One thing that is worth thinking about here is what happens to posts once a particular shark is deleted. We likely do not want the posts associated with a deleted shark persisting in the database. To ensure that any posts associated with a given shark are eliminated when that shark is deleted, we can include the dependent
option with the association.
值得考虑的一件事是,删除特定的鲨鱼后帖子会发生什么。 我们可能不希望与已删除的鲨鱼相关的帖子保留在数据库中。 为了确保在删除该鲨鱼后删除与该鲨鱼相关的所有帖子,我们可以在该关联中包括dependent
选项。
Add the following code to the file to ensure that the destroy
action on a given shark deletes any associated posts:
将以下代码添加到文件中,以确保对给定鲨鱼的destroy
动作将删除所有关联的帖子:
class Shark < ApplicationRecord
has_many :posts, dependent: :destroy
validates :name, presence: true, uniqueness: true
validates :facts, presence: true
end
Once you have finished making these changes, save and close the file. If you are working with nano
, do this by pressing CTRL+X
, Y
, then ENTER
.
完成这些更改后,保存并关闭文件。 如果您使用的是nano
,请按CTRL+X
, Y
,然后按ENTER
。
You now have a model generated for your posts, but you will also need a controller to coordinate between the data in your database and the HTML that’s generated and presented to users.
现在,您已经为帖子生成了一个模型,但是您还需要一个控制器来协调数据库中的数据与生成并呈现给用户HTML之间的关系。
Creating a posts controller will involve setting a nested resource route in the application’s main routing file and creating the controller file itself to specify the methods we want associated with particular actions.
创建发布控制器将涉及在应用程序的主路由文件中设置嵌套资源路由,并创建控制器文件本身以指定我们想要与特定操作关联的方法。
To begin, open your config/routes.rb
file to establish the relationship between your resourceful routes:
首先,打开config/routes.rb
文件以建立资源丰富的路由之间的关系:
Currently, the file looks like this:
当前,文件如下所示:
Rails.application.routes.draw do
resources :sharks
root 'sharks#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
We want to create a dependent relationship relationship between shark and post resources. To do this, update your route declaration to make :sharks
the parent of :posts
. Update the code in the file to look like the following:
我们想要在鲨鱼和职位资源之间创建一个依赖关系 。 为此,请更新您的路线声明以使:sharks
为:posts
的父级。 更新文件中的代码,如下所示:
Rails.application.routes.draw do
resources :sharks do
resources :posts
end
root 'sharks#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Save and close the file when you are finished editing.
完成编辑后,保存并关闭文件。
Next, create a new file called app/controllers/posts_controller.rb
for the controller:
接下来,为控制器创建一个名为app/controllers/posts_controller.rb
的新文件:
In this file, we’ll define the methods that we will use to create and destroy individual posts. However, because this is a nested model, we’ll also want to create a local instance variable, @shark
, that we can use to associate particular posts with specific sharks.
在此文件中,我们将定义用于创建和销毁单个帖子的方法。 但是,因为这是一个嵌套模型,所以我们还想创建一个本地实例变量@shark
,我们可以使用它来将特定帖子与特定鲨鱼相关联。
First, we can create the PostsController
class itself, along with two private
methods: get_shark
, which will allow us to reference a particular shark, and post_params
, which gives us access to user-submitted information by way of the params method.
首先,我们可以创建PostsController
类本身,有两个沿private
方法: get_shark
,这将使我们引用一个特定的鲨鱼,和post_params
,这让我们用的方式获得用户提交的信息PARAMS方法 。
Add the following code to the file:
将以下代码添加到文件中:
class PostsController < ApplicationController
before_action :get_shark
private
def get_shark
@shark = Shark.find(params[:shark_id])
end
def post_params
params.require(:post).permit(:body, :shark_id)
end
end
You now have methods to get the particular shark instances with which your posts will be associated, using the :shark_id
key, and the data that users are inputting to create posts. Both of these objects will now be available for the methods you will define to handle creating and destroying posts.
现在,您可以使用:shark_id
键以及用户输入的用于创建帖子的数据,来获取与您的帖子相关联的特定鲨鱼实例的方法。 这两个对象现在都可用于您定义的用于处理创建和销毁帖子的方法。
Next, above the private
methods, add the following code to the file to define your create
and destroy
methods:
接下来,在private
方法上方,将以下代码添加到文件中,以定义您的create
和destroy
方法:
. . .
def create
@post = @shark.posts.create(post_params)
end
def destroy
@post = @shark.posts.find(params[:id])
@post.destroy
end
. . .
These methods associate @post
instances with particular @shark
instances, and use the collection methods that became available to us when we created the has_many
association between sharks and posts. Methods such as find
and create
allow us to target the collection of posts associated with a particular shark.
这些方法将@post
实例与特定的@shark
实例相关联,并使用在我们创建鲨鱼和帖子之间的has_many
关联时可用的收集方法 。 find
和create
使我们可以定位与特定鲨鱼相关的职位集合。
The finished file will look like this:
完成的文件将如下所示:
class PostsController < ApplicationController
before_action :get_shark
def create
@post = @shark.posts.create(post_params)
end
def destroy
@post = @shark.posts.find(params[:id])
@post.destroy
end
private
def get_shark
@shark = Shark.find(params[:shark_id])
end
def post_params
params.require(:post).permit(:body, :shark_id)
end
end
Save and close the file when you are finished editing.
完成编辑后,保存并关闭文件。
With your controller and model in place, you can begin thinking about your view templates and how you will organize your application’s generated HTML.
放置好控制器和模型后,您就可以开始考虑视图模板以及如何组织应用程序生成HTML。
You have created a Post
model and controller, so the last thing to think about from a Rails perspective will be the views that present and allow users to input information about sharks. Views are also the place where you will have a chance to build out interactivity with Stimulus.
您已经创建了Post
模型和控制器,因此从Rails角度考虑的最后一件事是呈现的视图,并允许用户输入有关鲨鱼的信息。 视图也是您与Stimulus建立互动性的地方。
In this step, you will map out your views and partials, which will be the starting point for your work with Stimulus.
在此步骤中,您将绘制视图和局部视图,这将是您与Stimulus一起工作的起点。
The view that will act as the base for posts and all partials associated with posts is the sharks/show
view.
用作帖子以及与帖子关联的所有部分的基础的视图是sharks/show
视图。
Open the file:
打开文件:
Currently, the file looks like this:
当前,文件如下所示:
<%= notice %>
Name:
<%= @shark.name %>
Facts:
<%= @shark.facts %>
<%= link_to 'Edit', edit_shark_path(@shark) %> |
<%= link_to 'Back', sharks_path %>
When we created our Post
model, we opted not to generate views for our posts, since we will handle them through our sharks/show
view. So in this view, the first thing we will address is how we will accept user input for new posts, and how we will present posts back to the user.
创建Post
模型时,我们选择不生成帖子视图,因为我们将通过sharks/show
视图处理它们。 因此,在此视图中,我们要解决的第一件事是我们如何接受用户对新帖子的输入,以及如何将帖子呈现给用户。
Note: For an alternative to this approach, please see How To Create Nested Resources for a Ruby on Rails Application, which sets up post views using the full range of Create, Read, Update, Delete (CRUD) methods defined in the posts controller. For a discussion of these methods and how they work, please see Step 3 of How To Build a Ruby on Rails Application.
注意:有关此方法的替代方法,请参见如何为Ruby on Rails应用程序创建嵌套资源 ,该方法使用在posts控制器中定义的全部Create,Read,Update,Delete (CRUD)方法来设置发布视图。 有关这些方法以及它们如何工作的讨论,请参见如何构建Ruby on Rails应用程序的 步骤3 。
Instead of building all of our functionality into this view, we will use partials — reusable templates that serve a particular function. We will create one partial for new posts, and another to control how posts are displayed back to the user. Throughout, we’ll be thinking about how and where we can use Stimulus to manipulate the appearance of posts on the page, since our goal is to control the presentation of posts with JavaScript.
与其将所有功能都构建到该视图中,我们将使用partials-用于特定功能的可重用模板。 我们将为新帖子创建一个部分,而另一部分将控制如何将帖子显示回用户。 在整个过程中,我们将考虑如何以及在何处使用Stimulus来操纵页面上帖子的外观,因为我们的目标是使用JavaScript控制帖子的显示。
First, below shark facts, add an header for posts and a line to render a partial called
sharks/posts
:
首先,在shark事实下方,添加标头作为帖子,并添加一行以显示部分名为
sharks/posts
:
. . .
Facts:
<%= @shark.facts %>
Posts
<%= render 'sharks/posts' %>
. . .
This will render the partial with the form builder for new post objects.
这将使用表单构建器为新的post对象呈现部分。
Next, below the Edit
and Back
links, we will add a section to control the presentation of older posts on the page. Add the following lines to the file to render a partial called sharks/all
:
接下来,在“ Edit
和“ Back
链接下方,我们将添加一个部分来控制页面上较旧帖子的显示。 sharks/all
添加到文件中以渲染名为sharks/all
的部分内容:
<%= link_to 'Edit', edit_shark_path(@shark) %> |
<%= link_to 'Back', sharks_path %>
<%= render 'sharks/all' %>
The 当我们开始将刺激集成到此文件中时, Once you are finished making these edits, save and close the file. With the changes you’ve made on the Rails side, you can now move on to installing and integrating Stimulus into your application. 完成这些编辑后,保存并关闭文件。 通过在Rails方面所做的更改,您现在可以继续安装Stimulus并将其集成到您的应用程序中。 The first step in using Stimulus will be to install and configure our application to work with it. This will include making sure we have the correct dependencies, including the Yarn package manager and Webpacker, the gem that will allow us to work with the JavaScript pre-processor and bundler webpack. With these dependencies in place, we will be able to install Stimulus and use JavaScript to manipulate events and elements in the DOM. 使用Stimulus的第一步将是安装和配置我们的应用程序以使其与之配合使用。 这将包括确保我们具有正确的依赖关系,包括Yarn程序包管理器和Webpacker ,这是使我们能够与JavaScript预处理程序和捆绑程序webpack一起使用的宝石 。 有了这些依赖关系之后,我们将能够安装Stimulus并使用JavaScript来操纵DOM中的事件和元素。 Let’s begin by installing Yarn. First, update your package list: 让我们从安装Yarn开始。 首先,更新您的包裹清单: Next, add the GPG key for the Debian Yarn repository: 接下来,为Debian Yarn存储库添加GPG密钥: Add the repository to your APT sources: 将存储库添加到您的APT源: Update the package database with the newly added Yarn packages: 使用新添加的Yarn软件包更新软件包数据库: And finally, install Yarn: 最后,安装Yarn: With 安装了 Open your project’s Gemfile, which lists the gem dependencies for your project: 打开项目的Gemfile,其中列出了项目的gem依赖关系: Inside the file, you will see Turbolinks enabled by default: 在文件内部,您将看到默认情况下启用了Turbolink: Turbolinks is designed to improve performance by optimizing page loads: instead of having link clicks navigate to a new page, Turbolinks intercepts these click events and makes the page request using Asynchronous JavaScript and HTML (AJAX). It then replaces the body of the current page and merges the contents of the Turbolinks旨在通过优化页面负载来提高性能:Turbolinks不会拦截链接单击导航到新页面,而是拦截这些点击事件并使用异步JavaScript和HTML(AJAX)发出页面请求。 然后,它替换当前页面的正文并合并 We get Turbolinks by default in our Gemfile, but we will need to add the 默认情况下,我们在Gemfile中获得Turbolinks,但是我们将需要添加 Save and close the file when you are finished. 完成后保存并关闭文件。 Next, add the gem to your project’s bundle with the 接下来,使用 This will generate a new 这将生成一个新的 Next, install the gem in the context of your bundle with the following 接下来,使用以下 Once the installation is complete, we will need to make one small adjustment to our application’s content security file. This is due to the fact that we are working with Rails 5.2+, which is a Content Security Policy (CSP) restricted environment, meaning that the only scripts allowed in the application must be from trusted sources. 安装完成后,我们将需要对应用程序的内容安全文件进行一些小的调整。 这是由于我们正在使用Rails 5.2+的事实,Rails 5.2+是受内容安全策略(CSP)限制的环境,这意味着应用程序中允许的唯一脚本必须来自受信任的来源。 Open 打开 Add the following lines to the bottom of the file to allow 在文件底部添加以下几行,以允许 This will ensure that the 这将确保 Save and close the file when you are finished making this change. 完成更改后,保存并关闭文件。 By installing 通过安装 The With 安装了 You will see output like the following, indicating that the installation was successful: 您将看到类似以下的输出,表明安装成功: We now have Stimulus installed, and the main directories we need to work with it in place. Before moving on to writing any code, we’ll need to make a few application-level adjustments to complete the installation process. 现在,我们已经安装了Stimulus,并需要使用它的主要目录。 在继续编写任何代码之前,我们需要进行一些应用程序级调整以完成安装过程。 First, we’ll need to make an adjustment to 首先,我们需要对 Open that file: 打开该文件: Change the following 将以下 Save and close the file when you have made this change. 进行此更改后,保存并关闭文件。 Next, open 接下来,打开 Initially, the file will look like this: 最初,文件将如下所示: Delete the boilerplate code that’s there, and add the following code to load your Stimulus controller files and boot the application instance: 删除那里的样板代码,并添加以下代码以加载Stimulus控制器文件并启动应用程序实例: This code uses webpack helper methods to require the controllers in the 此代码使用webpack帮助程序方法来要求 Save and close the file when you are finished editing. 完成编辑后,保存并关闭文件。 You now have Stimulus installed and ready to use in your application. Next, we’ll build out the partials that we referenced in our sharks 现在,您已经安装了Stimulus,可以在您的应用程序中使用它了。 接下来,我们将使用Stimulus 控制器 , 目标和操作构建在“鲨鱼” Our 我们的 Controllers are JavaScript classes that are defined in JavaScript modules and exported as the module’s default object. Through controllers, you have access to particular HTML elements and the Stimulus Application instance defined in 控制器是在JavaScript模块中定义并作为模块的默认对象导出JavaScript类。 通过控制器,您可以访问特定HTML元素和 In our partial, we’re first going to build a form as we normally would using Rails. We will then add a Stimulus controller, action, and targets to the form in order to use JavaScript to control how new posts get added to the page. 在本部分中,我们首先要像通常使用Rails一样构建一个表单。 然后,我们将在表单中添加一个Stimulus控制器,动作和目标,以便使用JavaScript控制如何将新帖子添加到页面。 First, create a new file for the partial: 首先,为局部文件创建一个新文件: Inside the file, add the following code to create a new post object using the 在文件内部,添加以下代码以使用 So far, this form behaves like a typical Rails form, using the 到目前为止,此表单的行为类似于典型的Rails表单,使用 Additionally, the form is scoped to take advantage of the collection methods that come with the associations between the 此外,该表单的范围仅限于利用 Our goal now is to add some Stimulus controllers, events, and actions to control how the post data gets displayed on the page. The user will ultimately submit post data and see it posted to the page thanks to a Stimulus action. 现在,我们的目标是添加一些Stimulus控制器,事件和操作,以控制帖子数据在页面上的显示方式。 最终,用户将通过Stimulus动作提交帖子数据并将其发布到页面上。 First, we’ll add a controller to the form called 首先,我们将在 Make sure you add the closing 确保添加结束 Next, we’ll attach an action to the form that will be triggered by the form submit event. This action will control how user input is displayed on the page. It will reference an 接下来,我们将向表单附加一个动作,该动作将由表单提交事件触发。 此操作将控制用户输入在页面上的显示方式。 它将引用一个 We use the 我们将 The DOM event to listen for. Here, we are using the default event associated with form elements, submit, so we do not need to specify the event in the descriptor itself. For more information about common element/event pairs, see the Stimulus documentation. 要监听的DOM事件 。 在这里,我们使用与表单元素Submit关联的默认事件,因此我们不需要在描述符本身中指定事件。 有关常见元素/事件对的更多信息,请参见“ 激励”文档 。 The controller identifier, in our case 控制器标识符 ,在我们的例子中是 The method that the event should invoke. In our case, this is the 事件应调用的方法 。 在我们的例子中,这是我们将在控制器中定义的 Next, we’ll attach a data target to the user input defined in the 接下来,我们将数据目标附加到 Add the following 将以下 Much like action descriptors, Stimulus targets have target descriptors, which include the controller identifier and the target name. In this case, 与动作描述符非常相似,激励目标具有目标描述符 ,其中包括控制器标识符和目标名称。 在这种情况下, As a last step, we’ll add a data target for the inputted 最后一步,我们将为输入的 Add the following 在表单下方和结束 As with the 与 The finished partial will look like this: 完成的部分将如下所示: Once you have made these changes, you can save and close the file. 进行这些更改后,您可以保存并关闭文件。 You have now created one of the two partials you added to the 现在,您已经创建了添加到 Create a new file named 在 Add the following code to the file to iterate through the collection of posts associated with the selected shark: 将以下代码添加到文件中,以遍历与所选鲨鱼相关的帖子集合: This code uses a for loop to iterate through each post instance in the collection of post objects associated with a particular shark. 此代码使用for循环遍历与特定鲨鱼关联的post对象的集合中的每个post实例。 We can now add some Stimulus actions to this partial to control the appearance of posts on the page. Specifically, we will add actions that will control upvotes and whether or not posts are visible on the page 现在,我们可以向此部分添加一些刺激动作,以控制页面上帖子的外观。 具体来说,我们将添加操作来控制投票以及页面上是否显示帖子 Before we do that, however, we will need to add a gem to our project so that we can work with Font Awesome icons, which we’ll use to register upvotes. Open a second terminal window, and navigate to your 但是,在执行此操作之前,我们需要在项目中添加一个gem,以便我们可以使用Font Awesome图标,并将其用于注册upvotes。 打开第二个终端窗口,然后导航到您的 Open your Gemfile: 打开您的Gemfile: Below your 在 Save and close the file. 保存并关闭文件。 Next, install the gem: 接下来,安装gem: Finally, open your application’s main stylesheet, 最后,打开应用程序的主样式表 Add the following line to include Font Awesome’s styles in your project: 添加以下行以在项目中包括Font Awesome的样式: Save and close the file. You can now close your second terminal window. 保存并关闭文件。 现在,您可以关闭第二个终端窗口。 Back in your 回到您的 Add the following code to 将以下代码添加到 Button tags also take a 按钮标记还带有 Save and close the file when you have finished editing. 完成编辑后,保存并关闭文件。 The final change we will make before moving on to defining our controller is to set a data target and action to control how and when the 在继续定义控制器之前,我们要做的最后更改是设置数据目标和操作,以控制 Open the 再次打开 At the bottom of the file, we have a 在文件的底部,我们有一个 First, add a controller to this 首先,将控制器添加到此 Next, add a button to control the appearance of the partial on the page. This button will trigger a 接下来,添加一个按钮来控制部分在页面上的外观。 此按钮将在我们的posts控制器中触发 Add the button below the 在 Again, we only need to identify our 同样,我们只需要在此处标识我们的 Next, we will add a data target. The goal of setting this target is to control the appearance of the partial on the page. Ultimately, we want users to see older posts only if they have opted into doing so by clicking on the 接下来,我们将添加一个数据目标。 设置此目标的目的是控制页面上局部的外观。 最终,我们希望用户仅在通过单击“ We will therefore attach a data target called 因此,我们将一个名为 Add the following 在按钮下方和部分渲染语句上方,添加以下具有 Be sure to add the closing 确保添加结束标记 The finished 完成的 <%= notice %>
Name:
<%= @shark.name %>
Facts:
<%= @shark.facts %>
Save and close the file when you are finished editing. 完成编辑后,保存并关闭文件。 With this template and its associated partials finished, you can move on to creating the controller with the methods you’ve referenced in these files. 完成此模板及其关联的部分之后,您可以继续使用在这些文件中引用的方法来创建控制器。 Installing Stimulus created the 安装Stimulus会创建 Create a file called 在 First, at the top of the file, extend Stimulus’s built-in 首先,在文件顶部,扩展Stimulus的内置 Next, add the following target definitions to the file: 接下来,将以下目标定义添加到文件中: Defining targets in this way will allow us to access them in our methods with the 以这种方式定义目标将允许我们使用 Next, we can define the 接下来,我们可以定义 This method defines a 此方法使用 Next, the method adds this post input to the 接下来,该方法将此发布输入 Next, below the 接下来,在 Here, we again use the 在这里,我们再次使用 Below 在 Add the following code to define this method: 添加以下代码以定义此方法: Here, we’re creating a 在这里,我们正在创建一个 Next, we’ll use a similar method to hide posts on the page. Add the following code below the 接下来,我们将使用类似的方法来隐藏页面上的帖子。 在 Once again, our 再次,我们的 The finished controller file will now look like this: 完成的控制器文件现在将如下所示: Save and close the file when you are finished editing. 完成编辑后,保存并关闭文件。 With your Stimulus controller in place, you can move on to making some final changes to the 安装好Stimulus控制器后,您可以继续对 With one final change to the sharks 对sharks Open the file: 打开文件: In place of the 代替为我们自动生成以显示和销毁鲨鱼的 Replace the existing 将文件中现有的 These helpers accomplish much the same things as their These helpers accomplish much the same things as their Save and close the file when you are finished editing. 完成编辑后,保存并关闭文件。 You are now ready to test your application. You are now ready to test your application. First, run your database migrations: First, run your database migrations: Next, start your server. If you are working locally, you can do this with the following command: Next, start your server. If you are working locally, you can do this with the following command: If you are working on a development server, you can start the application with: 如果您在开发服务器上工作,则可以使用以下命令启动应用程序: rails s --binding=your_server_ip rails s --binding = your_server_ip Navigate to the application landing page in your browser. If you are working locally, this will be Navigate to the application landing page in your browser. If you are working locally, this will be You will see the following landing page: 您将看到以下登录页面: Clicking on Show will take you to the Clicking on Show will take you to the In the post form, type “These sharks are scary!”: In the post form, type “These sharks are scary!”: Click on Create Post. You will now see the new post on the page: Click on Create Post . You will now see the new post on the page: You can add another new post, if you would like. This time, type “These sharks are often misrepresented in films” and click Create Post: You can add another new post, if you would like. This time, type “These sharks are often misrepresented in films” and click Create Post : In order to test the functionality of the Show Older Posts feature, we will need to leave this page, since our Great White does not currently have any posts that are older than the ones we’ve just added. In order to test the functionality of the Show Older Posts feature, we will need to leave this page, since our Great White does not currently have any posts that are older than the ones we've just added. Click Back to get to the main page, and then Show to return to the Great White landing page: Click Back to get to the main page, and then Show to return to the Great White landing page: Clicking on Show Older Posts will now show you the posts you created: Clicking on Show Older Posts will now show you the posts you created: You can now upvote a post by clicking on the Upvote Post button: You can now upvote a post by clicking on the Upvote Post button: Similarly, clicking Remove Post will hide the post: Similarly, clicking Remove Post will hide the post: You have now confirmed that you have a working Rails application that uses Stimulus to control how nested post resources are displayed on individual shark pages. You can use this as the jumping off point for future development and experimentation with Stimulus. You have now confirmed that you have a working Rails application that uses Stimulus to control how nested post resources are displayed on individual shark pages. You can use this as the jumping off point for future development and experimentation with Stimulus. Stimulus represents a possible alternative to working with rails-ujs, JQuery, and frameworks like React and Vue. Stimulus represents a possible alternative to working with rails-ujs , JQuery , and frameworks like React and Vue. As discussed in the introduction, Stimulus makes the most sense when you need to work directly with HTML generated by the server. It is lightweight, and aims to make code – particularly HTML – self-explanatory to the highest degree possible. If you don’t need to manage state on the client side, then Stimulus may be a good choice. As discussed in the introduction, Stimulus makes the most sense when you need to work directly with HTML generated by the server. It is lightweight, and aims to make code – particularly HTML – self-explanatory to the highest degree possible. If you don't need to manage state on the client side, then Stimulus may be a good choice. If you are interested in how to create nested resources without a Stimulus integration, you can consult How To Create Nested Resources for a Ruby on Rails Application. If you are interested in how to create nested resources without a Stimulus integration, you can consult How To Create Nested Resources for a Ruby on Rails Application . For more information on how you would integrate React with a Rails application, see How To Set Up a Ruby on Rails Project with a React Frontend. For more information on how you would integrate React with a Rails application, see How To Set Up a Ruby on Rails Project with a React Frontend . 翻译自: https://www.digitalocean.com/community/tutorials/how-to-add-stimulus-to-a-ruby-on-rails-application ruby on rails 第4步-安装刺激 (Step 4 — Installing Stimulus)
yarn
installed, you can move on to adding the webpacker
gem to your project.yarn
,您可以继续将webpacker
gem添加到您的项目中。
. . .
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
. . .
sections, while the JavaScript
window
and document
objects and the element persist between renders. This addresses one of the main causes of slow page load times: the reloading of CSS and JavaScript resources.
部分的内容,而JavaScript
window
和document
对象以及元素在渲染之间保持不变。 这解决了页面加载时间慢的主要原因之一:CSS和JavaScript资源的重新加载。
webpacker
gem so that we can install and use Stimulus. Below the turbolinks
gem, add webpacker
:webpacker
gem,以便我们可以安装和使用Stimulus。 在turbolinks
gem下方,添加webpacker
: . . .
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
gem 'webpacker', '~> 4.x'
. . .
bundle
command:bundle
命令将gem添加到项目的bundle中:
Gemfile.lock
file — the definitive record of gems and versions for your project.Gemfile.lock
文件-项目的gem和版本的Gemfile.lock
记录。 bundle exec
command:bundle exec
命令在包的上下文中安装gem:
config/initializers/content_security_policy.rb
, which is the default file Rails gives us for defining application-wide security policies:config/initializers/content_security_policy.rb
,这是Rails为我们定义应用程序范围的安全策略提供的默认文件:
webpack-dev-server
— the server that serves our application’s webpack bundle — as an allowed origin:webpack-dev-server
(服务于我们的应用程序的webpack软件包的服务器)作为允许的来源: . . .
Rails.application.config.content_security_policy do |policy|
policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
end
webpacker-dev-server
is recognized as a trusted asset source.webpacker-dev-server
被识别为可信资产来源。 webpacker
, you created two new directories in your project’s app
directory, the directory where your main application code is located. The new parent directory, app/javascript
, will be where your project’s JavaScript code will live, and it will have the following structure:webpacker
,您在项目的app
目录中创建了两个新目录,这是您的主要应用程序代码所在的目录。 新的父目录app/javascript
将是项目JavaScript代码所在的目录,并且具有以下结构:
app/javascript
directory will contain two child directories: app/javascript/packs
, which will have your webpack entry points, and app/javascript/controllers
, where you will define your Stimulus controllers. The bundle exec
command that we just used will create the app/javascript/packs
directory, but we will need to install Stimulus for the app/javascript/controllers
directory to be autogenerated.app/javascript
目录将包含两个子目录: app/javascript/packs
(将具有您的webpack入口点)和app/javascript/controllers
(将在其中定义Stimulus 控制器) 。 我们刚刚使用的bundle exec
命令将创建app/javascript/packs
目录,但是我们需要安装Stimulus才能自动生成app/javascript/controllers
目录。 webpacker
installed, we can now install Stimulus with the following command:webpacker
,我们现在可以使用以下命令安装Stimulus:
app/views/layouts/application.html.erb
to ensure that our JavaScript code is available and that the code defined in our main webpacker
entry point, app/javascript/packs/application.js
, runs each time a page is loaded.app/views/layouts/application.html.erb
进行调整,以确保我们JavaScript代码可用,并确保在我们的主要webpacker
入口点app/javascript/packs/application.js
定义的代码app/javascript/packs/application.js
,每次加载页面时运行。
javascript_include_tag
tag to javascript_pack_tag
to load app/javascript/packs/application.js
:javascript_include_tag
标记更改为javascript_ pack _tag
以加载app/javascript/packs/application.js
: . . .
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
. . .
app/javascript/packs/application.js
: app/javascript/packs/application.js
:
. . .
console.log('Hello World from Webpacker')
import "controllers"
. . .
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("../controllers", true, /\.js$/)
application.load(definitionsFromContext(context))
app/javascript/controllers
directory and load this context for use in your application.app/javascript/controllers
目录中的app/javascript/controllers
并加载此上下文以在您的应用程序中使用。 show
view — sharks/posts
and sharks/all
— using Stimulus controllers, targets, and actions. show
视图中引用的局部对象(“ sharks/posts
和“ sharks/all
。 第5步—在Rails部分中使用刺激 (Step 5 — Using Stimulus in Rails Partials)
sharks/posts
partial will use the form_with
form helper to create a new post object. It will also make use of Stimulus’s three core concepts: controllers, targets, and actions. These concepts work as follows:form_with
sharks/posts
部分将使用form_with
表单助手创建一个新的post对象。 它还将利用Stimulus的三个核心概念:控制器,目标和动作。 这些概念的工作方式如下:
app/javascript/packs/application.js
.app/javascript/packs/application.js
定义的Stimulus Application实例。
form_with
helper:form_with
帮助器创建一个新的post对象: <%= form_with model: [@shark, @shark.posts.build] do |form| %>
<%= form.text_area :body, placeholder: "Your post here" %>
<%= form.submit %>
<% end %>form_with
helper to build a post object with the fields defined for the Post
model. Thus, the form has a field for the post :body
, to which we’ve added a placeholder
with a prompt for filling in a post. form_with
helper来构建具有为Post
模型定义的字段的post对象。 因此,表单在post :body
字段中添加了一个placeholder
,并提示您填写帖子。 Shark
and Post
models. In this case, the new post object that’s created from user-submitted data will belong to the collection of posts associated with the shark we’re currently viewing.Shark
和Post
模型之间的关联所附带的收集方法。 在这种情况下,根据用户提交的数据创建的新post对象将属于与当前正在查看的鲨鱼相关的post的集合。 posts
in a posts
的控制器:
<%= form.submit %>
<% end %>
addPost
method that we will define in the posts Stimulus controller:addPost
方法,该方法将在post Stimulus控制器中定义: :data
option with form_with
to submit the Stimulus action as an additional HTML data attribute. The action itself has a value called an action descriptor made up of the following::data
选项与form_with
一起使用,以将Stimulus操作作为附加HTML数据属性提交。 动作本身具有一个称为动作描述符的值,该值由以下各项组成:
posts
.posts
。 addBody
method that we will define in the controller.addBody
方法。 :body
element, since we will use this inputted value in the
addBody
method.:body
元素中定义的用户输入上,因为我们将在
addBody
方法中使用此输入值。 :data
option to the :body
element:
:data
选项添加到:body
元素中:
posts
is our controller, and body
is the target itself.posts
是我们的控制者, body
是目标本身。 body
values so that users will be able to see their posts as soon as they are submitted. body
值添加一个数据目标,以便用户在提交后即可查看其帖子。
element with an add
target below the form and above the closing
元素,并add
目标:
. . .
<% end %>
body
target, our target descriptor includes both the name of the controller and the target — in this case, add
.body
目标一样,我们的目标描述符同时包含控制器名称和目标名称-在这种情况下,请add
。
<%= form.submit %>
<% end %>
sharks/show
view template. Next, you’ll create the second, sharks/all
, which will show all of the older posts from the database.sharks/show
视图模板的两个部分之一。 接下来,您将创建第二个sharks/all
,它将显示数据库中所有较旧的帖子。 _all.html.erb
in the app/views/sharks/
directory:app/views/sharks/
目录中创建一个名为_all.html.erb
的新文件:
<% for post in @shark.posts %>
<% end %>sharkapp
project directory.sharkapp
项目目录。
webpacker
gem, add the following line to include the font-awesome-rails
gem in the project:webpacker
gem的下面,添加以下行以在项目中包含font-awesome-rails
gem : . . .
gem 'webpacker', '~> 4.x'
gem 'font-awesome-rails', '~>4.x'
. . .
app/assets/stylesheets/application.css
:app/assets/stylesheets/application.css
:
. . .
*
*= require_tree .
*= require_self
*= require font-awesome
*/
app/views/sharks/_all.html.erb
partial, you can now add two button_tags
with associated Stimulus actions, which will be triggered on click events. One button will give users the option to upvote a post and the other will give them the option to remove it from the page view. app/views/sharks/_all.html.erb
部分中,您现在可以添加两个带有关联的Stimulus动作的button_tags
,这些动作将在点击事件时触发。 一个按钮将为用户提供对帖子进行投票的选项,而另一个按钮将为用户提供将其从页面视图中删除的选项。 app/views/sharks/_all.html.erb
:app/views/sharks/_all.html.erb
: <% for post in @shark.posts %>
<% end %>:data
option, so we’ve added our posts Stimulus controller and two actions: remove
and upvote
. Once again, in the action descriptors, we only need to define our controller and method, since the default event associated with button elements is click. Clicking on each of these buttons will trigger the respective remove
and upvote
methods defined in our controller.:data
选项,因此我们添加了Stimulus控制器帖子和两个操作: remove
和upvote
。 再次,在动作描述符中,我们只需要定义我们的控制器和方法,因为与按钮元素关联的默认事件是click。 单击这些按钮中的每个按钮将触发在我们的控制器中定义的相应的remove
和upvote
方法。 sharks/all
partial will be displayed.sharks/all
部分的显示方式和时间。 show
template again, where the initial call to render sharks/all
is currently defined:show
模板,当前定义了渲染sharks/all
的初始调用:
. . .
. . .
showAll
method in our posts controller. showAll
方法。 render
statement:
render
语句上方添加按钮:
. . .
posts
controller and showAll
method here — the action will be triggered by a click event.posts
控制器和showAll
方法-该操作将由click事件触发。 Show Older Posts
button.Show Older Posts
按钮选择加入后才看到Show Older Posts
。 show
to the sharks/all
partial, and set its default style to visibility:hidden
. This will hide the partial unless users opt in to seeing it by clicking on the button.show
的数据目标附加到sharks/all
部分,并将其默认样式设置为visibility:hidden
。 这将隐藏部分内容,除非用户选择通过单击按钮来查看它。 show
target and style
definition below the button and above the partial render statement:
show
目标和style
定义的
。 . . .
tag.
show
template will look like this:show
模板将如下所示: Posts
<%= render 'sharks/posts' %>
<%= link_to 'Edit', edit_shark_path(@shark) %> |
<%= link_to 'Back', sharks_path %>
第6步-创建刺激控制器 (Step 6 — Creating the Stimulus Controller)
app/javascript/controllers
directory, which is where webpack is loading our application context from, so we will create our posts controller in this directory. This controller will include each of the methods we referenced in the previous step:app/javascript/controllers
目录,这是webpack从中加载应用程序上下文的目录,因此我们将在此目录中创建posts控制器。 该控制器将包括我们在上一步中引用的每个方法:
addBody()
, to add new posts.addBody()
,以添加新帖子。 showAll()
, to show older posts.showAll()
,以显示较早的帖子。 remove()
, to remove posts from the current view.remove()
,从当前视图中删除帖子。 upvote()
, to attach an upvote icon to posts.upvote()
,将upvote图标附加到帖子。 posts_controller.js
in the app/javascript/controllers
directory:app/javascript/controllers
目录中创建一个名为posts_controller.js
的文件:
Controller
class: Controller
类: import { Controller } from "stimulus"
export default class extends Controller {
}
. . .
export default class extends Controller {
static targets = ["body", "add", "show"]
}
this.target-nameTarget
property, which gives us the first matching target element. So, for example, to match the body
data target defined in our targets array, we would use this.bodyTarget
. This property allows us to manipulate things like input values or css styles. this. target-name Target
在我们的方法中访问它们this. target-name Target
this. target-name Target
属性,它为我们提供了第一个匹配的target元素。 因此,例如,为了匹配在目标数组中定义的body
数据目标,我们将使用this. body Target
this. body Target
。 此属性使我们可以操纵输入值或CSS样式之类的东西。 addBody
method, which will control the appearance of new posts on the page. Add the following code below the target definitions to define this method:addBody
方法,该方法将控制页面上新帖子的外观。 在目标定义下面添加以下代码以定义此方法: . . .
export default class extends Controller {
static targets = [ "body", "add", "show"]
addBody() {
let content = this.bodyTarget.value;
this.addTarget.insertAdjacentHTML('beforebegin', "
content
variable with the let
keyword and sets it equal to the post input string that users entered into the posts form. It does this by virtue of the body
data target that we attached to the element in our form. Using
this.bodyTarget
to match this element, we can then use the value
property that is associated with that element to set the value of content
as the post input users have entered.let
关键字定义一个content
变量,并将其设置为等于用户在posts表单中输入的post输入字符串。 它借助于我们以表单形式附加到元素的
body
数据目标来实现。 使用this.bodyTarget
来匹配此元素,然后我们可以使用与该元素关联的value
属性来设置用户输入的post输入content
的值。 add
target we added to the
element below the form builder in the sharks/posts
partial. It does this using the Element.insertAdjacentHTML()
method, which will insert the content of the new post, set in the content
variable, before the add
target element. We’ve also enclosed the new post in an element, so that new posts appear as bulleted list items.
add
到add
目标,该add
目标添加到了在sharks/posts
部分中的表单构建器下方的
元素中。 它使用Element.insertAdjacentHTML()
方法执行此操作,该方法将在content
变量中设置的新帖子的内容插入到add
目标元素之前。 我们还将新帖子包含在元素中,以便新帖子显示为项目符号列表项。
addBody
method, we can add the showAll
method, which will control the appearance of older posts on the page:addBody
方法下方,我们可以添加showAll
方法,该方法将控制页面上较旧帖子的外观: . . .
export default class extends Controller {
. . .
addBody() {
let content = this.bodyTarget.value;
this.addTarget.insertAdjacentHTML('beforebegin', "
this.target-nameTarget
property to match our show
target, which is attached to the sharks/all
partial. We gave it a default style, "visibility:hidden"
, so in this method, we simply change the style to "visible"
. This will show the partial to users who have opted into seeing older posts.
this. target-name Target
this. target-name Target
属性以匹配我们的show
目标,该show
目标以sharks/all
部分附加到"visibility:hidden"
,因此在此方法中,我们只需将样式更改为"visible"
。 这将向选择查看较旧帖子的用户显示偏爱。
showAll
, we’ll next add an upvote
method, to allow users to “upvote” posts on the page by attaching the free Font Awesome check-circle
icon to a particular post.showAll
下方,我们接下来将添加一个upvote
方法,以允许用户通过将免费的 Font Awesome check-circle
图标附加到特定帖子来“赞扬”页面上的帖子。 . . .
export default class extends Controller {
. . .
showAll() {
this.showTarget.style.visibility = "visible";
}
upvote() {
let post = event.target.closest(".post");
post.insertAdjacentHTML('beforeend', '');
}
}
post
variable that will target the closest element with the class
post
— the class we attached to each element in our loop iteration in
sharks/all
. This will target the closest post and add the check-circle
icon just inside element, after its last child.
post
变量,该变量将以class post
目标,后者是最接近的元素-我们在
sharks/all
中的循环迭代中附加到每个元素的类。 这将以最近的帖子为目标,并在
元素的最后一个子元素之后添加
check-circle
图标。 upvote
method to define a remove
method:upvote
方法下面添加以下代码,以定义remove
方法: . . .
export default class extends Controller {
. . .
upvote() {
let post = event.target.closest(".post");
post.insertAdjacentHTML('beforeend', '');
}
remove() {
let post = event.target.closest(".post");
post.style.visibility = "hidden";
}
}
post
variable will target the closest element with the class
post
. It will then set the visibility property to "hidden"
to hide the post on the page.post
变量将目标最接近的与类元素
post
。 然后它将可见性属性设置为"hidden"
以隐藏页面上的帖子。 import { Controller } from "stimulus"
export default class extends Controller {
static targets = ["body", "add", "show"]
addBody() {
let content = this.bodyTarget.value;
this.addTarget.insertAdjacentHTML('beforebegin', "
index
view and testing your application.index
视图进行一些最终更改并测试您的应用程序。 步骤7 —修改索引视图并测试应用程序 (Step 7 — Modifying the Index View and Testing the Application)
index
view you will be ready to test out your application. The index
view is the root of the application, which you set in Step 4 of How To Build a Ruby on Rails Application. index
视图进行最后更改后,您就可以测试应用程序了。 index
视图是应用程序的根,您可以在如何构建Ruby on Rails应用程序的 步骤4中进行设置。
link_to
helpers that were autogenerated for us to display and destroy sharks, we’ll use button_to
helpers. This will help us work with generated HTML code instead of the default Rails JavaScript assets, which we specified we would no longer use in Step 1, when we changed javascript_include_tag
to javascript_pack_tag
in app/views/layouts/application.html.erb
.link_to
帮助器,我们将使用button_to
帮助器。 这将帮助我们生成HTML代码,而不是默认的工作RailsJavaScript的资产,这是我们指定的,我们会在第1步不再使用时,当我们改变javascript_include_tag
到javascript_pack_tag
在app/views/layouts/application.html.erb
。 link_to
helpers in the file with the following button_to
helpers:link_to
助手替换为以下button_to
助手: . . .
<% @sharks.each do |shark| %>
<% end %>
. . .<%= shark.name %>
<%= shark.facts %>
<%= button_to 'Show', shark_path(:id => shark.id), :method => :get %>
<%= button_to 'Edit', edit_shark_path(:id => shark.id), :method => :get %>
<%= button_to 'Destroy', shark_path(:id => shark.id), :method => :delete %>
link_to
counterparts, but the Destroy
helper now relies on generated HTML rather than Rails’s default JavaScript.link_to
counterparts, but the Destroy
helper now relies on generated HTML rather than Rails's default JavaScript.
localhost:3000
, or http://your_server_ip:3000
if you are working on a server.localhost:3000
, or http:// your_server_ip :3000
if you are working on a server. show
view for this shark. Here you will see a form to fill out a post:show
view for this shark. Here you will see a form to fill out a post: 结论 (Conclusion)