Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Ruby on rails

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the example analysis of Ruby on rails, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Experience one adds a basic scaffold and a simple data migration to achieve a basic list of additions, deletions, modifications and queries. Experience 2 should continue to enrich the function on the sub-function. Achieve the following effects:

In each company, there are different departments, according to the method of experience 1, add a department support, a data migration. To achieve the addition, deletion, modification and search function of the department. The results of the department are as follows:

The specific way is omitted.

Simply create a bracket without a custom application, it will be difficult to use, in order to add a department to a company, it will be troublesome. Let's compare the page we want with the page of the department generated by the bracket:

Cid and Chargeuserid do not need to be associated first, but can be added by hand, just to be able to reorganize the page.

The pages of the department list and the create department look very similar to what we want on the page, with the middle part of the target page looking like a list and the end like creating a department page.

Here's what you want to sort out today: break down the contents of a page into several files.

First, you need to understand three concepts:

(1) layout: set a uniform look for a series of web pages, most of which provide standard HTML elements that appear at the top and bottom of each page.

(2) templates: templates are the main content of the page, and templates are associated with actions.

(3) Local template: a template will call a number of different local templates to establish the main content of the page, local template allows a complex template to be divided into a number of smaller parts, allowing the separation of common content, such as menus and navigation bars, local templates can be used by templates, it can also be directly used by the layout.

Three kinds of embedded Ruby (ERB) files, layout, template and partial template, form a web page.

Create a local template to add a department form:

A partial template is just another ERB file that contains the same type of tags as the template. The views structure of the project is shown below:

Create a local template by copying the app/views/departments/new.html and saving it as app/views/companies/_new_department.html.erb. One important thing is that local templates start with the character _, and Rails uses the character _ to distinguish between page templates and local templates.

Include partial templates in the template:

We need to include the created partial template in its output in the show.html.erb page template of company. The local template, like the template, is just a piece of Ruby code disguised as HTML and sent to the company page through a render command:

Now that the partial template should have been added to the company page, let's take a look at the show.html.erb Company page to see if the information is realistic and correct. The reality is as follows:

It feels strange. Let's take a look at the contents of the app/views/departments/new.html and app/views/companies/_new_department.html.erb files:

1 New department 2 3 4 5

It seems that the problem is in this place, where the page jumps directly to the _ form.html.erb page. So there will be real problems such as the above page.

Next, change the app/views/companies/_new_department.html.erb to the following content (the information copied from the app/views/departments/_form.html.erb, because the page of the added department is also directly redirected to this page) to avoid conflicts:

12 3 4 prohibited this department from being saved: 5 6 7 8 9 10 11 12 13 14 15

16 17 18 19

20 21 22 23

24 25 26 27

28 29 30 31 32 33

Let's take a look at the result of the browser request:

What do you mean, something went wrong again? it is said that @ departmentis empty, but it works normally on the page of adding a department, ah, why? Comparing the content in the template is the same, the problem is the @ department variable, well, this is the problem. The original creation of a department is controlled by DepartmentsController, but now it is controlled by CompaniesController. When calling the create method in DepartmentController, call the following code:

1 # POST / departments 2 # POST / departments.json 3 def create 4 @ department = Department.new (department_params) 5 6 respond_to do | format | 7 if @ department.save 8 format.html {redirect_to @ department, notice: 'Department was successfully created.'} 9 format.json {render action:' show', status:: created Location: @ department} 10 else 11 format.html {render action: 'new'} 12 format.json {render json: @ department.errors, status:: unprocessable_entity} 13 end 14 end 15 end

@ department = Department.new (department_params) originally defined an empty department instance variable when calling create. If you know the problem, then solve the problem.

We need to change the @ department in the page to a local variable.

1 New department 2 3 4 5 6 prohibited this department from being saved: 7 8 9 10 11 12 13 14 15 16 17

18 19 20 21

22 23 24 25

26 27 28 29

30 31 32 33 34 35 36 37 38

How to pass a local variable to a local template:

Because a local template is like a function, considering the values that can be used for @ departmentde in DepartmentController, and because the form is used to initialize department, we just pass a new Department object to the form using the following method:

"new_department",: locals= > {: department = > Department.new}% >

Let's take a look at the results of the page:

It works fine, but there are two Back below, er, because the Back in the local template has not been removed, but there is no need to create a department to department list, so delete it here. It's very simple.

The above is all the content of this article "sample Analysis of Ruby on rails". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report