6--rails resources in the schema 2

Source: Internet
Author: User

In the previous section, we created the controller for the microblog resource and created the corresponding 4 actions, and rails will automatically help us generate the route information for the action and the default is get, we need to change

We don't need to open the file every time we view the routing information by command.

The following are the access routes that rails automatically generates for us.

The first column is the HTTP action such as Create is the post (the default is a get error, so the following command to generate a full set of routes is correct), the second column is the URL access, the third column is the controller and the corresponding action, All routing information for Rails engineering can be viewed at any time through this command.

We've talked about 7 ways to access each resource (essentially an action), and some of the ways we have customized action access. So if we're writing 7 different access methods to each resource, it's too complicated to read, and it's easy for rails to write. In the route, write the resources keyword, followed by the name of the resource, then it will automatically generate 7 routes of these resources.

So we delete the following 4 get statements

As follows, note that there are spaces between the resources and the colon, so that a row has a full set of routing information for the posts resource.

If the build is OK, we can use the route view command again to view:

Weibo now has empty resources, but when it is time to add resources, resources are needed to use the model, which is the database.

So we create a resource, like the previous trouble, to create a controller and action, and then modify the route, the model has to add itself, there is no easier way? Yes, scaffolding, automatically generates a complete resource suite, including the Controller and action, the model, and the corresponding large number of view code are all built for us:

But before we do that, we need to identify the models, such as Weibo, which requires IDs to represent each microblog and also needs content, and the ID will automatically generate the rails, and we'll add only content.

Because we want to build a complete set of resources through scaffolding, we first delete the previously generated controller and so on.

With the above command, all files associated with the posts controller are deleted.

And don't forget to delete the route.

And then use scaffolding to generate, notice that scaffolding is the same as the model for the singular post and the controller is the plural posts, this is the contract of rails; directly after the scaffold post directly to the name and type of the data field, which is the content is the text type

After the command presses ENTER, we also need to execute the data Migration command so that the data tables we created are actually created in the rails database:

Then go back to see the IDE, you can see the deleted resources:p OSTs and automatically generated.

Controller files There are 7 types of action and other code that have been created:

The view also generates a whole bunch of basic HTML code, and then we start the service

The above results from the routing profile can know that the access is index, and index is to list all resources, such as Weibo list of all Weibo records and the following list of micro-blog content. Because our Weibo has not yet added content, the display effect is the same as above. So how do you create a Weibo blog? Rails generates a link to the new post directly, but instead of the link, we enter the route for the page that created the microblog in the browser, and click the Enter page, which is a form to create the Weibo:

We lose the content, and then click Create Post to create a new microblog, which, of course, is built by scaffolding.

Let's look at POSTS_CONTROLLER.RB in this controller file, index this action:

Index on a line of code, this code means that the post model instance through the all method to get all the data records of the model, and save to the variable posts, and then the index action will default render index this view, Rendering also passes the posts variable into the index view so that the contents of these variables can be accessed in the view

Let's take a look at the index view file, which means to list the contents:

<% @posts.each do |post| %>就是将posts变量中所有元素进行遍历(也就是控制器的index这个action将posts变量传递到index视图,然后在该视图中遍历该变量所有元素),并将它所有元素的content内容进行显示(也就是上面标签内的代码)。

Because there is no microblogging, so the display is empty, we try to add a record, let index have something to display, that is, the new page to enter the content click Create Post:

Then the program automatically goes to this microblogging page, the results are as follows:

Then we visit index homepage (http://localhost:3000 default is index, do not need to add index back)

This is the page that lists all the tweets on the homepage, and the page for that particular microblog is done through show. We can enter the path from the browser to access the specific micro-blog last surface parameter 1 is the ID number of the microblog.

So let's take a look at the show and see that this action doesn't have any code, but it's not a process, but rails hides show's processing code by default. That is, you make HTTP requests from the standard URLs above, so it is possible for the show to not write any code:

In fact, the hidden code is a line, we can add to write up also:

This line of code means: The post model uses the Find function to find a specific piece of content and save it to a variable according to the parameters.

The URL 1 is the ID number of the Weibo record that we want to access, which is passed to the show action with the HTTP request, and in rails it will save this ID number to the params array and the keyword ID ([: id]).

params[:id]就是去获取http请求中传递过来的id。

The post variable is then passed to the corresponding view for display, and the show view file looks like this:

<%=@post.content %>作用就是将post变量中的content字段显示出来

There is only one record in Weibo, and if we visit records that don't exist, we'll get an error:

couldn ' t find Post with ' id ' =2 It means that the record number is not found . 2 the record.

A summary is:

The user initiates a request through a URL to the rails application, and rails receives the request to determine which controller should be processed by the controller, and then the action will be given to the corresponding action by the control, where it obtains the corresponding data (record) through the model by acquiring the passed parameters. Pass the data to the view (this rails automatically does the work for us without writing the code), and the view code determines what content of the display data is rendered by rails.

?

?

6--rails resources in the schema 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.