Mobile Store Notes 6

Source: Internet
Author: User

补充说明:前面我们修改和增加都使用了goods_id,没有像表单页面中需要的那些字段一样设置成attr_accessible,是因为我们数据库中该商品号字段设计为自增所以不需要在表单中添加该字段的输入框。而添加商品或修改商品都涉及到我们去修改数据库中的数据,而商品号我们在add页面没有去修改,是数据库自增的,在edit页面也没有修改商品号商品号仍然是原来的。至于我们根据goods_id来找到修改的链接页面这个我们只是读取,没有写,所以不设置attr_accessible,默认为protected的权限就够了,不能写入或修改,读取还是可以的。

?

?

?

The show and add of the data have already been done, and now it's time to modify the data section.

(1) We click on the modification of the link, will jump to modify the page, but the data to modify the page is false data, regardless of the click on which product modification link results are that false data page, the results are as follows:

(2) above the return connection we have done in the beginning, these links do not involve the database operation we have done in front. We want to achieve the click on which modification link can be displayed on the modified page of the corresponding product data, not false data.

1. For specification, and rails default routing specification, we changed the modified action name update with the view to edit, so that we can define the actual modification to occur with the action named Update.

Modify Route 19th update to edit as follows, because we are modifying a specific product so the routing parameters need a specific product goods_id, the result is as follows:

2. Modify the view file Update.html.erb, do not use refactor way, we put the code inside copy, and then delete Update.html.erb, Create the view file Edit.html.erb, and paste the original Update.html.erb code in.

3. We change the Controller update action to edit action, and add code

10~12 Line modified to:

4. Modify the change link on the show page

The original 58 lines each link is update, so no matter which product click the edit link is the same page, are the same false data

We should modify 58 lines, add the parameter goods_id later, so as to jump to the specific product modification page

58 lines are modified as follows:

5. We test whether it is feasible, we first goods_id in the database goods_id record 1 to 111, so that the first record in the database goods_id 2, as follows:

Then we move the show page cursor to the first modification link, the lower left corner shows the goods_id/edit of the record, indicating that the result is corresponding to the database.

6. The above modification instructions are linked correctly, and the name is also standardized, but we also need to use Ruby code to rewrite the edit page to display the actual data rather than the false data.

NOTE: The following SQL statement is the query all fields, *, if we write a specific field will be error miss the rest of the field, this first regardless, after querying all the fields you want to use a partial field or all fields can be.

?

It is interesting to change the edit page from a static page of false data to a dynamic one, because your Add page template is the same form as the Edit page template, except that the form of the edit page shows the product information in the database, and the Add page form blank requires us to enter, so according to what they have in common, We do this:

1. Delete the code of the Edit static page

2. Copy the Code of the Add page to the edit page

Only the above two steps, that is, the Add page and the edit page code all the same

The result is that we click on a product link, will automatically jump to the product's edit page, the page will automatically display the corresponding database information as follows:

As shown on the page and the Add page, but can use the corresponding controller edit action in the database obtained data displayed on the page. But we changed the words with the added words to the following lines, line 4th and 13th:

Switch

(3) Since the two add page and the edit page code, why the edit page can automatically read the data in the database and show on the page? The difference is that the page can be caused by the difference between the database data obtained in the corresponding controller action. The following edit action obtains the corresponding information in the database: goods_id, so the view can be automatically read and displayed, and the Add action will not read the data in the database.

(4) At this time we also need to add a real change action update, but also appropriate to modify the dynamic page code

1, add the 21st line of the route, note that the HTTP action needs to be post mode

2, CONTROLLER Add Update action line 29th to 34th

3, the following 22 lines of code to modify the submitted to which action execution

The original code:

Modify the code after the 22nd line:

4, the result is as follows, we click on the modification link of a product, add 3333 Huawei in the following

Click the button to submit a successful go to the show page, and the data is indeed modified successfully, the page and the data in the database can see the modified results

?

The above modification of the function of the product has also been completed, the next completion of the deletion of products, product deletion is much simpler than the above, the deletion function does not need two actions to complete (a view of a no view, such as Edit and update two mates to complete), so do not need the view file,

(1) Add route line 22nd, the Rails specification removes the action named destroy, and delete is just the URL named other line, but it is best to name delete from the specification. Because the deletion is a specific product, so need to product number goods_id to do parameters

(2) Add destroy action, line 36th to 41st

(3) Modify the link code removed from the show page line 59th

The original code:

Modify the code after the 59th line:

Note: the page link URL in the Html.erb view template is written in a route, that is, something behind a get or post in a routing file, not a write action, so it is/delete instead of/destroy

(4) View effects

The original page is as follows:

After we click on the serial number 6 for the deletion of the record link, jump back to this page and record deletion is successful, the deletion function is complete:

?

?

Mobile Store Notes 6

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.