{"id":406,"date":"2023-03-13T23:14:24","date_gmt":"2023-11-24T04:22:22","guid":{"rendered":"https:\/\/www.silicloud.com\/zh\/blog\/index.php\/2023\/11\/30\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/"},"modified":"2025-08-02T05:27:42","modified_gmt":"2025-08-01T21:27:42","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/","title":{"rendered":"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357"},"content":{"rendered":"<h3>\u5f15\u8a00<\/h3>\n<p>Ruby on Rails\u662f\u4e00\u6b3e\u70ed\u95e8\u7684\u670d\u52a1\u5668\u7aefWeb\u5e94\u7528\u6846\u67b6\u3002\u5b83\u9a71\u52a8\u7740\u4eca\u5929\u7f51\u7edc\u4e0a\u8bb8\u591a\u6d41\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982GitHub\u3001Basecamp\u3001SoundCloud\u3001Airbnb\u548cTwitch\u3002\u51ed\u501f\u6ce8\u91cd\u7a0b\u5e8f\u5458\u4f53\u9a8c\u548c\u56f4\u7ed5\u5b83\u5efa\u7acb\u7684\u5145\u6ee1\u6fc0\u60c5\u7684\u793e\u533a\uff0cRuby on Rails\u5c06\u4e3a\u60a8\u63d0\u4f9b\u6784\u5efa\u548c\u7ef4\u62a4\u73b0\u4ee3Web\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u5de5\u5177\u3002<\/p>\n<p>React\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u524d\u7aef\u7528\u6237\u754c\u9762\u7684JavaScript\u5e93\u3002\u7531Facebook\u652f\u6301\uff0c\u5b83\u662f\u5f53\u4eca\u6700\u6d41\u884c\u7684\u524d\u7aef\u5e93\u4e4b\u4e00\u3002React\u63d0\u4f9b\u4e86\u8bf8\u5982\u865a\u62dfDOM\u3001\u7ec4\u4ef6\u67b6\u6784\u548c\u72b6\u6001\u7ba1\u7406\u7b49\u529f\u80fd\uff0c\u4f7f\u524d\u7aef\u5f00\u53d1\u8fc7\u7a0b\u66f4\u6709\u7ec4\u7ec7\u548c\u9ad8\u6548\u3002<\/p>\n<p>\u968f\u7740\u7f51\u7edc\u524d\u7aef\u9010\u6e10\u4ece\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u4e2d\u5206\u79bb\u51fa\u6765\uff0c\u5c06Rails\u7684\u4f18\u96c5\u4e0eReact\u7684\u9ad8\u6548\u7ed3\u5408\uff0c\u60a8\u5c06\u80fd\u591f\u6784\u5efa\u5f3a\u5927\u800c\u73b0\u4ee3\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u4e86\u89e3\u5f53\u524d\u8d8b\u52bf\u3002\u901a\u8fc7\u5728Rails\u89c6\u56fe\u4e2d\u4f7f\u7528React\u6765\u6e32\u67d3\u7ec4\u4ef6\uff08\u800c\u4e0d\u662fRails\u6a21\u677f\u5f15\u64ce\uff09\uff0c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u5c06\u83b7\u76ca\u4e8eJavaScript\u548c\u524d\u7aef\u5f00\u53d1\u7684\u6700\u65b0\u8fdb\u5c55\uff0c\u540c\u65f6\u5229\u7528\u4e86Ruby on Rails\u7684\u8868\u73b0\u529b\u3002<\/p>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u5c06\u521b\u5efa\u4e00\u4e2aRuby on Rails\u5e94\u7528\u7a0b\u5e8f\uff0c\u7528\u4e8e\u5b58\u50a8\u60a8\u559c\u7231\u7684\u98df\u8c31\uff0c\u7136\u540e\u4f7f\u7528React\u524d\u7aef\u5c06\u5176\u663e\u793a\u51fa\u6765\u3002\u5f53\u60a8\u5b8c\u6210\u540e\uff0c\u60a8\u5c06\u80fd\u591f\u4f7f\u7528Bootstrap\u98ce\u683c\u7684React\u754c\u9762\u521b\u5efa\u3001\u67e5\u770b\u548c\u5220\u9664\u98df\u8c31\u3002<\/p>\n<div>\n    <img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/6-0.png\" class='post-images' alt=\"\u5b8c\u6210\u7684\u98df\u8c31\u5e94\u7528\u4e3b\u9875\u622a\u56fe\" title=\"\u5b8c\u6210\u7684\u98df\u8c31\u5e94\u7528\u4e3b\u9875\u622a\u56fe\">\n<\/div>\n<h2>\u524d\u63d0\u6761\u4ef6<\/h2>\n<p>\u6309\u7167\u672c\u6559\u7a0b\uff0c\u60a8\u9700\u8981\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>\u5728\u60a8\u7684\u5f00\u53d1\u673a\u5668\u4e0a\u5b89\u88c5Node.js\u548cnpm\u3002\u672c\u6559\u7a0b\u4f7f\u7528Node.js\u7248\u672c16.14.0\u548cnpm\u7248\u672c8.3.1\u3002Node.js\u662f\u4e00\u4e2aJavaScript\u8fd0\u884c\u65f6\u73af\u5883\uff0c\u5141\u8bb8\u60a8\u5728\u6d4f\u89c8\u5668\u5916\u8fd0\u884c\u4ee3\u7801\u3002\u5b83\u9644\u5e26\u4e00\u4e2a\u9884\u88c5\u7684\u5305\u7ba1\u7406\u5668npm\uff0c\u8ba9\u60a8\u53ef\u4ee5\u5b89\u88c5\u548c\u66f4\u65b0\u8f6f\u4ef6\u5305\u3002\u8981\u5728Ubuntu 20.04\u6216macOS\u4e0a\u5b89\u88c5\u8fd9\u4e9b\uff0c\u8bf7\u6309\u7167\u300a\u5982\u4f55\u5728Ubuntu 20.04\u4e0a\u5b89\u88c5Node.js\u300b\u4e2d\u7684&#8221;\u4f7f\u7528PPA\u5b89\u88c5&#8221;\u90e8\u5206\u6216\u300a\u5982\u4f55\u5728macOS\u4e0a\u5b89\u88c5Node.js\u5e76\u521b\u5efa\u672c\u5730\u5f00\u53d1\u73af\u5883\u300b\u4e2d\u7684\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li>\u5728\u60a8\u7684\u5f00\u53d1\u673a\u5668\u4e0a\u5b89\u88c5Yarn\u5305\u7ba1\u7406\u5668\uff0c\u8fd9\u5c06\u5141\u8bb8\u60a8\u4e0b\u8f7dReact\u6846\u67b6\u3002\u672c\u6559\u7a0b\u5728\u7248\u672c1.22.10\u4e0a\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff1b\u8981\u5b89\u88c5\u6b64\u4f9d\u8d56\u9879\uff0c\u8bf7\u9075\u5faa\u5b98\u65b9Yarn\u5b89\u88c5\u6307\u5357\u3002<\/li>\n<li>\u5b89\u88c5Ruby on Rails\u3002\u8981\u83b7\u53d6\u6b64\u529f\u80fd\uff0c\u8bf7\u6309\u7167\u6211\u4eec\u7684\u300a\u5982\u4f55\u5728Ubuntu 20.04\u4e0a\u4f7f\u7528rbenv\u5b89\u88c5Ruby on Rails\u300b\u6307\u5357\u8fdb\u884c\u64cd\u4f5c\u3002\u5982\u679c\u60a8\u60f3\u5728macOS\u4e0a\u5f00\u53d1\u6b64\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u300a\u5982\u4f55\u5728macOS\u4e0a\u4f7f\u7528rbenv\u5b89\u88c5Ruby on Rails\u300b\u3002\u672c\u6559\u7a0b\u5728Ruby\u7248\u672c3.1.2\u548cRails\u7248\u672c7.0.4\u4e0a\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff0c\u56e0\u6b64\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u8bf7\u786e\u4fdd\u6307\u5b9a\u8fd9\u4e9b\u7248\u672c\u3002<\/li>\n<\/ul>\n<div class=\"post-conf-note\">\n<p class=\"post-note\">\n<p class=\"post-conf-desc\">\u6ce8\u610f<\/p>\n<div>\u6ce8\u610f\uff1aRails7\u7248\u672c\u4e0d\u5411\u540e\u517c\u5bb9\u3002\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528Rails5\u7248\u672c\uff0c\u8bf7\u8bbf\u95ee\u6709\u5173\u5728Ubuntu 18.04\u4e0a\u5982\u4f55\u8bbe\u7f6e\u4f7f\u7528React\u524d\u7aef\u7684Ruby on Rails v5\u9879\u76ee\u7684\u6559\u7a0b\u3002<\/div>\n<\/p>\n<ul class=\"post-ul\">\n<li>\u5b89\u88c5PostgreSQL\uff0c\u5982\u300a\u5982\u4f55\u5728Ubuntu 20.04\u4e0a\u5c06PostgreSQL\u4e0eRuby on Rails\u5e94\u7528\u7a0b\u5e8f\u4e00\u8d77\u4f7f\u7528\u300b\u6216\u300a\u5982\u4f55\u5728macOS\u4e0a\u5c06PostgreSQL\u4e0eRuby on Rails\u5e94\u7528\u7a0b\u5e8f\u4e00\u8d77\u4f7f\u7528\u300b\u4e2d\u7684\u6b65\u9aa41\u548c2\u6240\u8ff0\u3002\u8981\u9075\u5faa\u672c\u6559\u7a0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528PostgreSQL\u7248\u672c12\u6216\u66f4\u9ad8\u7248\u672c\u3002\u5982\u679c\u60a8\u60f3\u5728\u4e0d\u540c\u7684Linux\u53d1\u884c\u7248\u6216\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5f00\u53d1\u6b64\u5e94\u7528\u7a0b\u5e8f\uff0c\u8bf7\u53c2\u9605\u5b98\u65b9PostgreSQL\u4e0b\u8f7d\u9875\u9762\u3002\u6709\u5173\u5982\u4f55\u4f7f\u7528PostgreSQL\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u300a\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528PostgreSQL\u300b\u3002<\/li>\n<\/ul>\n<\/div>\n<h2>\u6b65\u9aa4\u4e00 &#8211; \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f<\/h2>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u4f60\u5c06\u4f7f\u7528Rails\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\u6784\u5efa\u4f60\u7684\u98df\u8c31\u5e94\u7528\u3002\u9996\u5148\uff0c\u4f60\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u4e3a\u4e0eReact\u4e00\u8d77\u5de5\u4f5c\u3002<\/p>\n<p>Rails\u63d0\u4f9b\u4e86\u51e0\u4e2a\u540d\u4e3a\u751f\u6210\u5668\u7684\u811a\u672c\uff0c\u7528\u4e8e\u521b\u5efa\u6784\u5efa\u73b0\u4ee3Web\u5e94\u7528\u6240\u9700\u7684\u4e00\u5207\u3002\u8981\u67e5\u770b\u5b8c\u6574\u7684\u547d\u4ee4\u5217\u8868\u53ca\u5176\u529f\u80fd\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails <span class=\"token parameter variable\">-h<\/span>\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5168\u9762\u7684\u9009\u9879\u5217\u8868\uff0c\u5141\u8bb8\u4f60\u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u53c2\u6570\u3002\u5176\u4e2d\u4e00\u4e2a\u5217\u51fa\u7684\u547d\u4ee4\u662f\u65b0\u547d\u4ee4\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u4f60\u5c06\u4f7f\u7528\u65b0\u7684\u751f\u6210\u5668\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u4f60\u7684\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails new <mark>rails_react_recipe<\/mark> <span class=\"token parameter variable\">-d<\/span> postgresql <span class=\"token parameter variable\">-j<\/span> esbuild <span class=\"token parameter variable\">-c<\/span> bootstrap <span class=\"token parameter variable\">-T<\/span>\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u524d\u9762\u7684\u547d\u4ee4\u5728\u540d\u4e3arails_react_recipe\u7684\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f\uff0c\u5b89\u88c5\u6240\u9700\u7684Ruby\u548cJavaScript\u4f9d\u8d56\uff0c\u5e76\u914d\u7f6eWebpack\u3002\u4e0e\u8fd9\u4e2a\u65b0\u7684\u751f\u6210\u5668\u547d\u4ee4\u76f8\u5173\u7684\u6807\u5fd7\u5305\u62ec\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>-d\u6807\u5fd7\u6307\u5b9a\u9996\u9009\u7684\u6570\u636e\u5e93\u5f15\u64ce\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u662fPostgreSQL\u3002<\/li>\n<li>-j\u6807\u5fd7\u6307\u5b9a\u5e94\u7528\u7a0b\u5e8f\u7684JavaScript\u5904\u7406\u65b9\u5f0f\u3002Rails\u63d0\u4f9b\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5904\u7406Rails\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684JavaScript\u4ee3\u7801\u3002\u4f20\u9012\u7ed9-j\u6807\u5fd7\u7684esbuild\u9009\u9879\u6307\u793aRails\u5c06esbuild\u9884\u914d\u7f6e\u4e3a\u9996\u9009\u7684JavaScript\u6253\u5305\u5de5\u5177\u3002<\/li>\n<li>-c\u6807\u5fd7\u6307\u5b9a\u5e94\u7528\u7a0b\u5e8f\u7684CSS\u5904\u7406\u5668\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cBootstrap\u662f\u9996\u9009\u9009\u9879\u3002<\/li>\n<li>-T\u6807\u5fd7\u6307\u793aRails\u8df3\u8fc7\u6d4b\u8bd5\u6587\u4ef6\u7684\u751f\u6210\uff0c\u56e0\u4e3a\u4f60\u4e0d\u4f1a\u5728\u672c\u6559\u7a0b\u4e2d\u7f16\u5199\u6d4b\u8bd5\u3002\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u4e0eRails\u63d0\u4f9b\u7684\u4e0d\u540c\u7684Ruby\u6d4b\u8bd5\u5de5\u5177\uff0c\u4e5f\u5efa\u8bae\u4f7f\u7528\u6b64\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<p>\u547d\u4ee4\u5b8c\u6210\u540e\uff0c\u8f6c\u5230rails_react_recipe\u76ee\u5f55\uff0c\u8fd9\u662f\u60a8\u5e94\u7528\u7a0b\u5e8f\u7684\u6839\u76ee\u5f55\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token builtin class-name\">cd<\/span> <mark>rails_react_recipe<\/mark>\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u8bf7\u5217\u51fa\u76ee\u5f55\u7684\u5185\u5bb9\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">ls<\/span>\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5185\u5bb9\u5c06\u4ee5\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u65b9\u5f0f\u6253\u5370\u51fa\u6765:<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51faGemfile       README.md  bin        db   node_modules  storage  yarn.lock\r\nGemfile.lock  Rakefile   config     lib  package.json  tmp\r\nProcfile.dev  app        config.ru  log  public        vendor\r\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6839\u76ee\u5f55\u5305\u542b\u4e86\u4e00\u4e9b\u81ea\u52a8\u751f\u6210\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\uff0c\u6784\u6210\u4e86Rails\u5e94\u7528\u7684\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u62ec\u4e00\u4e2apackage.json\u6587\u4ef6\uff0c\u91cc\u9762\u5305\u542b\u4e86React\u5e94\u7528\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<p>\u73b0\u5728\u60a8\u5df2\u7ecf\u6210\u529f\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e0b\u4e00\u6b65\u5c06\u662f\u5c06\u5176\u8fde\u63a5\u5230\u4e00\u4e2a\u6570\u636e\u5e93\u3002<\/p>\n<h2>\u7b2c\u4e8c\u6b65 \u2014 \u8bbe\u7f6e\u6570\u636e\u5e93<\/h2>\n<p>\u5728\u8fd0\u884c\u65b0\u7684Rails\u5e94\u7528\u7a0b\u5e8f\u4e4b\u524d\uff0c\u4f60\u5fc5\u987b\u9996\u5148\u5c06\u5176\u8fde\u63a5\u5230\u4e00\u4e2a\u6570\u636e\u5e93\u3002\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u4f60\u5c06\u628a\u65b0\u521b\u5efa\u7684Rails\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u5230\u4e00\u4e2aPostgreSQL\u6570\u636e\u5e93\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b58\u50a8\u548c\u83b7\u53d6\u98df\u8c31\u6570\u636e\u3002<\/p>\n<p>\u5728config\/database.yml\u4e2d\u627e\u5230\u7684database.yml\u6587\u4ef6\u5305\u542b\u4e86\u4e0d\u540c\u5f00\u53d1\u73af\u5883\u7684\u6570\u636e\u5e93\u540d\u79f0\u7b49\u6570\u636e\u5e93\u7ec6\u8282\u3002Rails\u901a\u8fc7\u5728\u73af\u5883\u540d\u79f0\u540e\u9762\u6dfb\u52a0\u4e0b\u5212\u7ebf(_)\u6765\u6307\u5b9a\u5404\u4e2a\u5f00\u53d1\u73af\u5883\u7684\u6570\u636e\u5e93\u540d\u79f0\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u5c06\u4f7f\u7528\u9ed8\u8ba4\u7684\u6570\u636e\u5e93\u914d\u7f6e\u503c\uff0c\u4f46\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u66f4\u6539\u914d\u7f6e\u503c\u3002<\/p>\n<div class=\"post-conf-note\">\n<p class=\"post-note\">\n<p class=\"post-conf-desc\">\u6ce8\u610f<\/p>\n<\/p>\n<p>\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4fee\u6539config\/database.yml\u6765\u8bbe\u7f6e\u4f60\u5e0c\u671bRails\u4f7f\u7528\u54ea\u4e2aPostgreSQL\u89d2\u8272\u6765\u521b\u5efa\u4f60\u7684\u6570\u636e\u5e93\u3002\u5728\u524d\u63d0\u6761\u4ef6\u4e2d\uff0c\u4f60\u6839\u636e\u300a\u5982\u4f55\u5728\u4f60\u7684Ruby on Rails\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528PostgreSQL\u300b\u6559\u7a0b\u521b\u5efa\u4e86\u4e00\u4e2a\u7531\u5bc6\u7801\u4fdd\u62a4\u7684\u89d2\u8272\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u8bbe\u7f6e\u7528\u6237\uff0c\u73b0\u5728\u4f60\u53ef\u4ee5\u6309\u7167\u540c\u6837\u524d\u63d0\u6761\u4ef6\u6559\u7a0b\u4e2d\u7684\u7b2c4\u6b65\u2014\u2014\u914d\u7f6e\u548c\u521b\u5efa\u4f60\u7684\u6570\u636e\u5e93\u7684\u8bf4\u660e\u6765\u64cd\u4f5c\u3002<\/p>\n<\/div>\n<p>Rails\u63d0\u4f9b\u4e86\u8bb8\u591a\u547d\u4ee4\uff0c\u4f7f\u5f97\u5f00\u53d1Web\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u7b80\u5355\uff0c\u5305\u62ec\u7528\u4e8e\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u547d\u4ee4\uff0c\u4f8b\u5982\u521b\u5efa\u3001\u5220\u9664\u548c\u91cd\u7f6e\u3002\u8981\u4e3a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails db:create<\/li><\/ol><\/code><\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u521b\u5efa\u4e00\u4e2a\u5f00\u53d1\u548c\u6d4b\u8bd5\u6570\u636e\u5e93\uff0c\u5e76\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>Created database 'rails_react_recipe_development'\r\nCreated database 'rails_react_recipe_test'<\/code><\/pre>\n<p>\u73b0\u5728\u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">bin\/dev<\/li><\/ol><\/code><\/pre>\n<p>Rails \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66ff\u4ee3\u7684 bin\/dev \u811a\u672c\uff0c\u901a\u8fc7\u4f7f\u7528 Foreman gem \u5728\u5e94\u7528\u7684\u6839\u76ee\u5f55\u4e2d\u6267\u884c Procfile.dev \u6587\u4ef6\u4e2d\u7684\u547d\u4ee4\u6765\u542f\u52a8 Rails \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4e00\u65e6\u4f60\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\uff0c\u4f60\u7684\u547d\u4ee4\u63d0\u793a\u7b26\u5c06\u4f1a\u6d88\u5931\uff0c\u7136\u540e\u4e0b\u9762\u7684\u8f93\u51fa\u5c06\u4f1a\u51fa\u73b0\u5728\u539f\u6765\u7684\u4f4d\u7f6e\u4e0a\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>started with pid 70099\r\nstarted with pid 70100\r\nstarted with pid 70101\r\nyarn run <mark>v1.22.10<\/mark>\r\nyarn run <mark>v1.22.10<\/mark>\r\n$ esbuild app\/javascript\/*.* --bundle --sourcemap --outdir=app\/assets\/builds --public-path=assets --watch\r\n$ sass .\/app\/assets\/stylesheets\/application.bootstrap.scss:.\/app\/assets\/builds\/application.css --no-source-map --load-path=node_modules --watch\r\n=&gt; Booting Puma\r\n=&gt; Rails <mark>7.0.4<\/mark> application starting in development\r\n=&gt; Run `bin\/rails server --help` for more startup options\r\n[watch] build finished, watching for changes...\r\nPuma starting in single mode...\r\n* Puma version: <mark>5.6.5<\/mark> (ruby 3.1.2-p20) (\"Birdie's Version\")\r\n*  Min threads: 5\r\n*  Max threads: 5\r\n*  Environment: development\r\n*          PID: 70099\r\n* Listening on http:\/\/127.0.0.1:3000\r\n* Listening on http:\/\/[::1]:3000\r\nUse Ctrl-C to stop\r\nSass is watching for changes. Press Ctrl-C to stop.<\/code><\/pre>\n<p>\u8981\u8bbf\u95ee\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u8bf7\u6253\u5f00\u4e00\u4e2a\u6d4f\u89c8\u5668\u7a97\u53e3\u5e76\u5bfc\u822a\u5230http:\/\/localhost:3000\u3002Rails\u7684\u9ed8\u8ba4\u6b22\u8fce\u9875\u9762\u5c06\u52a0\u8f7d\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u5df2\u7ecf\u6b63\u786e\u8bbe\u7f6e\u4e86Rails\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<div><img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/43-0.png\" class='post-images' alt=\"Rails\u6b22\u8fce\u9875\u9762\u7684\u5c4f\u5e55\u622a\u56fe\" title=\"\"><\/div>\n<p>\u8981\u505c\u6b62\u7f51\u9875\u670d\u52a1\u5668\uff0c\u5728\u8fd0\u884c\u670d\u52a1\u5668\u7684\u7ec8\u7aef\u4e0a\u6309\u4e0bCTRL+C\u3002\u4f60\u4f1a\u6536\u5230\u6765\u81eaPuma\u7684\u544a\u522b\u4fe1\u606f\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>^C SIGINT received, starting shutdown\r\n- Gracefully stopping, waiting for requests to finish\r\n=== puma shutdown: 2019-07-31 14:21:24 -0400 ===\r\n- Goodbye!\r\nExiting\r\nsending SIGTERM to all processes\r\nterminated by SIGINT\r\nterminated by SIGINT\r\nexited with code 0<\/code><\/pre>\n<p>\u7136\u540e\u4f60\u7684\u7ec8\u7aef\u63d0\u793a\u7b26\u5c06\u91cd\u65b0\u51fa\u73b0\u3002<\/p>\n<p>\u60a8\u5df2\u6210\u529f\u4e3a\u60a8\u7684\u98df\u8c31\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u4e86\u6570\u636e\u5e93\u3002\u4e0b\u4e00\u6b65\uff0c\u60a8\u5c06\u5b89\u88c5\u6240\u9700\u7684JavaScript\u4f9d\u8d56\u9879\uff0c\u4ee5\u7ec4\u5408\u60a8\u7684React\u524d\u7aef\u3002<\/p>\n<h2>\u7b2c\u4e09\u6b65 \u2014 \u5b89\u88c5\u524d\u7aef\u4f9d\u8d56<\/h2>\n<p>\u5728\u8fd9\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u60a8\u5c06\u5b89\u88c5\u98df\u8c31\u5e94\u7528\u7a0b\u5e8f\u524d\u7aef\u6240\u9700\u7684JavaScript\u4f9d\u8d56\u9879\u3002\u5b83\u4eec\u5305\u62ec\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>React\uff0c\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\u3002<\/li>\n<li>React DOM\uff0c\u4f7fReact\u80fd\u591f\u4e0e\u6d4f\u89c8\u5668DOM\u4ea4\u4e92\u3002<\/li>\n<li>React Router\uff0c\u7528\u4e8e\u5904\u7406React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bfc\u822a\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528Yarn\u8f6f\u4ef6\u5305\u7ba1\u7406\u5668\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u8f6f\u4ef6\u5305\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">yarn<\/span> <span class=\"token function\">add<\/span> react react-dom react-router-dom<\/li><\/ol><\/code><\/pre>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f7f\u7528Yarn\u6765\u5b89\u88c5\u6307\u5b9a\u7684\u8f6f\u4ef6\u5305\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230package.json\u6587\u4ef6\u4e2d\u3002\u8981\u9a8c\u8bc1\u8fd9\u4e00\u70b9\uff0c\u6253\u5f00\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u7684package.json\u6587\u4ef6\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> package.json<\/li><\/ol><\/code><\/pre>\n<p>\u5b89\u88c5\u7684\u8f6f\u4ef6\u5305\u5c06\u5728\u4f9d\u8d56\u9879\u952e\u4e0b\u5217\u51fa\u3002<\/p>\n<div>\n<p>~\/rails_react_recipe\/package.json\u7684\u4e2d\u6587\u539f\u751f\u89e3\u91ca\uff1a<\/p>\n<p>~\/rails_react_recipe\/package.json<\/p>\n<pre class=\"post-pre\"><code>{\r\n  \"name\": \"app\",\r\n  \"private\": \"true\",\r\n  \"dependencies\": {\r\n    \"@hotwired\/stimulus\": \"^3.1.0\",\r\n    \"@hotwired\/turbo-rails\": \"^7.1.3\",\r\n    \"@popperjs\/core\": \"^2.11.6\",\r\n    \"bootstrap\": \"^5.2.1\",\r\n    \"bootstrap-icons\": \"^1.9.1\",\r\n    \"esbuild\": \"^0.15.7\",\r\n    <mark>\"react\": \"^18.2.0\",<\/mark>\r\n    <mark>\"react-dom\": \"^18.2.0\",<\/mark>\r\n    <mark>\"react-router-dom\": \"^6.3.0\",<\/mark>\r\n    \"sass\": \"^1.54.9\"\r\n  },\r\n  \"scripts\": {\r\n    \"build\": \"esbuild app\/javascript\/*.* --bundle --sourcemap --outdir=app\/assets\/builds --public-path=assets\",\r\n    \"build:css\": \"sass .\/app\/assets\/stylesheets\/application.bootstrap.scss:.\/app\/assets\/builds\/application.css --no-source-map --load-path=node_modules\"\r\n  }\r\n}<\/code><\/pre>\n<\/div>\n<p>\u901a\u8fc7\u6309\u4e0bCTRL+X\u952e\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p>\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u4e00\u4e9b\u5e94\u7528\u7a0b\u5e8f\u7684\u524d\u7aef\u4f9d\u8d56\u9879\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u5c06\u4e3a\u4f60\u7684\u98df\u8c31\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u4e00\u4e2a\u4e3b\u9875\u3002<\/p>\n<h2>\u6b65\u9aa4\u56db \u2014 \u8bbe\u7f6e\u4e3b\u9875<\/h2>\n<p>\u5728\u5b89\u88c5\u4e86\u6240\u9700\u7684\u4f9d\u8d56\u9879\u4e4b\u540e\uff0c\u60a8\u73b0\u5728\u5c06\u4e3a\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u4e3b\u9875\uff0c\u4f5c\u4e3a\u7528\u6237\u9996\u6b21\u8bbf\u95ee\u5e94\u7528\u7a0b\u5e8f\u65f6\u7684\u7740\u9646\u9875\u9762\u3002<\/p>\n<p>Rails\u9075\u5faa\u5e94\u7528\u7a0b\u5e8f\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\u67b6\u6784\u6a21\u5f0f\u3002\u5728MVC\u6a21\u5f0f\u4e2d\uff0c\u63a7\u5236\u5668\u7684\u76ee\u7684\u662f\u63a5\u6536\u7279\u5b9a\u8bf7\u6c42\u5e76\u5c06\u5176\u4f20\u9012\u5230\u9002\u5f53\u7684\u6a21\u578b\u6216\u89c6\u56fe\u3002\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u5728\u6d4f\u89c8\u5668\u52a0\u8f7d\u6839URL\u65f6\u663e\u793aRails\u6b22\u8fce\u9875\u3002\u4e3a\u4e86\u6539\u53d8\u8fd9\u4e00\u70b9\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u548c\u89c6\u56fe\u6765\u5904\u7406\u4e3b\u9875\uff0c\u5e76\u5c06\u5176\u4e0e\u4e00\u4e2a\u8def\u7531\u8fdb\u884c\u5339\u914d\u3002<\/p>\n<p>Rails \u63d0\u4f9b\u4e86\u4e00\u4e2a\u63a7\u5236\u5668\u751f\u6210\u5668\u6765\u521b\u5efa\u63a7\u5236\u5668\u3002\u63a7\u5236\u5668\u751f\u6210\u5668\u63a5\u6536\u4e00\u4e2a\u63a7\u5236\u5668\u540d\u79f0\u548c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u8981\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u67e5\u9605 Rails \u6587\u6863\u3002<\/p>\n<p>\u672c\u6559\u7a0b\u5c06\u8c03\u7528\u63a7\u5236\u5668\u4e3aHomepage\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u5e26\u6709index\u884c\u4e3a\u7684Homepage\u63a7\u5236\u5668\u3002<\/p>\n<h2>\u7b2c\u4e09\u90e8\u5206\uff1a\u5728Ubuntu 20.04\u4e0a\u521b\u5efaRails\u63a7\u5236\u5668\u5e76\u8bbe\u7f6e\u6839\u8def\u7531<\/h2>\n<p>\u5728\u672c\u90e8\u5206\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u5728Ruby on Rails v7\u9879\u76ee\u4e2d\u521b\u5efa\u63a7\u5236\u5668\uff0c\u8bbe\u7f6e\u6839\u8def\u7531\uff0c\u5e76\u4e3a\u540e\u7eed\u96c6\u6210React\u524d\u7aef\u505a\u51c6\u5907\u3002\u8fd9\u662f\u6784\u5efaUbuntu 20.04\u4e0aRails\u4e0eReact\u5e94\u7528\u7684\u91cd\u8981\u6b65\u9aa4\u3002<\/p>\n<h3>\u521b\u5efaHomepage\u63a7\u5236\u5668<\/h3>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528Rails\u751f\u6210\u5668\u521b\u5efa\u4e00\u4e2aHomepage\u63a7\u5236\u5668\uff0c\u8fd9\u662fRuby on Rails\u5e94\u7528\u5f00\u53d1\u7684\u57fa\u7840\u6b65\u9aa4\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails g controller <mark>Homepage<\/mark> index<\/li><\/ol><\/code><\/pre>\n<div class=\"post-tip-note\">\n<p class=\"post-note\"><strong>Linux\u7cfb\u7edf\u63d0\u793a\uff1a<\/strong><\/p>\n<p class=\"post-tip-desc\">\u5982\u679c\u5728Ubuntu 20.04\u4e0a\u9047\u5230<strong>FATAL\uff1a\u4fa6\u542c\u9519\u8bef\uff1a\u65e0\u6cd5\u76d1\u89c6\u76ee\u5f55\u4e2d\u7684\u66f4\u6539<\/strong>\u9519\u8bef\uff0c\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u7cfb\u7edf\u6587\u4ef6\u76d1\u89c6\u9650\u5236\u5bfc\u81f4\u7684\u3002\u60a8\u7684\u7cfb\u7edf\u5bf9\u53ef\u76d1\u89c6\u7684\u6587\u4ef6\u6570\u91cf\u6709\u9650\u5236\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/p>\n<pre class=\"post-pre\"><code>echo fs.inotify.max_user_watches=524288 | sudo tee -a \/etc\/sysctl.conf && sudo sysctl -p<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c06\u6c38\u4e45\u589e\u52a0Ubuntu\u7cfb\u7edf\u53ef\u76d1\u89c6\u7684\u76ee\u5f55\u6570\u91cf\u81f3524288\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06524288\u66ff\u6362\u4e3a\u6240\u9700\u6570\u5b57\u6765\u518d\u6b21\u8c03\u6574\u6b64\u8bbe\u7f6e\u3002<\/p><\/div>\n<h3>\u7406\u89e3Rails\u751f\u6210\u7684\u6587\u4ef6<\/h3>\n<p>\u8fd0\u884c\u63a7\u5236\u5668\u547d\u4ee4\u540e\uff0cRuby on Rails\u5c06\u751f\u6210\u4ee5\u4e0b\u5173\u952e\u6587\u4ef6\uff1a<\/p>\n<ul class=\"post-ul\">\n<li><strong>homepage_controller.rb<\/strong> &#8211; \u7528\u4e8e\u63a5\u6536\u6240\u6709\u4e0e\u4e3b\u9875\u76f8\u5173\u7684\u8bf7\u6c42\u3002\u6b64\u6587\u4ef6\u5305\u542b\u60a8\u5728\u547d\u4ee4\u4e2d\u6307\u5b9a\u7684index\u64cd\u4f5c\u3002<\/li>\n<li><strong>homepage_helper.rb<\/strong> &#8211; \u7528\u4e8e\u6dfb\u52a0\u4e0eHomepage\u63a7\u5236\u5668\u76f8\u5173\u7684\u8f85\u52a9\u65b9\u6cd5\u3002<\/li>\n<li><strong>index.html.erb<\/strong> &#8211; \u4f5c\u4e3a\u89c6\u56fe\u9875\u9762\uff0c\u7528\u4e8e\u6e32\u67d3\u4e0e\u4e3b\u9875\u76f8\u5173\u7684\u4efb\u4f55\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p>\u9664\u4e86\u8fd9\u4e9b\u65b0\u521b\u5efa\u7684\u6587\u4ef6\uff0cRails\u8fd8\u4f1a\u81ea\u52a8\u66f4\u65b0\u4f4d\u4e8e<code>config\/routes.rb<\/code>\u7684\u8def\u7531\u6587\u4ef6\uff0c\u4e3a\u60a8\u7684\u4e3b\u9875\u6dfb\u52a0\u4e00\u4e2aGET\u8def\u7531\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u4fee\u6539\u4e3a\u6839\u8def\u7531\u3002<\/p>\n<h3>\u914d\u7f6eRails\u6839\u8def\u7531<\/h3>\n<p>\u5728Ruby on Rails\u4e2d\uff0c\u6839\u8def\u7531\u6307\u5b9a\u4e86\u7528\u6237\u8bbf\u95ee\u5e94\u7528\u7a0b\u5e8f\u6839URL\u65f6\u6240\u663e\u793a\u7684\u5185\u5bb9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e0c\u671b\u7528\u6237\u770b\u5230\u60a8\u7684\u4e3b\u9875\u3002\u8bf7\u4f7f\u7528\u60a8\u559c\u7231\u7684\u7f16\u8f91\u5668\u6253\u5f00\u4f4d\u4e8e<code>config\/routes.rb<\/code>\u7684\u8def\u7531\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> config\/routes.rb<\/li><\/ol><\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5c06<code>get 'homepage\/index'<\/code>\u66ff\u6362\u4e3a<code>root 'homepage#index'<\/code>\uff0c\u4f7f\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<div class=\"file-location\">~\/rails_react_recipe\/config\/routes.rb<\/div>\n<pre class=\"post-pre\"><code>Rails.application.routes.draw do\r\n  <mark>root 'homepage#index'<\/mark>\r\n  # For details on the DSL available within this file, see http:\/\/guides.rubyonrails.org\/routing.html\r\nend<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4fee\u6539\u6307\u793aRuby on Rails\u5c06\u5e94\u7528\u7a0b\u5e8f\u7684\u6839\u8bf7\u6c42\u6620\u5c04\u5230Homepage\u63a7\u5236\u5668\u7684\u7d22\u5f15\u64cd\u4f5c\uff0c\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u5448\u73b0\u4f4d\u4e8e<code>app\/views\/homepage\/index.html.erb<\/code>\u7684<code>index.html.erb<\/code>\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<h3>\u6d4b\u8bd5Rails\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<p>\u4e3a\u4e86\u9a8c\u8bc1\u914d\u7f6e\u662f\u5426\u6709\u6548\uff0c\u8bf7\u542f\u52a8\u60a8\u7684Rails\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">bin\/dev<\/li><\/ol><\/code><\/pre>\n<p>\u5f53\u60a8\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u6216\u5237\u65b0\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5c06\u52a0\u8f7d\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u4e3b\u9875\u3002<\/p>\n<div class=\"post-image-container\"><img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/80-0.webp\" class=\"post-images\" alt=\"Rails\u5e94\u7528\u7a0b\u5e8f\u4e3b\u9875\u663e\u793aHomepage#index\" title=\"Ruby on Rails\u5e94\u7528\u7a0b\u5e8f\u9ed8\u8ba4\u4e3b\u9875\"><\/div>\n<p>\u4e00\u65e6\u60a8\u786e\u8ba4\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u6b63\u5e38\uff0c\u6309\u4e0b<code>Ctrl+C<\/code>\u505c\u6b62\u670d\u52a1\u5668\u3002<\/p>\n<h3>\u51c6\u5907React\u524d\u7aef\u96c6\u6210<\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u6253\u5f00<code>~\/rails_react_recipe\/app\/views\/homepage\/index.html.erb<\/code>\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/views\/homepage\/index.html.erb<\/li><\/ol><\/code><\/pre>\n<p>\u5220\u9664\u6587\u4ef6\u5185\u7684\u6240\u6709\u4ee3\u7801\uff0c\u7136\u540e\u5c06\u6587\u4ef6\u4fdd\u5b58\u4e3a\u7a7a\u767d\u3002\u901a\u8fc7\u8fd9\u6837\u505a\uff0c\u786e\u4fdd<code>index.html.erb<\/code>\u6587\u4ef6\u7684\u5185\u5bb9\u4e0d\u4f1a\u5e72\u6270\u524d\u7aefReact\u7684\u6e32\u67d3\u3002<\/p>\n<p>\u73b0\u5728\u60a8\u5df2\u7ecf\u4e3aRuby on Rails\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u4e86\u4e3b\u9875\uff0c\u60a8\u53ef\u4ee5\u8f6c\u5230\u4e0b\u4e00\u90e8\u5206\uff0c\u5176\u4e2d\u60a8\u5c06\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u524d\u7aef\u4ee5\u4f7f\u7528React\u3002<\/p>\n<h2>\u7b2c\u4e94\u6b65 \u2014 \u5c06React\u914d\u7f6e\u4e3aRails\u524d\u7aef<\/h2>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u60a8\u5c06\u914d\u7f6eRuby on Rails\u4f7f\u7528React\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u524d\u7aef\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5176\u9ed8\u8ba4\u7684\u6a21\u677f\u5f15\u64ce\u3002\u8fd9\u4e2a\u65b0\u7684\u914d\u7f6e\u5c06\u4f7f\u60a8\u80fd\u591f\u4f7f\u7528React\u521b\u5efa\u4e00\u4e2a\u66f4\u5177\u89c6\u89c9\u5438\u5f15\u529b\u548c\u4ea4\u4e92\u6027\u7684\u4e3b\u9875\uff0c\u8fd9\u662f\u5728Ubuntu 20.04\u4e0a\u6784\u5efa\u73b0\u4ee3Web\u5e94\u7528\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<p>\u901a\u8fc7\u5728\u751f\u6210Rails\u5e94\u7528\u7a0b\u5e8f\u65f6\u6307\u5b9a\u7684<code>esbuild<\/code>\u9009\u9879\u7684\u5e2e\u52a9\u4e0b\uff0c\u5df2\u7ecf\u5b8c\u6210\u4e86\u5927\u90e8\u5206\u8bbe\u7f6e\uff0c\u4f7fJavaScript\u80fd\u591f\u4e0eRails\u65e0\u7f1d\u914d\u5408\u3002\u73b0\u5728\u53ea\u9700\u8981\u5c06React\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u52a0\u8f7d\u5230JavaScript\u6587\u4ef6\u7684esbuild\u5165\u53e3\u70b9\u4e2d\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u8bf7\u4ece<code>app\/javascript<\/code>\u76ee\u5f55\u4e2d\u5f00\u59cb\u521b\u5efa\u4e00\u4e2a<code>components<\/code>\u76ee\u5f55\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">mkdir<\/span> ~\/rails_react_recipe\/app\/javascript\/components<\/li><\/ol><\/code><\/pre>\n<p>\u7ec4\u4ef6\u76ee\u5f55\u5c06\u5305\u542b\u9996\u9875\u7684\u7ec4\u4ef6\uff0c\u4ee5\u53ca\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5176\u4ed6React\u7ec4\u4ef6\uff0c\u5305\u62ecReact\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u6587\u4ef6\u3002<\/p>\n<h3>\u914d\u7f6eJavaScript\u5165\u53e3\u70b9<\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u6253\u5f00\u4f4d\u4e8e<code>app\/javascript\/application.js<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/javascript\/application.js<\/li><\/ol><\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0b\u6807\u8bb0\u7684\u4ee3\u7801\u884c\u6dfb\u52a0\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<div class=\"file-location\">~\/rails_react_recipe\/app\/javascript\/application.js<\/div>\n<pre class=\"post-pre\"><code>\/\/ Entry point for the build script in your package.json\r\nimport \"@hotwired\/turbo-rails\"\r\nimport \".\/controllers\"\r\nimport * as bootstrap from \"bootstrap\"\r\n<mark>import \".\/components\"<\/mark><\/code><\/pre>\n<p>\u5728<code>application.js<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u7684\u4ee3\u7801\u884c\u5c06\u4f1a\u5bfc\u5165components\u76ee\u5f55\u4e2d\u7684\u4ee3\u7801\uff0c\u4f7f\u5176\u80fd\u591f\u88abesbuild\u7528\u4e8e\u6253\u5305\u3002\u901a\u8fc7\u5c06<code>\/components<\/code>\u76ee\u5f55\u5bfc\u5165\u5230Rails\u5e94\u7528\u7684JavaScript\u5165\u53e3\u70b9\uff0c\u60a8\u53ef\u4ee5\u4e3a\u4e3b\u9875\u521b\u5efa\u4e00\u4e2aReact\u7ec4\u4ef6\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<h3>\u521b\u5efaReact\u7ec4\u4ef6<\/h3>\n<p>\u7136\u540e\uff0c\u5728\u7ec4\u4ef6\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a<code>Home.jsx<\/code>\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/javascript\/components\/Home.jsx<\/li><\/ol><\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<div class=\"file-location\">~\/rails_react_recipe\/app\/javascript\/components\/Home.jsx<\/div>\n<pre class=\"post-pre\"><code>\/\/ \u5728\u6b64\u6dfb\u52a0React\u7ec4\u4ef6\u4ee3\u7801<\/code><\/pre>\n<p>\u8fd9\u4e2aReact\u7ec4\u4ef6\u5c06\u4e3a\u60a8\u7684Ruby on Rails\u5e94\u7528\u7a0b\u5e8f\u4e3b\u9875\u521b\u5efa\u4e00\u4e2a\u754c\u9762\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u8fd9\u4e2a\u7ec4\u4ef6\u80fd\u591f\u88abRails\u5e94\u7528\u7a0b\u5e8f\u6b63\u786e\u52a0\u8f7d\u548c\u6e32\u67d3\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u6210\u529f\u5730\u5728Ubuntu 20.04\u4e0a\u7684Rails\u5e94\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u4e86React\u524d\u7aef\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u57fa\u672c\u7684Home\u7ec4\u4ef6\u3002\u5728\u4e0b\u4e00\u90e8\u5206\u4e2d\uff0c\u60a8\u5c06\u8fdb\u4e00\u6b65\u5f00\u53d1\u8fd9\u4e2aReact\u7ec4\u4ef6\uff0c\u5e76\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\uff0c\u5b8c\u5584\u60a8\u7684Ruby on Rails\u4e0eReact\u5e94\u7528\u3002<\/p>\n<h2>\u521b\u5efaReact\u4e3b\u9875\u7ec4\u4ef6\u5e76\u914d\u7f6e\u8def\u7531<\/h2>\n<p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e3aRuby on Rails\u5e94\u7528\u521b\u5efaReact\u4e3b\u9875\u7ec4\u4ef6\uff0c\u5e76\u8bbe\u7f6eReact Router\u6765\u7ba1\u7406\u9875\u9762\u5bfc\u822a\u3002\u8fd9\u662f\u6784\u5efa\u73b0\u4ee3\u5355\u9875\u5e94\u7528\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<h3>\u6b65\u9aa41\uff1a\u521b\u5efaReact\u4e3b\u9875\u7ec4\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7f8e\u89c2\u7684\u4e3b\u9875\u7ec4\u4ef6\u3002\u8fd9\u4e2a\u7ec4\u4ef6\u5c06\u4f7f\u7528Bootstrap\u8fdb\u884c\u6837\u5f0f\u8bbe\u8ba1\uff0c\u5e76\u5305\u542b\u5bfc\u822a\u5230\u98df\u8c31\u9875\u9762\u7684\u94fe\u63a5\u3002<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport { Link } from \"react-router-dom\";\r\n\r\nexport default () => (\r\n  &lt;div className=\"vw-100 vh-100 primary-color d-flex align-items-center justify-content-center\"&gt;\r\n    &lt;div className=\"jumbotron jumbotron-fluid bg-transparent\"&gt;\r\n      &lt;div className=\"container secondary-color\"&gt;\r\n        &lt;h1 className=\"display-4\"&gt;Food Recipes&lt;\/h1&gt;\r\n        &lt;p className=\"lead\"&gt;\r\n          A curated list of recipes for the best homemade meal and delicacies.\r\n        &lt;\/p&gt;\r\n        &lt;hr className=\"my-4\" \/&gt;\r\n        &lt;Link\r\n          to=\"\/recipes\"\r\n          className=\"btn btn-lg custom-button\"\r\n          role=\"button\"\r\n        &gt;\r\n          View Recipes\r\n        &lt;\/Link&gt;\r\n      &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n  &lt;\/div&gt;\r\n);\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4eceReact Router\u5e93\u5bfc\u5165\u4e86React\u548cLink\u7ec4\u4ef6\u3002Link\u7ec4\u4ef6\u7528\u4e8e\u521b\u5efa\u5e94\u7528\u5185\u7684\u5bfc\u822a\u94fe\u63a5\uff0c\u65e0\u9700\u5237\u65b0\u9875\u9762\u5373\u53ef\u5728\u4e0d\u540c\u89c6\u56fe\u95f4\u5207\u6362\u3002\u8fd9\u4e2a\u529f\u80fd\u7ec4\u4ef6\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f15\u4eba\u6ce8\u76ee\u7684\u4e3b\u9875\u5e03\u5c40\uff0c\u4f7f\u7528\u4e86Bootstrap\u7684\u7c7b\u8fdb\u884c\u6837\u5f0f\u8bbe\u8ba1\uff0c\u5305\u62ec\u4e00\u4e2a\u9192\u76ee\u7684\u6807\u9898\u3001\u63cf\u8ff0\u6587\u672c\u548c\u6307\u5411\u98df\u8c31\u9875\u9762\u7684\u6309\u94ae\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u7ee7\u7eed\u8bbe\u7f6eReact\u8def\u7531\u7cfb\u7edf\u3002<\/p>\n<h3>\u6b65\u9aa42\uff1a\u8bbe\u7f6eReact\u8def\u7531\u76ee\u5f55\u7ed3\u6784<\/h3>\n<p>\u4e3a\u4e86\u7ec4\u7ec7\u6211\u4eec\u7684\u8def\u7531\u914d\u7f6e\uff0c\u9700\u8981\u5728JavaScript\u5e94\u7528\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e13\u95e8\u7684\u8def\u7531\u6587\u4ef6\u5939\u3002\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">mkdir<\/span> ~\/rails_react_recipe\/app\/javascript\/routes\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8def\u7531\u76ee\u5f55\u5c06\u5305\u542b\u5e94\u7528\u4e2d\u6240\u6709\u9875\u9762\u7684\u8def\u7531\u5b9a\u4e49\u3002\u6bcf\u4e2a\u8def\u7531\u90fd\u4f1a\u6620\u5c04\u5230\u76f8\u5e94\u7684React\u7ec4\u4ef6\uff0c\u5f53\u7528\u6237\u8bbf\u95ee\u7279\u5b9aURL\u65f6\uff0c\u5bf9\u5e94\u7684\u7ec4\u4ef6\u5c06\u88ab\u6e32\u67d3\u5230\u6d4f\u89c8\u5668\u4e2d\u3002<\/p>\n<h3>\u6b65\u9aa43\uff1a\u521b\u5efa\u8def\u7531\u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>\u5728\u8def\u7531\u76ee\u5f55\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aindex.jsx\u6587\u4ef6\u6765\u5b9a\u4e49\u5e94\u7528\u7684\u8def\u7531\u89c4\u5219\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/javascript\/routes\/index.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport { BrowserRouter as Router, Routes, Route } from \"react-router-dom\";\r\nimport Home from \"..\/components\/Home\";\r\n\r\nexport default (\r\n  &lt;Router&gt;\r\n    &lt;Routes&gt;\r\n      &lt;Route path=\"\/\" element={&lt;Home \/&gt;} \/&gt;\r\n    &lt;\/Routes&gt;\r\n  &lt;\/Router&gt;\r\n);\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u8def\u7531\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86\u5fc5\u8981\u7684\u6a21\u5757\uff1aReact\u5e93\u3001React Router\u7684BrowserRouter\uff08\u91cd\u547d\u540d\u4e3aRouter\uff09\u3001Routes\u548cRoute\u7ec4\u4ef6\u3002\u8fd9\u4e9b\u7ec4\u4ef6\u5171\u540c\u5de5\u4f5c\uff0c\u5b9e\u73b0\u4e86\u5ba2\u6237\u7aef\u8def\u7531\u529f\u80fd\u3002\u6211\u4eec\u8fd8\u5bfc\u5165\u4e86\u4e4b\u524d\u521b\u5efa\u7684Home\u7ec4\u4ef6\uff0c\u5e76\u5c06\u5176\u6620\u5c04\u5230\u5e94\u7528\u7684\u6839\u8def\u5f84(&#8220;\/&#8221;)\u3002\u8fd9\u610f\u5473\u7740\u5f53\u7528\u6237\u8bbf\u95ee\u5e94\u7528\u6839URL\u65f6\uff0cHome\u7ec4\u4ef6\u5c06\u88ab\u6e32\u67d3\u3002\u968f\u7740\u5e94\u7528\u7684\u53d1\u5c55\uff0c\u60a8\u53ef\u4ee5\u5728\u6b64\u6587\u4ef6\u4e2d\u6dfb\u52a0\u66f4\u591a\u8def\u7531\uff0c\u6bcf\u4e2a\u8def\u7531\u5bf9\u5e94\u4e00\u4e2a\u9875\u9762\u7ec4\u4ef6\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\uff0c\u7ee7\u7eed\u4e0b\u4e00\u6b65\u3002<\/p>\n<h3>\u6b65\u9aa44\uff1a\u521b\u5efa\u5e94\u7528\u4e3b\u7ec4\u4ef6<\/h3>\n<p>\u4e3a\u4e86\u4f7f\u8def\u7531\u7cfb\u7edf\u6b63\u5e38\u5de5\u4f5c\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aApp\u7ec4\u4ef6\uff0c\u5b83\u5c06\u8d1f\u8d23\u6e32\u67d3\u8def\u7531\u914d\u7f6e\u3002\u5728components\u76ee\u5f55\u4e2d\u521b\u5efaApp.jsx\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/javascript\/components\/App.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u5230App.jsx\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport Routes from \"..\/routes\";\r\n\r\nexport default props => &lt;&gt;{Routes}&lt;\/&gt;;\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u7b80\u6d01\u7684App\u7ec4\u4ef6\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86React\u548c\u4e4b\u524d\u521b\u5efa\u7684\u8def\u7531\u914d\u7f6e\u3002\u7ec4\u4ef6\u7684\u529f\u80fd\u975e\u5e38\u7b80\u5355\uff1a\u5b83\u6e32\u67d3\u8def\u7531\u914d\u7f6e\uff0c\u4f7f\u6240\u6709\u5b9a\u4e49\u7684\u8def\u7531\u5728\u5e94\u7528\u4e2d\u53ef\u7528\u3002\u8fd9\u4e2a\u7ec4\u4ef6\u5c06\u5728\u5e94\u7528\u7684\u5165\u53e3\u70b9\u88ab\u6e32\u67d3\uff0c\u786e\u4fdd\u5f53\u5e94\u7528\u52a0\u8f7d\u65f6\uff0c\u6574\u4e2a\u8def\u7531\u7cfb\u7edf\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u540e\uff0c\u6211\u4eec\u7ee7\u7eed\u521b\u5efa\u5e94\u7528\u7684\u5165\u53e3\u6587\u4ef6\u3002<\/p>\n<h3>\u6b65\u9aa45\uff1a\u521b\u5efaReact\u5e94\u7528\u5165\u53e3\u70b9<\/h3>\n<p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5165\u53e3\u6587\u4ef6\uff0c\u5b83\u5c06\u8d1f\u8d23\u5c06React\u5e94\u7528\u6302\u8f7d\u5230DOM\u4e2d\u3002\u5728components\u76ee\u5f55\u4e2d\u521b\u5efaindex.jsx\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/javascript\/components\/index.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230index.jsx\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport { createRoot } from \"react-dom\/client\";\r\nimport App from \".\/App\";\r\n\r\ndocument.addEventListener(\"turbo:load\", () => {\r\n  const root = createRoot(\r\n    document.body.appendChild(document.createElement(\"div\"))\r\n  );\r\n  root.render(&lt;App \/&gt;);\r\n});\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u5165\u53e3\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86React\u5e93\u3001ReactDOM\u7684createRoot\u51fd\u6570\u4ee5\u53caApp\u7ec4\u4ef6\u3002\u4ee3\u7801\u76d1\u542cTurbo\u7684\u52a0\u8f7d\u4e8b\u4ef6\uff08\u8fd9\u662fRails 7\u4e2d\u7684Turbo\u529f\u80fd\uff09\uff0c\u5f53\u9875\u9762\u52a0\u8f7d\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2aReact\u6839\u8282\u70b9\uff0c\u5e76\u5c06App\u7ec4\u4ef6\u6e32\u67d3\u5230\u65b0\u521b\u5efa\u7684div\u5143\u7d20\u4e2d\u3002\u8fd9\u79cd\u96c6\u6210\u65b9\u5f0f\u5141\u8bb8React\u5e94\u7528\u4e0eRails\u7684Turbo\u9a71\u52a8\u9875\u9762\u52a0\u8f7d\u534f\u540c\u5de5\u4f5c\uff0c\u63d0\u4f9b\u6d41\u7545\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\uff0c\u6700\u540e\u4e00\u6b65\u662f\u6dfb\u52a0\u4e00\u4e9b\u81ea\u5b9a\u4e49CSS\u6837\u5f0f\u3002<\/p>\n<h3>\u6b65\u9aa46\uff1a\u6dfb\u52a0\u81ea\u5b9a\u4e49CSS\u6837\u5f0f<\/h3>\n<p>\u4e3a\u4e86\u4f7f\u6211\u4eec\u7684\u5e94\u7528\u770b\u8d77\u6765\u66f4\u52a0\u7f8e\u89c2\uff0c\u8ba9\u6211\u4eec\u6dfb\u52a0\u4e00\u4e9b\u81ea\u5b9a\u4e49\u6837\u5f0f\u3002\u6253\u5f00application.bootstrap.scss\u6587\u4ef6\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/assets\/stylesheets\/application.bootstrap.scss\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5c06\u6587\u4ef6\u5185\u5bb9\u66ff\u6362\u4e3a\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"post-pre\"><code>\/\/ \u5bfc\u5165Bootstrap\u6837\u5f0f\r\n@import \"bootstrap\/scss\/bootstrap\";\r\n\r\n\/\/ \u81ea\u5b9a\u4e49\u53d8\u91cf\r\n$primary-color: #4a6fa5;\r\n$secondary-color: #6c757d;\r\n\r\n\/\/ \u81ea\u5b9a\u4e49\u6837\u5f0f\r\n.primary-color {\r\n  background-color: $primary-color;\r\n}\r\n\r\n.secondary-color {\r\n  color: white;\r\n}\r\n\r\n.custom-button {\r\n  background-color: white;\r\n  color: $primary-color;\r\n  border: none;\r\n  padding: 10px 20px;\r\n  font-weight: bold;\r\n  transition: all 0.3s ease;\r\n\r\n  &:hover {\r\n    background-color: $secondary-color;\r\n    color: white;\r\n    transform: scale(1.05);\r\n  }\r\n}\r\n\r\n\/\/ \u5168\u5c40\u6837\u5f0f\r\nbody {\r\n  margin: 0;\r\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',\r\n    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',\r\n    sans-serif;\r\n  -webkit-font-smoothing: antialiased;\r\n  -moz-osx-font-smoothing: grayscale;\r\n}\r\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u6837\u5f0f\u5b9a\u4e49\u4e86\u5e94\u7528\u7684\u4e3b\u9898\u989c\u8272\u3001\u6309\u94ae\u6837\u5f0f\u548c\u4e00\u4e9b\u5168\u5c40\u8bbe\u7f6e\u3002\u6211\u4eec\u4f7f\u7528\u4e86SCSS\u7684\u53d8\u91cf\u529f\u80fd\u6765\u5b9a\u4e49\u989c\u8272\uff0c\u4f7f\u6837\u5f0f\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002\u81ea\u5b9a\u4e49\u6309\u94ae\u6837\u5f0f\u6dfb\u52a0\u4e86\u60ac\u505c\u6548\u679c\u548c\u8fc7\u6e21\u52a8\u753b\uff0c\u63d0\u5347\u4e86\u7528\u6237\u4f53\u9a8c\u3002\u5168\u5c40\u6837\u5f0f\u786e\u4fdd\u4e86\u8de8\u6d4f\u89c8\u5668\u7684\u4e00\u81f4\u6027\u548c\u66f4\u597d\u7684\u5b57\u4f53\u6e32\u67d3\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u521b\u5efa\u4e86React\u4e3b\u9875\u7ec4\u4ef6\uff0c\u914d\u7f6e\u4e86\u8def\u7531\u7cfb\u7edf\uff0c\u5e76\u6dfb\u52a0\u4e86\u81ea\u5b9a\u4e49\u6837\u5f0f\u3002\u5728\u4e0b\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u6784\u5efa\u5e94\u7528\u7684\u5176\u4f59\u90e8\u5206\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token atrule\"><span class=\"token rule\">@import<\/span> <span class=\"token string\">'bootstrap\/scss\/bootstrap'<\/span><span class=\"token punctuation\">;<\/span><\/span>\r\n<span class=\"token atrule\"><span class=\"token rule\">@import<\/span> <span class=\"token string\">'bootstrap-icons\/font\/bootstrap-icons'<\/span><span class=\"token punctuation\">;<\/span><\/span>\r\n\r\n<span class=\"token selector\"><span class=\"token class\">.bg_primary-color<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">background-color<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token hexcode color\">#FFFFFF<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.primary-color<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">background-color<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token hexcode color\">#FFFFFF<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.bg_secondary-color<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">background-color<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token hexcode color\">#293241<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.secondary-color<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">color<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token hexcode color\">#293241<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.custom-button<\/span><span class=\"token class\">.btn<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">background-color<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token hexcode color\">#293241<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">color<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token hexcode color\">#FFF<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">border<\/span><span class=\"token punctuation\">:<\/span> none<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.hero<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">width<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">100<\/span><span class=\"token unit\">vw<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">height<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">50<\/span><span class=\"token unit\">vh<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.hero<\/span> img<\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">object-fit<\/span><span class=\"token punctuation\">:<\/span> cover<span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">object-position<\/span><span class=\"token punctuation\">:<\/span> top<span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">height<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">100<\/span><span class=\"token unit\">%<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">width<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">100<\/span><span class=\"token unit\">%<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<span class=\"token selector\"><span class=\"token class\">.overlay<\/span><\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token property\">height<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">100<\/span><span class=\"token unit\">%<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">width<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">100<\/span><span class=\"token unit\">%<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token property\">opacity<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token number\">0.4<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<\/code><\/pre>\n<p>\u60a8\u4e3a\u9875\u9762\u8bbe\u7f6e\u4e86\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u989c\u8272\u3002.hero\u90e8\u5206\u5c06\u4e3a\u4e3b\u9875\u4e0a\u7684\u82f1\u96c4\u56fe\u7247\u6216\u5927\u578b\u7f51\u9875\u6a2a\u5e45\u521b\u5efa\u6846\u67b6\uff0c\u7a0d\u540e\u60a8\u5c06\u6dfb\u52a0\u3002\u6b64\u5916\uff0ccustom-button.btn\u6837\u5f0f\u4f1a\u5e94\u7528\u4e8e\u7528\u6237\u7528\u6765\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u7684\u6309\u94ae\u3002<\/p>\n<p>\u5728CSS\u6837\u5f0f\u8bbe\u7f6e\u597d\u540e\uff0c\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u91cd\u65b0\u542f\u52a8\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">bin\/dev\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u7136\u540e\u5728\u60a8\u7684\u6d4f\u89c8\u5668\u4e2d\u91cd\u65b0\u52a0\u8f7d\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e00\u4e2a\u5168\u65b0\u7684\u4e3b\u9875\u5c06\u52a0\u8f7d\u51fa\u6765\u3002<\/p>\n<div>\n  <img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/141-0.png\" class=\"post-images\" alt=\"\u5e26\u6709\u65b0\u6837\u5f0f\u7684\u4e3b\u9875\" title=\"\"><\/p>\n<p>\u4f7f\u7528CTRL+C\u505c\u6b62\u7f51\u7edc\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u60a8\u914d\u7f6e\u4e86\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4ee5\u4f7f\u7528React\u4f5c\u4e3a\u5176\u524d\u7aef\u3002\u5728\u4e0b\u4e00\u6b65\u4e2d\uff0c\u60a8\u5c06\u521b\u5efa\u80fd\u591f\u521b\u5efa\uff0c\u8bfb\u53d6\uff0c\u66f4\u65b0\u548c\u5220\u9664\u98df\u8c31\u7684\u6a21\u578b\u548c\u63a7\u5236\u5668\u3002<\/p>\n<h2>\u7b2c\u516d\u6b65 &#8211; \u521b\u5efa\u83dc\u8c31\u63a7\u5236\u5668\u548c\u6a21\u578b<\/h2>\n<p>\u73b0\u5728\u4f60\u5df2\u7ecf\u4e3a\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u4e86React\u524d\u7aef\uff0c\u4f60\u5c06\u521b\u5efa\u4e00\u4e2aRecipe\u6a21\u578b\u548c\u63a7\u5236\u5668\u3002Recipe\u6a21\u578b\u5c06\u4ee3\u8868\u5305\u542b\u6709\u5173\u7528\u6237\u914d\u65b9\u4fe1\u606f\u7684\u6570\u636e\u5e93\u8868\uff0c\u800c\u63a7\u5236\u5668\u5c06\u63a5\u6536\u5e76\u5904\u7406\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u6216\u5220\u9664\u914d\u65b9\u7684\u8bf7\u6c42\u3002\u5f53\u7528\u6237\u8bf7\u6c42\u914d\u65b9\u65f6\uff0c\u914d\u65b9\u63a7\u5236\u5668\u63a5\u6536\u8be5\u8bf7\u6c42\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u914d\u65b9\u6a21\u578b\uff0c\u540e\u8005\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u6240\u8bf7\u6c42\u7684\u6570\u636e\u3002\u7136\u540e\uff0c\u6a21\u578b\u5c06\u914d\u65b9\u6570\u636e\u4f5c\u4e3a\u54cd\u5e94\u8fd4\u56de\u7ed9\u63a7\u5236\u5668\u3002\u6700\u540e\uff0c\u8be5\u4fe1\u606f\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p>\u4eceRails\u63d0\u4f9b\u7684generate model\u5b50\u547d\u4ee4\u5f00\u59cb\u521b\u5efa\u4e00\u4e2aRecipe\u6a21\u578b\uff0c\u6307\u5b9a\u6a21\u578b\u7684\u540d\u79f0\u4ee5\u53ca\u5176\u5217\u548c\u6570\u636e\u7c7b\u578b\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails generate model Recipe name:string ingredients:text instruction:text image:string\r\n<\/li><\/ol><\/code><\/pre>\n<p>\u4e0a\u8ff0\u547d\u4ee4\u6307\u793aRails\u521b\u5efa\u4e00\u4e2aRecipe\u6a21\u578b\uff0c\u5b83\u5305\u62ec\u4e00\u4e2a\u540d\u4e3aname\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u5217\u3001\u4e00\u4e2a\u540d\u4e3aingredients\u548cinstruction\u7684\u6587\u672c\u7c7b\u578b\u5217\uff0c\u4ee5\u53ca\u4e00\u4e2a\u540d\u4e3aimage\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u5217\u3002\u672c\u6559\u7a0b\u5c06\u6a21\u578b\u547d\u540d\u4e3aRecipe\uff0c\u56e0\u4e3aRails\u4e2d\u7684\u6a21\u578b\u4f7f\u7528\u5355\u6570\u540d\u79f0\uff0c\u800c\u5b83\u4eec\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8868\u4f7f\u7528\u590d\u6570\u540d\u79f0\u3002<\/p>\n<p>\u8fd0\u884c\u751f\u6210\u6a21\u578b\u547d\u4ee4\u4f1a\u521b\u5efa\u4e24\u4e2a\u6587\u4ef6\u5e76\u6253\u5370\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa\r\n\u8c03\u7528  active_record\r\n      \u521b\u5efa    db\/migrate\/<mark>20221017220817<\/mark>_create_recipes.rb\r\n      \u521b\u5efa    app\/models\/recipe.rb\r\n<\/div><\/code><\/pre>\n<p>\u521b\u5efa\u7684\u4e24\u4e2a\u6587\u4ef6\u6709\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>\u4e00\u4e2arecipe.rb\u6587\u4ef6\uff0c\u5305\u542b\u6240\u6709\u4e0e\u6a21\u578b\u76f8\u5173\u7684\u903b\u8f91\u3002<\/li>\n<li>\u4e00\u4e2a20221017220817_create_recipes.rb\u6587\u4ef6\uff08\u6587\u4ef6\u5f00\u5934\u7684\u6570\u5b57\u53ef\u80fd\u53d6\u51b3\u4e8e\u60a8\u8fd0\u884c\u547d\u4ee4\u7684\u65e5\u671f\u800c\u6709\u6240\u4e0d\u540c\uff09\u3002\u8fd9\u4e2a\u8fc1\u79fb\u6587\u4ef6\u5305\u542b\u521b\u5efa\u6570\u636e\u5e93\u7ed3\u6784\u7684\u6307\u4ee4\u3002<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u5c06\u7f16\u8f91\u914d\u65b9\u6a21\u578b\u6587\u4ef6\u4ee5\u786e\u4fdd\u53ea\u5c06\u6709\u6548\u6570\u636e\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u6a21\u578b\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u6570\u636e\u5e93\u9a8c\u8bc1\u6765\u5b9e\u73b0\u6b64\u76ee\u6807\u3002<\/p>\n<p>\u6253\u5f00\u4f4d\u4e8eapp\/models\/recipe.rb\u7684\u914d\u65b9\u6a21\u578b\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/models\/recipe.rb\r\n<\/li><\/ol><\/code><\/pre>\n<p>\u8bf7\u5c06\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u7684\u7a81\u51fa\u90e8\u5206\u6dfb\u52a0\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/models\/recipe.rb<\/div>\n<pre class=\"post-pre\"><code>class Recipe &lt; ApplicationRecord\r\n  <mark>validates :name, presence: true<\/mark>\r\n  <mark>validates :ingredients, presence: true<\/mark>\r\n  <mark>validates :instruction, presence: true<\/mark>\r\nend\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u4f60\u6dfb\u52a0\u4e86\u6a21\u578b\u9a8c\u8bc1\uff0c\u68c0\u67e5\u540d\u79f0\u3001\u6210\u5206\u548c\u6307\u4ee4\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u7f3a\u5c11\u8fd9\u4e09\u4e2a\u5b57\u6bb5\uff0c\u5219\u83dc\u8c31\u65e0\u6548\uff0c\u4e0d\u4f1a\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p>\u8981\u8ba9Rails\u5728\u4f60\u7684\u6570\u636e\u5e93\u4e2d\u521b\u5efarecipes\u8868\uff0c\u4f60\u9700\u8981\u8fd0\u884c\u4e00\u4e2a\u8fc1\u79fb\uff08migration\uff09\uff0c\u8fd9\u662f\u4e00\u79cd\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u66f4\u6539\u7684\u65b9\u6cd5\u3002\u4e3a\u4e86\u786e\u4fdd\u8fc1\u79fb\u4e0e\u4f60\u8bbe\u7f6e\u7684\u6570\u636e\u5e93\u914d\u5408\u5de5\u4f5c\uff0c\u4f60\u5fc5\u987b\u5bf920221017220817_create_recipes.rb \u6587\u4ef6\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p>\u8bf7\u5728\u4f60\u7684\u7f16\u8f91\u5668\u4e2d\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/db\/migrate\/<mark>20221017220817<\/mark>_create_recipes.rb\r\n<\/li><\/ol><\/code><\/pre>\n<p>\u6dfb\u52a0\u88ab\u6807\u8bb0\u7684\u6750\u6599\u4ee5\u4f7f\u60a8\u7684\u6587\u4ef6\u4e0e\u4e0b\u5217\u5185\u5bb9\u76f8\u5339\u914d\u3002<\/p>\n<div>db\/migrate\/20221017220817_create_recipes.rb<\/div>\n<pre class=\"post-pre\"><code>class CreateRecipes &lt; ActiveRecord::Migration[5.2]\r\n  def change\r\n    create_table :recipes do |t|\r\n      t.string :name<mark>, null: false<\/mark>\r\n      t.text :ingredients<mark>, null: false<\/mark>\r\n      t.text :instruction<mark>, null: false<\/mark>\r\n      t.string :image<mark>, default: 'https:\/\/raw.githubusercontent.com\/do-community\/react_rails_recipe\/master\/app\/assets\/images\/Sammy_Meal.jpg'<\/mark>\r\n\r\n      t.timestamps\r\n    end\r\n  end\r\nend\r\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8fc1\u79fb\u6587\u4ef6\u5305\u542b\u4e00\u4e2aRuby\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2achange\u65b9\u6cd5\u548c\u4e00\u4e2a\u521b\u5efa\u540d\u4e3arecipes\u7684\u8868\u4ee5\u53ca\u5217\u548c\u5b83\u4eec\u7684\u6570\u636e\u7c7b\u578b\u7684\u547d\u4ee4\u3002\u4f60\u8fd8\u901a\u8fc7\u6dfb\u52a0null: false\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5217\u5728\u66f4\u6539\u6570\u636e\u5e93\u4e4b\u524d\u90fd\u6709\u4e00\u4e2a\u503c\uff0c\u66f4\u65b0\u4e8620221017220817_create_recipes.rb\uff0c\u5728name\u3001ingredients\u548cinstruction\u5217\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e2aNOT NULL\u7ea6\u675f\u3002\u6700\u540e\uff0c\u4f60\u4e3aimage\u5217\u6dfb\u52a0\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684\u56fe\u7247URL\uff1b\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u4e0d\u540c\u7684\u56fe\u7247\uff0c\u8fd9\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2aURL\u3002<\/p>\n<p>\u5b8c\u6210\u8fd9\u4e9b\u66f4\u6539\u540e\uff0c\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u51c6\u5907\u597d\u8fd0\u884c\u8fc1\u79fb\u5e76\u521b\u5efa\u8868\u3002\u5728\u60a8\u7684\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails db:migrate\r\n<\/li><\/ol><\/code><\/pre>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u8fc1\u79fb\u547d\u4ee4\u6765\u8fd0\u884c\u8fc1\u79fb\u6587\u4ef6\u4e2d\u7684\u6307\u4ee4\u3002\u547d\u4ee4\u6210\u529f\u8fd0\u884c\u540e\uff0c\u60a8\u5c06\u4f1a\u6536\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u8f93\u51fa\u4fe1\u606f\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa\r\n== <mark>20190407161357<\/mark> CreateRecipes: migrating ====================================\r\n-- create_table(:recipes)\r\n   -&gt; 0.0140s\r\n== <mark>20190407161357<\/mark> CreateRecipes: migrated (0.0141s) ===========================\r\n<\/div><\/code><\/pre>\n<p>\u5b89\u88c5\u597d\u4f60\u7684\u914d\u65b9\u6a21\u578b\u540e\uff0c\u63a5\u4e0b\u6765\u8981\u521b\u5efa\u914d\u65b9\u63a7\u5236\u5668\u6765\u6dfb\u52a0\u521b\u5efa\u3001\u8bfb\u53d6\u548c\u5220\u9664\u914d\u65b9\u7684\u903b\u8f91\u3002\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails generate controller api\/v1\/Recipes index create show destroy  --skip-template-engine --no-helper\r\n<\/li><\/ol><\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u547d\u4ee4\u4e2d\uff0c\u4f60\u4f1a\u5728api\/v1\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2aRecipes\u63a7\u5236\u5668\uff0c\u5305\u62ecindex\u3001create\u3001show\u548cdestroy\u56db\u4e2a\u52a8\u4f5c\u3002 index\u52a8\u4f5c\u5c06\u8d1f\u8d23\u83b7\u53d6\u6240\u6709\u7684\u98df\u8c31\uff1b create\u52a8\u4f5c\u5c06\u8d1f\u8d23\u521b\u5efa\u65b0\u7684\u98df\u8c31\uff1b show\u52a8\u4f5c\u5c06\u83b7\u53d6\u5355\u4e2a\u98df\u8c31\uff0c\u800cdestroy\u52a8\u4f5c\u5c06\u5904\u7406\u5220\u9664\u98df\u8c31\u7684\u903b\u8f91\u3002<\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f20\u5165\u4e00\u4e9b\u6807\u5fd7\u6765\u4f7f\u63a7\u5236\u5668\u66f4\u8f7b\u91cf\u5316\uff0c\u5305\u62ec\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>&#8211;skip-template-engine\uff0c\u8be5\u6807\u5fd7\u6307\u793aRails\u8df3\u8fc7\u751f\u6210Rails\u89c6\u56fe\u6587\u4ef6\uff0c\u56e0\u4e3aReact\u8d1f\u8d23\u5904\u7406\u524d\u7aef\u9700\u6c42\u3002<\/li>\n<li>&#8211;no-helper\uff0c\u8be5\u6807\u5fd7\u6307\u793aRails\u8df3\u8fc7\u4e3a\u63a7\u5236\u5668\u751f\u6210\u8f85\u52a9\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u8be5\u547d\u4ee4\u8fd8\u4f1a\u5728\u8def\u7531\u6587\u4ef6\u4e2d\u4e3a Recipes \u63a7\u5236\u5668\u4e2d\u7684\u6bcf\u4e2a\u64cd\u4f5c\u6dfb\u52a0\u4e00\u6761\u8def\u7531\u3002<\/p>\n<p>\u5f53\u547d\u4ee4\u8fd0\u884c\u65f6\uff0c\u5b83\u5c06\u6253\u5370\u51fa\u4ee5\u4e0b\u7c7b\u4f3c\u7684\u8f93\u51fa:<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa\r\n\r\n<p>\u521b\u5efa <code>app\/controllers\/api\/v1\/recipes_controller.rb<\/code> \u63a7\u5236\u5668\u6587\u4ef6\uff0c\u5e76\u8bbe\u7f6e\u4ee5\u4e0b\u8def\u7531\uff1a<\/p>\r\n<pre><code>namespace :api do\r\n  namespace :v1 do\r\n    get 'recipes\/index'\r\n    get 'recipes\/create'\r\n    get 'recipes\/show'\r\n    get 'recipes\/destroy'\r\n  end\r\nend<\/code><\/pre>\n<p>\u8981\u4f7f\u7528\u8fd9\u4e9b\u8def\u7531\uff0c\u4f60\u9700\u8981\u5bf9\u4f60\u7684 <code>config\/routes.rb<\/code> \u6587\u4ef6\u8fdb\u884c\u66f4\u6539\u3002\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u6253\u5f00 routes.rb \u6587\u4ef6\uff1a<\/p>\n<pre><code>nano ~\/rails_react_recipe\/config\/routes.rb<\/code><\/pre>\n<p>\u5c06\u6b64\u6587\u4ef6\u66f4\u65b0\u4e3a\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4fee\u6539\u6216\u6dfb\u52a0\u6807\u8bb0\u7684\u884c\uff1a<\/p>\n<div>\n<pre><code>Rails.application.routes.draw do\r\n  namespace :api do\r\n    namespace :v1 do\r\n      get 'recipes\/index'\r\n      post 'recipes\/create'\r\n      get '\/show\/:id', to: 'recipes#show'\r\n      delete '\/destroy\/:id', to: 'recipes#destroy'\r\n    end\r\n  end\r\n  root 'homepage#index'\r\n  get '\/*path' => 'homepage#index'\r\n  # Define your application routes per the DSL in https:\/\/guides.rubyonrails.org\/routing.html\r\n\r\n  # Defines the root path route (\"\/\")\r\n  # root \"articles#index\"\r\nend<\/code><\/pre>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u8def\u7531\u6587\u4ef6\u4e2d\uff0c\u4f60\u4fee\u6539\u4e86\u521b\u5efa\u548c\u5220\u9664\u8def\u7531\u7684HTTP\u8c13\u8bcd\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u53d1\u9001\u548c\u5220\u9664\u6570\u636e\u3002\u4f60\u8fd8\u901a\u8fc7\u5728\u8def\u7531\u4e2d\u6dfb\u52a0 <code>:id<\/code> \u53c2\u6570\uff0c\u4fee\u6539\u4e86\u5c55\u793a\u548c\u5220\u9664\u64cd\u4f5c\u7684\u8def\u7531\u3002<code>:id<\/code> \u5c06\u4fdd\u5b58\u4f60\u60f3\u8981\u8bfb\u53d6\u6216\u5220\u9664\u7684\u98df\u8c31\u7684\u6807\u8bc6\u53f7\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>get '\/*path'<\/code> \u6dfb\u52a0\u4e00\u4e2a\u6355\u83b7\u6240\u6709\u8def\u5f84\u7684\u8def\u7531\uff0c\u5c06\u6240\u6709\u4e0d\u5339\u914d\u73b0\u6709\u8def\u7531\u7684\u5176\u4ed6\u8bf7\u6c42\u91cd\u5b9a\u5411\u5230\u4e3b\u9875\u63a7\u5236\u5668\u7684\u7d22\u5f15\u52a8\u4f5c\u3002\u524d\u7aef\u8def\u7531\u5c06\u5904\u7406\u4e0e\u521b\u5efa\u3001\u9605\u8bfb\u6216\u5220\u9664\u98df\u8c31\u65e0\u5173\u7684\u8bf7\u6c42\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u8981\u8bc4\u4f30\u60a8\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u7528\u7684\u8def\u7531\u5217\u8868\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>rails routes<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u663e\u793a\u9879\u76ee\u4e2d\u7684URI\u6a21\u5f0f\u3001\u52a8\u8bcd\u548c\u5339\u914d\u7684\u63a7\u5236\u5668\u6216\u52a8\u4f5c\u7684\u4e00\u4e2a\u957f\u5217\u8868\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u5c06\u6dfb\u52a0\u4e00\u79cd\u903b\u8f91\u6765\u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u7684\u83dc\u8c31\u3002Rails\u4f7f\u7528ActiveRecord\u5e93\u6765\u5904\u7406\u4e0e\u6570\u636e\u5e93\u76f8\u5173\u7684\u4efb\u52a1\u3002ActiveRecord\u5c06\u7c7b\u8fde\u63a5\u5230\u5173\u7cfb\u578b\u6570\u636e\u5e93\u8868\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u4e0e\u8fd9\u4e9b\u8868\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p>\u4e3a\u4e86\u83b7\u53d6\u6240\u6709\u7684\u83dc\u8c31\uff0c\u4f60\u5c06\u4f7f\u7528ActiveRecord\u67e5\u8be2\u83dc\u8c31\u8868\uff0c\u5e76\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6240\u6709\u7684\u83dc\u8c31\u3002<\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6253\u5f00 <code>recipes_controller.rb<\/code> \u6587\u4ef6\uff1a<\/p>\n<pre><code>nano ~\/rails_react_recipe\/app\/controllers\/api\/v1\/recipes_controller.rb<\/code><\/pre>\n<p>\u5c06\u7a81\u51fa\u663e\u793a\u7684\u884c\u6dfb\u52a0\u5230\u98df\u8c31\u63a7\u5236\u5668\u4e2d\uff1a<\/p>\n<div>\n<pre><code>class Api::V1::RecipesController < ApplicationController\r\n  def index\r\n    recipe = Recipe.all.order(created_at: :desc)\r\n    render json: recipe\r\n  end\r\n\r\n  def create\r\n  end\r\n\r\n  def show\r\n  end\r\n\r\n  def destroy\r\n  end\r\nend<\/code><\/pre>\n<\/div>\n<p>\u5728\u4f60\u7684\u7d22\u5f15\u64cd\u4f5c\u4e2d\uff0c\u4f60\u4f7f\u7528\u4e86ActiveRecord\u7684 <code>all<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6240\u6709\u83dc\u8c31\u3002\u4f7f\u7528 <code>order<\/code> \u65b9\u6cd5\uff0c\u4f60\u6309\u7167\u521b\u5efa\u65e5\u671f\u7684\u964d\u5e8f\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u6837\u6700\u65b0\u7684\u83dc\u8c31\u4f1a\u6392\u5728\u9996\u4f4d\u3002\u6700\u540e\uff0c\u4f60\u4f7f\u7528 <code>render<\/code> \u5c06\u83dc\u8c31\u5217\u8868\u4ee5JSON\u54cd\u5e94\u7684\u5f62\u5f0f\u53d1\u9001\u51fa\u53bb\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u5c06\u6dfb\u52a0\u6709\u5173\u521b\u5efa\u65b0\u98df\u8c31\u7684\u903b\u8f91\u3002\u4e0e\u83b7\u53d6\u6240\u6709\u98df\u8c31\u4e00\u6837\uff0c\u4f60\u5c06\u4f9d\u8d56\u4e8eActiveRecord\u6765\u9a8c\u8bc1\u548c\u4fdd\u5b58\u6240\u63d0\u4f9b\u7684\u98df\u8c31\u7ec6\u8282\u3002\u8bf7\u5728\u4f60\u7684\u98df\u8c31\u63a7\u5236\u5668\u4e2d\u66f4\u65b0\u4e0b\u9762\u7a81\u51fa\u663e\u793a\u7684\u4ee3\u7801\u884c\uff1a<\/p>\n<div>\n<pre><code>class Api::V1::RecipesController < ApplicationController\r\n  def index\r\n    recipe = Recipe.all.order(created_at: :desc)\r\n    render json: recipe\r\n  end\r\n\r\n  def create\r\n    recipe = Recipe.create!(recipe_params)\r\n    if recipe\r\n      render json: recipe\r\n    else\r\n      render json: recipe.errors\r\n    end\r\n  end\r\n\r\n  def show\r\n  end\r\n\r\n  def destroy\r\n  end\r\n\r\n  private\r\n\r\n  def recipe_params\r\n    params.permit(:name, :image, :ingredients, :instruction)\r\n  end\r\nend<\/code><\/pre>\n<\/div>\n<p>\u5728\u521b\u5efa\u64cd\u4f5c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528ActiveRecord\u7684 <code>create<\/code> \u65b9\u6cd5\u6765\u521b\u5efa\u65b0\u7684\u98df\u8c31\u3002<code>create<\/code> \u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u5c06\u6240\u6709\u63a7\u5236\u5668\u53c2\u6570\u5206\u914d\u7ed9\u6a21\u578b\u3002\u8be5\u65b9\u6cd5\u4f7f\u8bb0\u5f55\u7684\u521b\u5efa\u53d8\u5f97\u7b80\u5355\uff0c\u4f46\u4e5f\u5b58\u5728\u6076\u610f\u4f7f\u7528\u7684\u53ef\u80fd\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Rails\u63d0\u4f9b\u7684\u5f3a\u53c2\u6570\u529f\u80fd\u6765\u9632\u6b62\u6076\u610f\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u53ea\u6709\u88ab\u5141\u8bb8\u7684\u53c2\u6570\u624d\u80fd\u88ab\u5206\u914d\u3002\u60a8\u5728\u4ee3\u7801\u4e2d\u5c06\u4e00\u4e2a <code>recipe_params<\/code> \u53c2\u6570\u4f20\u9012\u7ed9 <code>create<\/code> \u65b9\u6cd5\u3002<code>recipe_params<\/code> \u662f\u4e00\u4e2a\u79c1\u6709\u65b9\u6cd5\uff0c\u5728\u5176\u4e2d\u5141\u8bb8\u63a7\u5236\u5668\u53c2\u6570\uff0c\u4ee5\u9632\u6b62\u9519\u8bef\u6216\u6076\u610f\u5185\u5bb9\u8fdb\u5165\u60a8\u7684\u6570\u636e\u5e93\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5141\u8bb8\u4f7f\u7528 <code>name<\/code>\u3001<code>image<\/code>\u3001<code>ingredients<\/code> \u548c <code>instruction<\/code> \u53c2\u6570\u6765\u6b63\u786e\u4f7f\u7528 <code>create<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u60a8\u7684\u914d\u65b9\u63a7\u5236\u5668\u73b0\u5728\u53ef\u4ee5\u8bfb\u53d6\u548c\u521b\u5efa\u914d\u65b9\u4e86\u3002\u5269\u4e0b\u7684\u5c31\u662f\u8bfb\u53d6\u548c\u5220\u9664\u5355\u4e2a\u914d\u65b9\u7684\u903b\u8f91\u3002\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u66f4\u65b0\u60a8\u7684\u914d\u65b9\u63a7\u5236\u5668\uff1a<\/p>\n<div>\n<pre><code>class Api::V1::RecipesController < ApplicationController\r\n  before_action :set_recipe, only: %i[show destroy]\r\n\r\n  def index\r\n    recipe = Recipe.all.order(created_at: :desc)\r\n    render json: recipe\r\n  end\r\n\r\n  def create\r\n    recipe = Recipe.create!(recipe_params)\r\n    if recipe\r\n      render json: recipe\r\n    else\r\n      render json: recipe.errors\r\n    end\r\n  end\r\n\r\n  def show\r\n    render json: @recipe\r\n  end\r\n\r\n  def destroy\r\n    @recipe&#038;.destroy\r\n    render json: { message: '\u98df\u8c31\u5df2\u5220\u9664\uff01' }\r\n  end\r\n\r\n  private\r\n\r\n  def recipe_params\r\n    params.permit(:name, :image, :ingredients, :instruction)\r\n  end\r\n\r\n  def set_recipe\r\n    @recipe = Recipe.find(params[:id])\r\n  end\r\nend<\/code><\/pre>\n<\/div>\n<p>\u5728\u65b0\u7684\u4ee3\u7801\u884c\u4e2d\uff0c\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u79c1\u6709\u7684 <code>set_recipe<\/code> \u65b9\u6cd5\uff0c\u5728show\u548cdelete\u64cd\u4f5c\u5339\u914d\u8bf7\u6c42\u65f6\u7531 <code>before_action<\/code> \u8c03\u7528\u3002<code>set_recipe<\/code> \u65b9\u6cd5\u4f7f\u7528ActiveRecord\u7684 <code>find<\/code> \u65b9\u6cd5\u627e\u5230\u4e00\u4e2aid\u4e0eparams\u4e2d\u63d0\u4f9b\u7684id\u5339\u914d\u7684recipe\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u5b9e\u4f8b\u53d8\u91cf <code>@recipe<\/code>\u3002\u5728show\u64cd\u4f5c\u4e2d\uff0c\u4f60\u5c06\u7531 <code>set_recipe<\/code> \u65b9\u6cd5\u8bbe\u7f6e\u7684 <code>@recipe<\/code> \u5bf9\u8c61\u4f5c\u4e3aJSON\u54cd\u5e94\u8fd4\u56de\u3002<\/p>\n<p>\u5728\u9500\u6bc1\u64cd\u4f5c\u4e2d\uff0c\u4f60\u4f7f\u7528\u4e86Ruby\u7684\u5b89\u5168\u5bfc\u822a\u8fd0\u7b97\u7b26 <code>&.<\/code>\uff0c\u5b83\u5728\u8c03\u7528\u65b9\u6cd5\u65f6\u907f\u514d\u4e86\u7a7a\u503c\u9519\u8bef\u3002\u8fd9\u4e2a\u9644\u52a0\u529f\u80fd\u5141\u8bb8\u4f60\u4ec5\u5728\u5b58\u5728\u65f6\u624d\u5220\u9664\u4e00\u4e2a\u83dc\u8c31\uff0c\u5e76\u4f5c\u4e3a\u54cd\u5e94\u53d1\u9001\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<p>\u5728\u5bf9 <code>recipes_controller.rb<\/code> \u8fdb\u884c\u8fd9\u4e9b\u66f4\u6539\u4e4b\u540e\uff0c\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u4f60\u4e3a\u4f60\u7684\u98df\u8c31\u521b\u5efa\u4e86\u4e00\u4e2a\u6a21\u578b\u548c\u63a7\u5236\u5668\u3002\u4f60\u5df2\u7ecf\u7f16\u5199\u4e86\u6240\u6709\u4e0e\u540e\u7aef\u98df\u8c31\u64cd\u4f5c\u76f8\u5173\u7684\u903b\u8f91\u3002\u5728\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4e2d\uff0c\u4f60\u5c06\u521b\u5efa\u7ec4\u4ef6\u6765\u67e5\u770b\u4f60\u7684\u98df\u8c31\u3002<\/p>\n<h2>\u7b2c\u4e03\u6b65 - \u6d4f\u89c8\u98df\u8c31<\/h2>\n<p>\u5728\u6b64\u90e8\u5206\uff0c\u60a8\u5c06\u521b\u5efa\u7528\u4e8e\u67e5\u770b\u98df\u8c31\u7684\u7ec4\u4ef6\u3002\u60a8\u5c06\u521b\u5efa\u4e24\u4e2a\u9875\u9762\uff1a\u4e00\u4e2a\u7528\u4e8e\u67e5\u770b\u6240\u6709\u73b0\u6709\u7684\u98df\u8c31\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u67e5\u770b\u5355\u4e2a\u98df\u8c31\u3002<\/p>\n<p>\u9996\u5148\uff0c\u60a8\u5c06\u521b\u5efa\u4e00\u4e2a\u9875\u9762\u6765\u67e5\u770b\u6240\u6709\u7684\u98df\u8c31\u3002\u5728\u521b\u5efa\u9875\u9762\u4e4b\u524d\uff0c\u7531\u4e8e\u60a8\u7684\u6570\u636e\u5e93\u5f53\u524d\u4e3a\u7a7a\uff0c\u60a8\u9700\u8981\u4e00\u4e9b\u98df\u8c31\u4f9b\u60a8\u4f7f\u7528\u3002Rails \u63d0\u4f9b\u4e86\u4e00\u79cd\u4e3a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u79cd\u5b50\u6570\u636e\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u6253\u5f00\u540d\u4e3aseeds.rb\u7684\u79cd\u5b50\u6587\u4ef6\u8fdb\u884c\u7f16\u8f91\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/db\/seeds.rb<\/li><\/ol><\/code><\/pre>\n<p>\u7528\u4ee5\u4e0b\u4ee3\u7801\u66ff\u6362\u79cd\u5b50\u6587\u4ef6\u7684\u521d\u59cb\u5185\u5bb9\u3002<\/p>\n<div>~\/rails_react_recipe\/db\/seeds.rb \u7684\u5185\u5bb9\uff1a<\/div>\n<pre class=\"post-pre\"><code>9.times do |i|\r\n  Recipe.create(\r\n    name: \"\u98df\u8c31 #{i + 1}\",\r\n    ingredients: '227\u514b\u51dd\u8102\u5976\u6cb9\uff0c25\u514b\u9ec4\u6cb9\uff0c1\u8336\u5319\u7389\u7c73\u6dc0\u7c89\uff0c100\u514b\u5e15\u5c14\u9a6c\u5e72\u916a\uff0c\u78e8\u788e\u7684\u8089\u8c46\u853b\uff0c250\u514b\u65b0\u9c9c\u610f\u9762\u6216\u5bbd\u9762\u6761\uff0c\u5207\u788e\u7684\u7ec6\u9999\u8471\u6216\u6b27\u82b9\uff08\u53ef\u9009\uff09',\r\n    instruction: '\u5728\u4e00\u4e2a\u4e2d\u7b49\u5927\u5c0f\u7684\u5e73\u5e95\u9505\u4e2d\uff0c\u7528\u4e2d\u5c0f\u706b\u6405\u62cc\u51dd\u8102\u5976\u6cb9\u3001\u9ec4\u6cb9\u548c\u7389\u7c73\u6dc0\u7c89\uff0c\u716e\u81f3\u5fae\u6cb8\u3002\u5173\u706b\u5e76\u4fdd\u6301\u6e29\u6696\u3002'\r\n  )\r\nend<\/code><\/pre>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f60\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u547d\u4ee4Rails\u521b\u5efa\u4e5d\u4e2a\u914d\u65b9\uff0c\u5305\u62ec\u540d\u79f0\u3001\u914d\u6599\u548c\u6307\u5bfc\u90e8\u5206\u3002\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u4e3a\u4e86\u7528\u8fd9\u4e9b\u6570\u636e\u586b\u5145\u6570\u636e\u5e93\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">rails db:seed<\/li><\/ol><\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u5c06\u4f1a\u5411\u60a8\u7684\u6570\u636e\u5e93\u4e2d\u6dfb\u52a0\u4e5d\u4e2a\u98df\u8c31\u3002\u73b0\u5728\u60a8\u53ef\u4ee5\u83b7\u53d6\u5e76\u5728\u524d\u7aef\u6e32\u67d3\u5b83\u4eec\u3002<\/p>\n<p>\u67e5\u770b\u6240\u6709\u98df\u8c31\u7684\u7ec4\u4ef6\u5c06\u5411 RecipesController \u7684 index \u64cd\u4f5c\u53d1\u51fa HTTP \u8bf7\u6c42\uff0c\u4ee5\u83b7\u53d6\u6240\u6709\u98df\u8c31\u7684\u5217\u8868\u3002\u7136\u540e\u5c06\u5728\u9875\u9762\u4e0a\u4ee5\u5361\u7247\u7684\u5f62\u5f0f\u663e\u793a\u8fd9\u4e9b\u98df\u8c31\u3002<\/p>\n<p>\u5728app\/javascript\/components\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aRecipes.jsx\u6587\u4ef6\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> ~\/rails_react_recipe\/app\/javascript\/components\/Recipes.jsx<\/li><\/ol><\/code><\/pre>\n<p>\u4e00\u65e6\u6587\u4ef6\u6253\u5f00\uff0c\u901a\u8fc7\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5bfc\u5165React\u3001useState\u3001useEffect\u3001Link\u548cuseNavigate\u6a21\u5757\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipes.jsx \u7684\u4ee3\u7801\uff1a<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u5c06\u6240\u793a\u7684\u4ee3\u7801\u884c\u6dfb\u52a0\u5230\u521b\u5efa\u5e76\u5bfc\u51fa\u4e00\u4e2a\u529f\u80fd\u6027\u7684React\u7ec4\u4ef6Recipes\u4e2d\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipes.jsx \u7684\u4ee3\u7801\uff1a<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\n<mark>const Recipes = () => {<\/mark>\r\n  <mark>const navigate = useNavigate();<\/mark>\r\n  <mark>const [recipes, setRecipes] = useState([]);<\/mark>\r\n<mark>};<\/mark>\r\n\r\n<mark>export default Recipes;<\/mark><\/code><\/pre>\n<p>\u5728Recipe\u7ec4\u4ef6\u5185\u90e8\uff0cReact Router\u7684\u5bfc\u822aAPI\u4f1a\u8c03\u7528useNavigate\u94a9\u5b50\u3002React\u7684useState\u94a9\u5b50\u5c06\u521d\u59cb\u5316recipes\u72b6\u6001\uff0c\u8be5\u72b6\u6001\u662f\u4e00\u4e2a\u7a7a\u6570\u7ec4([])\uff0c\u4ee5\u53ca\u4e00\u4e2a\u7528\u4e8e\u66f4\u65b0recipes\u72b6\u6001\u7684setRecipes\u51fd\u6570\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728useEffect\u94a9\u5b50\u4e2d\uff0c\u60a8\u5c06\u53d1\u9001\u4e00\u4e2aHTTP\u8bf7\u6c42\u6765\u83b7\u53d6\u6240\u6709\u7684\u98df\u8c31\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u8bf7\u6dfb\u52a0\u4ee5\u4e0b\u7a81\u51fa\u663e\u793a\u7684\u4ee3\u7801\u884c\uff1a<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipes.jsx \u7684\u4ee3\u7801\uff1a<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\nconst Recipes = () => {\r\n  const navigate = useNavigate();\r\n  const [recipes, setRecipes] = useState([]);\r\n\r\n  <mark>useEffect(() => {<\/mark>\r\n    <mark>const url = \"\/api\/v1\/recipes\/index\";<\/mark>\r\n    <mark>fetch(url)<\/mark>\r\n      <mark>.then((res) => {<\/mark>\r\n        <mark>if (res.ok) {<\/mark>\r\n          <mark>return res.json();<\/mark>\r\n        <mark>}<\/mark>\r\n        <mark>throw new Error(\"\u7f51\u7edc\u54cd\u5e94\u4e0d\u6b63\u5e38\u3002\");<\/mark>\r\n      <mark>})<\/mark>\r\n      <mark>.then((res) => setRecipes(res))<\/mark>\r\n      <mark>.catch(() => navigate(\"\/\"));<\/mark>\r\n  <mark>}, []);<\/mark>\r\n};\r\n\r\nexport default Recipes;<\/code><\/pre>\n<p>\u5728\u4f60\u7684 useEffect hook \u4e2d\uff0c\u4f60\u4f7f\u7528 Fetch API \u53d1\u8d77\u4e00\u4e2a HTTP \u8c03\u7528\u6765\u83b7\u53d6\u6240\u6709\u7684\u98df\u8c31\u3002\u5982\u679c\u54cd\u5e94\u6210\u529f\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u5c06\u98df\u8c31\u6570\u7ec4\u4fdd\u5b58\u5230 recipes \u72b6\u6001\u4e2d\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u5b83\u4f1a\u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230\u4e3b\u9875\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5f53\u7ec4\u4ef6\u88ab\u6e32\u67d3\u65f6\uff0c\u5728\u6d4f\u89c8\u5668\u9875\u9762\u4e0a\u8fd4\u56de\u5c06\u88ab\u8bc4\u4f30\u548c\u663e\u793a\u7684\u5143\u7d20\u7684\u6807\u8bb0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ec4\u4ef6\u5c06\u4ecerecipes\u72b6\u6001\u6e32\u67d3\u4e00\u4e2a\u98df\u8c31\u5361\u7247\u3002\u5c06\u8fd9\u4e9b\u9ad8\u4eae\u884c\u6dfb\u52a0\u5230Recipes.jsx\u4e2d\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipes.jsx \u7684\u4ee3\u7801\uff1a<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\nconst Recipes = () =&gt; {\r\n  const navigate = useNavigate();\r\n  const [recipes, setRecipes] = useState([]);\r\n\r\n  useEffect(() =&gt; {\r\n    const url = \"\/api\/v1\/recipes\/index\";\r\n    fetch(url)\r\n      .then((res) =&gt; {\r\n        if (res.ok) {\r\n          return res.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then((res) =&gt; setRecipes(res))\r\n      .catch(() =&gt; navigate(\"\/\"));\r\n  }, []);\r\n\r\n  <mark>const allRecipes = recipes.map((recipe, index) =&gt; (<\/mark>\r\n    <mark>&lt;div key={index} className=\"col-md-6 col-lg-4\"&gt;<\/mark>\r\n      <mark>&lt;div className=\"card mb-4\"&gt;<\/mark>\r\n        <mark>&lt;img<\/mark>\r\n          <mark>src={recipe.image}<\/mark>\r\n          <mark>className=\"card-img-top\"<\/mark>\r\n          <mark>alt={`${recipe.name} image`}<\/mark>\r\n        <mark>\/&gt;<\/mark>\r\n        <mark>&lt;div className=\"card-body\"&gt;<\/mark>\r\n          <mark>&lt;h5 className=\"card-title\"&gt;{recipe.name}&lt;\/h5&gt;<\/mark>\r\n          <mark>&lt;Link to={`\/recipe\/${recipe.id}`} className=\"btn custom-button\"&gt;<\/mark>\r\n            <mark>View Recipe<\/mark>\r\n          <mark>&lt;\/Link&gt;<\/mark>\r\n        <mark>&lt;\/div&gt;<\/mark>\r\n      <mark>&lt;\/div&gt;<\/mark>\r\n    <mark>&lt;\/div&gt;<\/mark>\r\n  <mark>));<\/mark>\r\n  <mark>const noRecipe = (<\/mark>\r\n    <mark>&lt;div className=\"vw-100 vh-50 d-flex align-items-center justify-content-center\"&gt;<\/mark>\r\n      <mark>&lt;h4&gt;<\/mark>\r\n        <mark>No recipes yet. Why not &lt;Link to=\"\/new_recipe\"&gt;create one&lt;\/Link&gt;<\/mark>\r\n      <mark>&lt;\/h4&gt;<\/mark>\r\n    <mark>&lt;\/div&gt;<\/mark>\r\n  <mark>);<\/mark>\r\n\r\n  <mark>return (<\/mark>\r\n    <mark>&lt;&gt;<\/mark>\r\n      <mark>&lt;section className=\"jumbotron jumbotron-fluid text-center\"&gt;<\/mark>\r\n        <mark>&lt;div className=\"container py-5\"&gt;<\/mark>\r\n          <mark>&lt;h1 className=\"display-4\"&gt;Recipes for every occasion&lt;\/h1&gt;<\/mark>\r\n          <mark>&lt;p className=\"lead text-muted\"&gt;<\/mark>\r\n            <mark>We\u2019ve pulled together our most popular recipes, our latest<\/mark>\r\n            <mark>additions, and our editor\u2019s picks, so there\u2019s sure to be something<\/mark>\r\n            <mark>tempting for you to try.<\/mark>\r\n          <mark>&lt;\/p&gt;<\/mark>\r\n        <mark>&lt;\/div&gt;<\/mark>\r\n      <mark>&lt;\/section&gt;<\/mark>\r\n      <mark>&lt;div className=\"py-5\"&gt;<\/mark>\r\n        <mark>&lt;main className=\"container\"&gt;<\/mark>\r\n          <mark>&lt;div className=\"text-end mb-3\"&gt;<\/mark>\r\n            <mark>&lt;Link to=\"\/recipe\" className=\"btn custom-button\"&gt;<\/mark>\r\n              <mark>Create New Recipe<\/mark>\r\n            <mark>&lt;\/Link&gt;<\/mark>\r\n          <mark>&lt;\/div&gt;<\/mark>\r\n          <mark>&lt;div className=\"row\"&gt;<\/mark>\r\n            <mark>{recipes.length &gt; 0 ? allRecipes : noRecipe}<\/mark>\r\n          <mark>&lt;\/div&gt;<\/mark>\r\n          <mark>&lt;Link to=\"\/\" className=\"btn btn-link\"&gt;<\/mark>\r\n            <mark>Home<\/mark>\r\n          <mark>&lt;\/Link&gt;<\/mark>\r\n        <mark>&lt;\/main&gt;<\/mark>\r\n      <mark>&lt;\/div&gt;<\/mark>\r\n    <mark>&lt;\/&gt;<\/mark>\r\n  <mark>);<\/mark>\r\n};\r\n\r\nexport default Recipes;\r\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u5e76\u9000\u51faRecipes.jsx\u3002<\/p>\n<p>\u65e2\u7136\u4f60\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u7528\u4e8e\u663e\u793a\u6240\u6709\u98df\u8c31\u7684\u7ec4\u4ef6\uff0c\u73b0\u5728\u4f60\u9700\u8981\u4e3a\u5b83\u521b\u5efa\u4e00\u4e2a\u8def\u7531\u3002\u6253\u5f00\u524d\u7aef\u8def\u7531\u6587\u4ef6app\/javascript\/routes\/index.jsx\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> app\/javascript\/routes\/index.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u88ab\u7a81\u51fa\u663e\u793a\u7684\u884c\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/routes\/index.jsx \u8bf7\u539f\u751f\u5730\u7528\u4e2d\u6587\u6539\u5199\u51fa\u6765\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u9009\u9879\uff1a<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport { BrowserRouter as Router, Routes, Route } from \"react-router-dom\";\r\nimport Home from \"..\/components\/Home\";\r\n<mark>import Recipes from \"..\/components\/Recipes\";<\/mark>\r\n\r\nexport default (\r\n  &lt;Router&gt;\r\n    &lt;Routes&gt;\r\n      &lt;Route path=\"\/\" exact component={Home} \/&gt;\r\n      <mark>&lt;Route path=\"\/recipes\" element={&lt;Recipes \/&gt;} \/&gt;<\/mark>\r\n    &lt;\/Routes&gt;\r\n  &lt;\/Router&gt;\r\n);\r\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u9a8c\u8bc1\u4ee3\u7801\u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002\u5c31\u50cf\u4e4b\u524d\u4e00\u6837\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8\u4f60\u7684\u670d\u52a1\u5668\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">bin\/dev\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u7136\u540e\u5728\u60a8\u7684\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u8be5\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u4e3b\u9875\u4e0a\u6309\u4e0b\u201c\u67e5\u770b\u98df\u8c31\u201d\u6309\u94ae\uff0c\u5373\u53ef\u8fdb\u5165\u4e00\u4e2a\u5305\u542b\u60a8\u7684\u79cd\u5b50\u98df\u8c31\u7684\u5c55\u793a\u9875\u9762\u3002<\/p>\n<div>\n                            <img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/249-0.png\" class='post-images' alt=\"Screencapture with the seed recipes page\" title=\"\"><\/p>\n<p>\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528CTRL+C\u505c\u6b62\u670d\u52a1\u5668\u5e76\u8fd4\u56de\u63d0\u793a\u7b26\u3002<\/p>\n<p>\u65e2\u7136\u4f60\u5df2\u7ecf\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u67e5\u770b\u6240\u6709\u7684\u98df\u8c31\uff0c\u73b0\u5728\u662f\u65f6\u5019\u521b\u5efa\u4e00\u4e2a\u7b2c\u4e8c\u4e2a\u7ec4\u4ef6\u6765\u67e5\u770b\u5355\u4e2a\u98df\u8c31\u4e86\u3002\u5728app\/javascript\/components\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aRecipe.jsx\u6587\u4ef6\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> app\/javascript\/components\/Recipe.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u4e0e\u201cRecipes\u201d\u7ec4\u4ef6\u4e00\u6837\uff0c\u901a\u8fc7\u6dfb\u52a0\u4ee5\u4e0b\u884c\u6765\u5bfc\u5165React\u3001useState\u3001useEffect\u3001Link\u3001useNavigate\u548cuseParam\u6a21\u5757\uff1a<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipe.jsx \u7684\u672c\u5730\u4e2d\u6587\u6f22\u5b57\u5316\u9078\u9805\uff08\u50c5\u9700\u4e00\u500b\uff09\u3002<\/p>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u5c06\u9ad8\u4eae\u90e8\u5206\u6dfb\u52a0\u5230\u4ee3\u7801\u4e2d\uff0c\u521b\u5efa\u5e76\u5bfc\u51fa\u4e00\u4e2a\u53ef\u7528\u7684React\u7ec4\u4ef6\uff0c\u547d\u540d\u4e3aRecipe\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipe.jsx \u53ef\u4ee5\u91cd\u65b0\u8868\u793a\u4e3a\u4ee5\u4e0b\u4e2d\u6587\u65b9\u6848\uff1a<br \/>\n~\/rails_react_recipe\/app\/javascript\/components\/\u83dc\u8c31\u7ec4\u4ef6.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n\r\n<mark>const Recipe = () =&gt; {<\/mark>\r\n  <mark>const params = useParams();<\/mark>\r\n  <mark>const navigate = useNavigate();<\/mark>\r\n  <mark>const [recipe, setRecipe] = useState({ ingredients: \"\" });<\/mark>\r\n<mark>};<\/mark>\r\n\r\n<mark>export default Recipe;<\/mark>\r\n<\/code><\/pre>\n<p>\u4e0e\"\u83dc\u8c31\"\u7ec4\u4ef6\u4e00\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 useNavigate \u94a9\u5b50\u6765\u521d\u59cb\u5316 React \u8def\u7531\u5bfc\u822a\u3002useState \u94a9\u5b50\u4f1a\u4f7f\u7528\u4e00\u4e2a recipe \u72b6\u6001\u548c setRecipe \u51fd\u6570\u6765\u66f4\u65b0\u72b6\u6001\u3002\u53e6\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8c03\u7528 useParams \u94a9\u5b50\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u7684\u952e\/\u503c\u5bf9\u662f URL \u53c2\u6570\u3002<\/p>\n<p>\u4e3a\u4e86\u627e\u5230\u7279\u5b9a\u7684\u98df\u8c31\uff0c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u77e5\u9053\u98df\u8c31\u7684id\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u7684\u98df\u8c31\u7ec4\u4ef6\u9700\u8981\u5728URL\u4e2d\u63a5\u6536\u4e00\u4e2aid\u53c2\u6570\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7params\u5bf9\u8c61\u8bbf\u95ee\u8fd9\u4e2a\u53c2\u6570\uff0c\u8be5\u5bf9\u8c61\u4fdd\u5b58useParams\u94a9\u5b50\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728useEffect\u94a9\u5b50\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u901a\u8fc7params\u5bf9\u8c61\u83b7\u53d6id\u53c2\u6570\u3002\u4e00\u65e6\u83b7\u53d6\u5230\u98df\u8c31\u7684id\u53c2\u6570\uff0c\u4f60\u5c06\u53d1\u9001\u4e00\u4e2aHTTP\u8bf7\u6c42\u6765\u83b7\u53d6\u8fd9\u4e2a\u98df\u8c31\u3002\u8bf7\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u6dfb\u52a0\u5230\u4f60\u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/\u98df\u8c31.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n\r\nconst Recipe = () =&gt; {\r\n  const params = useParams();\r\n  const navigate = useNavigate();\r\n  const [recipe, setRecipe] = useState({ ingredients: \"\" });\r\n\r\n  <mark>useEffect(() =&gt; {<\/mark>\r\n    <mark>const url = `\/api\/v1\/show\/${params.id}`;<\/mark>\r\n    <mark>fetch(url)<\/mark>\r\n      <mark>.then((response) =&gt; {<\/mark>\r\n        <mark>if (response.ok) {<\/mark>\r\n          <mark>return response.json();<\/mark>\r\n        <mark>}<\/mark>\r\n        <mark>throw new Error(\"Network response was not ok.\");<\/mark>\r\n      <mark>})<\/mark>\r\n      <mark>.then((response) =&gt; setRecipe(response))<\/mark>\r\n      <mark>.catch(() =&gt; navigate(\"\/recipes\"));<\/mark>\r\n  <mark>}, [params.id]);<\/mark>\r\n};\r\n\r\nexport default Recipe;\r\n<\/code><\/pre>\n<p>\u5728useEffect\u94a9\u5b50\u4e2d\uff0c\u4f60\u4f7f\u7528params.id\u7684\u503c\u6765\u53d1\u8d77GET HTTP\u8bf7\u6c42\uff0c\u4ee5\u83b7\u53d6\u62e5\u6709\u76f8\u5e94id\u7684\u98df\u8c31\uff0c\u5e76\u4f7f\u7528setRecipe\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u5230\u7ec4\u4ef6\u72b6\u6001\u4e2d\u3002\u5982\u679c\u98df\u8c31\u4e0d\u5b58\u5728\uff0c\u8be5\u5e94\u7528\u4f1a\u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230\u98df\u8c31\u9875\u9762\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6dfb\u52a0\u4e00\u4e2aaddHtmlEntities\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u7ec4\u4ef6\u4e2d\u7684\u5b57\u7b26\u5b9e\u4f53\u66ff\u6362\u4e3aHTML\u5b9e\u4f53\u3002addHtmlEntities\u51fd\u6570\u5c06\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u6240\u6709\u8f6c\u4e49\u7684\u5f00\u95ed\u62ec\u53f7\u66ff\u6362\u4e3a\u5b83\u4eec\u5bf9\u5e94\u7684HTML\u5b9e\u4f53\u3002\u8fd9\u4e2a\u51fd\u6570\u5c06\u5e2e\u52a9\u60a8\u5c06\u4fdd\u5b58\u5728\u98df\u8c31\u8bf4\u660e\u4e2d\u7684\u4efb\u4f55\u8f6c\u4e49\u5b57\u7b26\u8fdb\u884c\u8f6c\u6362\u3002\u6dfb\u52a0\u4ee5\u4e0b\u7a81\u51fa\u663e\u793a\u7684\u884c\uff1a<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/Recipe.jsx\u7684\u4e2d\u6587\u91ca\u4e49\u662f\u4ec0\u4e48\uff1f<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n\r\nconst Recipe = () =&gt; {\r\n  const params = useParams();\r\n  const navigate = useNavigate();\r\n  const [recipe, setRecipe] = useState({ ingredients: \"\" });\r\n\r\n  useEffect(() =&gt; {\r\n    const url = `\/api\/v1\/show\/${params.id}`;\r\n    fetch(url)\r\n      .then((response) =&gt; {\r\n        if (response.ok) {\r\n          return response.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then((response) =&gt; setRecipe(response))\r\n      .catch(() =&gt; navigate(\"\/recipes\"));\r\n  }, [params.id]);\r\n\r\n  <mark>const addHtmlEntities = (str) =&gt; {<\/mark>\r\n    <mark>return String(str).replace(\/&amp;lt;\/g, \"&lt;\").replace(\/&amp;gt;\/g, \"&gt;\");<\/mark>\r\n  <mark>};<\/mark>\r\n};\r\n\r\nexport default Recipe;\r\n<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u901a\u8fc7\u6dfb\u52a0\u9ad8\u4eae\u884c\u5c06\u6807\u8bb0\u8fd4\u56de\u5230\u9875\u9762\u4e0a\u7684\u7ec4\u4ef6\u72b6\u6001\u4e2d\uff0c\u4ee5\u6e32\u67d3\u914d\u65b9\u3002<\/p>\n<div>~ \/rails_react_recipe\/app\/javascript\/components\/Recipe.jsx \u8fd9\u4e2a\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\u3002<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n\r\nconst Recipe = () => {\r\n  const params = useParams();\r\n  const navigate = useNavigate();\r\n  const [recipe, setRecipe] = useState({ ingredients: \"\" });\r\n\r\n  useEffect(() => {\r\n    const url = `\/api\/v1\/show\/${params.id}`;\r\n    fetch(url)\r\n      .then((response) => {\r\n        if (response.ok) {\r\n          return response.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then((response) => setRecipe(response))\r\n      .catch(() => navigate(\"\/recipes\"));\r\n  }, [params.id]);\r\n\r\n  const addHtmlEntities = (str) => {\r\n    return String(str).replace(\/&amp;lt;\/g, \"&lt;\").replace(\/&amp;gt;\/g, \"&gt;\");\r\n  };\r\n\r\n  <mark>const ingredientList = () => {<\/mark>\r\n    <mark>let ingredientList = \"\u6ca1\u6709\u53ef\u7528\u7684\u98df\u6750\";<\/mark>\r\n\r\n    <mark>if (recipe.ingredients.length > 0) {<\/mark>\r\n      <mark>ingredientList = recipe.ingredients<\/mark>\r\n        <mark>.split(\",\")<\/mark>\r\n        <mark>.map((ingredient, index) => (<\/mark>\r\n          <mark>&lt;li key={index} className=\"list-group-item\"&gt;<\/mark>\r\n            <mark>{ingredient}<\/mark>\r\n          <mark>&lt;\/li&gt;<\/mark>\r\n        <mark>));<\/mark>\r\n    <mark>}<\/mark>\r\n\r\n    <mark>return ingredientList;<\/mark>\r\n  <mark>};<\/mark>\r\n\r\n  <mark>const recipeInstruction = addHtmlEntities(recipe.instruction);<\/mark>\r\n  \r\n  <mark>return (<\/mark>\r\n    <mark>&lt;div className=\"\"&gt;<\/mark>\r\n      <mark>&lt;div className=\"hero position-relative d-flex align-items-center justify-content-center\"&gt;<\/mark>\r\n        <mark>&lt;img<\/mark>\r\n          <mark>src={recipe.image}<\/mark>\r\n          <mark>alt={`${recipe.name} image`}<\/mark>\r\n          <mark>className=\"img-fluid position-absolute\"<\/mark>\r\n        <mark>\/&gt;<\/mark>\r\n        <mark>&lt;div className=\"overlay bg-dark position-absolute\" \/&gt;<\/mark>\r\n        <mark>&lt;h1 className=\"display-4 position-relative text-white\"&gt;<\/mark>\r\n          <mark>{recipe.name}<\/mark>\r\n        <mark>&lt;\/h1&gt;<\/mark>\r\n      <mark>&lt;\/div&gt;<\/mark>\r\n      <mark>&lt;div className=\"container py-5\"&gt;<\/mark>\r\n        <mark>&lt;div className=\"row\"&gt;<\/mark>\r\n          <mark>&lt;div className=\"col-sm-12 col-lg-3\"&gt;<\/mark>\r\n            <mark>&lt;ul className=\"list-group\"&gt;<\/mark>\r\n              <mark>&lt;h5 className=\"mb-2\"&gt;\u98df\u6750&lt;\/h5&gt;<\/mark>\r\n              <mark>{ingredientList()}<\/mark>\r\n            <mark>&lt;\/ul&gt;<\/mark>\r\n          <mark>&lt;\/div&gt;<\/mark>\r\n          <mark>&lt;div className=\"col-sm-12 col-lg-7\"&gt;<\/mark>\r\n            <mark>&lt;h5 className=\"mb-2\"&gt;\u5236\u4f5c\u8bf4\u660e&lt;\/h5&gt;<\/mark>\r\n            <mark>&lt;div<\/mark>\r\n              <mark>dangerouslySetInnerHTML={{<\/mark>\r\n                <mark>__html: `${recipeInstruction}`,<\/mark>\r\n              <mark>}}<\/mark>\r\n            <mark>\/&gt;<\/mark>\r\n          <mark>&lt;\/div&gt;<\/mark>\r\n          <mark>&lt;div className=\"col-sm-12 col-lg-2\"&gt;<\/mark>\r\n            <mark>&lt;button<\/mark>\r\n              <mark>type=\"button\"<\/mark>\r\n              <mark>className=\"btn btn-danger\"<\/mark>\r\n            <mark>&gt;<\/mark>\r\n              <mark>\u5220\u9664\u98df\u8c31<\/mark>\r\n            <mark>&lt;\/button&gt;<\/mark>\r\n          <mark>&lt;\/div&gt;<\/mark>\r\n        <mark>&lt;\/div&gt;<\/mark>\r\n        <mark>&lt;Link to=\"\/recipes\" className=\"btn btn-link\"&gt;<\/mark>\r\n          <mark>\u8fd4\u56de\u98df\u8c31\u5217\u8868<\/mark>\r\n        <mark>&lt;\/Link&gt;<\/mark>\r\n      <mark>&lt;\/div&gt;<\/mark>\r\n    <mark>&lt;\/div&gt;<\/mark>\r\n  <mark>);<\/mark>\r\n};\r\n\r\nexport default Recipe;\r\n<\/code><\/pre>\n<p>\u901a\u8fc7ingredientList\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5c06\u9017\u53f7\u5206\u9694\u7684\u98df\u8c31\u98df\u6750\u5206\u6210\u6570\u7ec4\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u6620\u5c04\u4ee5\u521b\u5efa\u98df\u6750\u5217\u8868\u3002\u5982\u679c\u6ca1\u6709\u98df\u6750\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u663e\u793a\u4e00\u4e2a\u63d0\u793a\u6d88\u606f\uff0c\u8868\u793a\u6ca1\u6709\u53ef\u7528\u7684\u98df\u6750\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u901a\u8fc7addHtmlEntities\u51fd\u6570\u66ff\u6362\u98df\u8c31\u5236\u4f5c\u8bf4\u660e\u4e2d\u7684\u6240\u6709\u5f00\u653e\u548c\u5173\u95ed\u62ec\u53f7\u3002\u6700\u540e\uff0c\u8be5\u4ee3\u7801\u5c06\u98df\u8c31\u56fe\u50cf\u663e\u793a\u4e3a\u4e3b\u8981\u56fe\u50cf\uff0c\u5e76\u5728\u98df\u8c31\u5236\u4f5c\u8bf4\u660e\u65c1\u8fb9\u6dfb\u52a0\u4e00\u4e2a\u5220\u9664\u98df\u8c31\u6309\u94ae\uff0c\u4ee5\u53ca\u4e00\u4e2a\u8fd4\u56de\u98df\u8c31\u5217\u8868\u7684\u6309\u94ae\u3002<\/p>\n<div class=\"post-conf-note\">\n<p class=\"post-note\">\n<p class=\"post-conf-desc\">\u6ce8\u610f\uff1a\u4f7f\u7528React\u7684dangerouslySetInnerHTML\u5c5e\u6027\u662f\u6709\u98ce\u9669\u7684\uff0c\u56e0\u4e3a\u5b83\u4f1a\u4f7f\u4f60\u7684\u5e94\u7528\u66b4\u9732\u7ed9\u8de8\u7ad9\u811a\u672c\u653b\u51fb\u3002\u901a\u8fc7\u786e\u4fdd\u5728\u521b\u5efa\u98df\u8c31\u65f6\u4f7f\u7528NewRecipe\u7ec4\u4ef6\u4e2d\u58f0\u660e\u7684stripHtmlEntities\u51fd\u6570\u66ff\u6362\u8f93\u5165\u7684\u7279\u6b8a\u5b57\u7b26\uff0c\u53ef\u4ee5\u51cf\u5c11\u8fd9\u79cd\u98ce\u9669\u3002<\/p>\n<\/div>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u8981\u5728\u9875\u9762\u4e0a\u67e5\u770b\u98df\u8c31\u7ec4\u4ef6\uff0c\u60a8\u9700\u8981\u5c06\u5176\u6dfb\u52a0\u5230\u60a8\u7684\u8def\u7531\u6587\u4ef6\u4e2d\u3002\u6253\u5f00\u60a8\u7684\u8def\u7531\u6587\u4ef6\u8fdb\u884c\u7f16\u8f91\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> app\/javascript\/routes\/index.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u8bf7\u5c06\u4e0b\u9762\u7684\u6807\u8bb0\u884c\u6dfb\u52a0\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<div>\n<p>\u8bf7\u4e3a\u6211\u5c06\u4ee5\u4e0b\u5185\u5bb9\u4ee5\u4e2d\u6587\u539f\u751f\u65b9\u5f0f\u590d\u8ff0\uff0c\u53ea\u9700\u8981\u4e00\u79cd\u9009\u9879\uff1a<br \/>\n~\/rails_react_recipe\/app\/javascript\/routes\/index.jsx<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport { BrowserRouter as Router, Routes, Route } from \"react-router-dom\";\r\nimport Home from \"..\/components\/Home\";\r\nimport Recipes from \"..\/components\/Recipes\";\r\n<mark>import Recipe from \"..\/components\/Recipe\";<\/mark>\r\n\r\nexport default (\r\n  &lt;Router&gt;\r\n    &lt;Routes&gt;\r\n      &lt;Route path=\"\/\" exact component={Home} \/&gt;\r\n      &lt;Route path=\"\/recipes\" exact component={Recipes} \/&gt;\r\n      <mark>&lt;Route path=\"\/recipe\/:id\" element={&lt;Recipe \/&gt;} \/&gt;<\/mark>\r\n    &lt;\/Routes&gt;\r\n  &lt;\/Router&gt;\r\n);\r\n<\/code><\/pre>\n<p>\u4f60\u9700\u8981\u5728\u8fd9\u4e2a\u8def\u7531\u6587\u4ef6\u4e2d\u5f15\u5165\u4f60\u7684\u98df\u8c31\u7ec4\u4ef6\u5e76\u6dfb\u52a0\u4e00\u4e2a\u8def\u7531\u3002\u6b64\u8def\u7531\u5305\u542b\u4e00\u4e2a:id\u53c2\u6570\uff0c\u5c06\u88ab\u4f60\u60f3\u8981\u67e5\u770b\u7684\u98df\u8c31\u7684id\u66ff\u6362\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/div>\n<p>\u4f7f\u7528bin\/dev\u811a\u672c\u91cd\u65b0\u542f\u52a8\u670d\u52a1\u5668\uff0c\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:3000\u3002\u70b9\u51fb\"\u67e5\u770b\u98df\u8c31\"\u6309\u94ae\uff0c\u8f6c\u5230\u98df\u8c31\u9875\u9762\u3002\u5728\u98df\u8c31\u9875\u9762\u4e2d\uff0c\u901a\u8fc7\u70b9\u51fb\"\u67e5\u770b\u98df\u8c31\"\u6309\u94ae\u8bbf\u95ee\u4efb\u610f\u98df\u8c31\u3002\u4f60\u5c06\u4f1a\u770b\u5230\u4e00\u4e2a\u6839\u636e\u6570\u636e\u5e93\u6570\u636e\u586b\u5145\u7684\u9875\u9762\u3002<\/p>\n<div>\n    <img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/282-0.png\" class='post-images' alt=\"Single Recipe Page\" title=\"\">\n<\/div>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528CTRL+C\u505c\u6b62\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u4f60\u5411\u4f60\u7684\u6570\u636e\u5e93\u6dfb\u52a0\u4e86\u4e5d\u4e2a\u98df\u8c31\uff0c\u5e76\u521b\u5efa\u4e86\u53ef\u4ee5\u5355\u72ec\u67e5\u770b\u8fd9\u4e9b\u98df\u8c31\u548c\u4f5c\u4e3a\u96c6\u5408\u67e5\u770b\u7684\u7ec4\u4ef6\u3002\u5728\u4e0b\u4e00\u6b65\u4e2d\uff0c\u4f60\u5c06\u6dfb\u52a0\u4e00\u4e2a\u521b\u5efa\u98df\u8c31\u7684\u7ec4\u4ef6\u3002<\/p>\n<h2>\u6b65\u9aa48 \u2014\u2014 \u5236\u4f5c\u98df\u8c31<\/h2>\n<p>\u62e5\u6709\u53ef\u7528\u98df\u8c31\u5e94\u7528\u7684\u4e0b\u4e00\u6b65\u662f\u80fd\u591f\u521b\u5efa\u65b0\u7684\u98df\u8c31\u3002\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u60a8\u5c06\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u6b64\u529f\u80fd\u7684\u7ec4\u4ef6\u3002\u8be5\u7ec4\u4ef6\u5c06\u5305\u542b\u4e00\u4e2a\u8868\u5355\uff0c\u7528\u4e8e\u4ece\u7528\u6237\u5904\u6536\u96c6\u6240\u9700\u7684\u98df\u8c31\u8be6\u7ec6\u4fe1\u606f\uff0c\u7136\u540e\u5411\u98df\u8c31\u63a7\u5236\u5668\u4e2d\u7684\u521b\u5efa\u64cd\u4f5c\u53d1\u51fa\u8bf7\u6c42\uff0c\u4fdd\u5b58\u98df\u8c31\u6570\u636e\u3002<\/p>\n<p>\u5728app\/javascript\/components\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2aNewRecipe.jsx\u6587\u4ef6\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> app\/javascript\/components\/NewRecipe.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5728\u65b0\u6587\u4ef6\u4e2d\uff0c\u5bfc\u5165\u4f60\u5728\u5176\u4ed6\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u7684React\u3001useState\u3001Link\u548cuseNavigate\u6a21\u5757\u3002<\/p>\n<div>\u00b7 ~\/rails_react_recipe\/app\/javascript\/components\/NewRecipe.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u6dfb\u52a0\u7a81\u51fa\u663e\u793a\u7684\u884c\u6765\u521b\u5efa\u548c\u5bfc\u51fa\u4e00\u4e2a\u529f\u80fd\u6027\u7684NewRecipe\u7ec4\u4ef6\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/NewRecipe.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\n<mark>const NewRecipe = () => {<\/mark>\r\n  <mark>const navigate = useNavigate();<\/mark>\r\n  <mark>const [name, setName] = useState(\"\");<\/mark>\r\n  <mark>const [ingredients, setIngredients] = useState(\"\");<\/mark>\r\n  <mark>const [instruction, setInstruction] = useState(\"\");<\/mark>\r\n<mark>};<\/mark>\r\n\r\n<mark>export default NewRecipe;<\/mark>\r\n<\/code><\/pre>\n<p>\u4e0e\u4e4b\u524d\u7684\u7ec4\u4ef6\u4e00\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528useNavigate\u94a9\u5b50\u6765\u521d\u59cb\u5316React\u8def\u7531\u5bfc\u822a\uff0c\u7136\u540e\u4f7f\u7528useState\u94a9\u5b50\u6765\u521d\u59cb\u5316\u540d\u79f0\u3001\u914d\u6599\u548c\u6307\u5bfc\u8bf4\u660e\u7684\u72b6\u6001\uff0c\u5e76\u5206\u522b\u4f7f\u7528\u5176\u5bf9\u5e94\u7684\u66f4\u65b0\u51fd\u6570\u3002\u8fd9\u4e9b\u662f\u60a8\u9700\u8981\u521b\u5efa\u6709\u6548\u7684\u98df\u8c31\u6240\u9700\u7684\u5b57\u6bb5\u3002<\/p>\n<p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2astripHtmlEntities\u51fd\u6570\uff0c\u5c06\u7279\u6b8a\u5b57\u7b26\uff08\u5982<\uff09\u8f6c\u6362\u4e3a\u5176\u8f6c\u4e49\/\u7f16\u7801\u503c\uff08\u5982&lt;\uff09\u3002\u4e3a\u6b64\uff0c\u5c06\u4ee5\u4e0b\u7a81\u51fa\u663e\u793a\u7684\u884c\u6dfb\u52a0\u5230NewRecipe\u7ec4\u4ef6\u4e2d\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/NewRecipe.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\nconst NewRecipe = () => {\r\n  const navigate = useNavigate();\r\n  const [name, setName] = useState(\"\");\r\n  const [ingredients, setIngredients] = useState(\"\");\r\n  const [instruction, setInstruction] = useState(\"\");\r\n\r\n  <mark>const stripHtmlEntities = (str) => {<\/mark>\r\n    <mark>return String(str)<\/mark>\r\n      <mark>.replace(\/\\n\/g, \"&lt;br&gt; &lt;br&gt;\")<\/mark>\r\n      <mark>.replace(\/&lt;\/g, \"&amp;lt;\")<\/mark>\r\n      <mark>.replace(\/&gt;\/g, \"&amp;gt;\");<\/mark>\r\n  <mark>};<\/mark>\r\n};\r\n\r\nexport default NewRecipe;\r\n<\/code><\/pre>\n<p>\u5728stripHtmlEntities\u51fd\u6570\u4e2d\uff0c\u4f60\u7528\u5b83\u4eec\u7684\u8f6c\u4e49\u503c\u66ff\u6362\u4e86<\u548c>\u5b57\u7b26\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u4e0d\u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u539f\u59cb\u7684HTML\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5c06\u4ee5\u4e0b\u9ad8\u4eae\u4ee3\u7801\u6dfb\u52a0\u5230NewRecipe\u7ec4\u4ef6\u4e2d\uff0c\u4ee5\u5904\u7406\u8868\u5355\u7684\u7f16\u8f91\u548c\u63d0\u4ea4\u529f\u80fd\uff1a<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/NewRecipe.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\nconst NewRecipe = () => {\r\n  const navigate = useNavigate();\r\n  const [name, setName] = useState(\"\");\r\n  const [ingredients, setIngredients] = useState(\"\");\r\n  const [instruction, setInstruction] = useState(\"\");\r\n\r\n  const stripHtmlEntities = (str) => {\r\n    return String(str)\r\n      .replace(\/\\n\/g, \"&lt;br&gt; &lt;br&gt;\")\r\n      .replace(\/&lt;\/g, \"&amp;lt;\")\r\n      .replace(\/&gt;\/g, \"&amp;gt;\");\r\n  };\r\n\r\n  <mark>const onChange = (event, setFunction) => {<\/mark>\r\n    <mark>setFunction(event.target.value);<\/mark>\r\n  <mark>};<\/mark>\r\n\r\n  <mark>const onSubmit = (event) => {<\/mark>\r\n    <mark>event.preventDefault();<\/mark>\r\n    <mark>const url = \"\/api\/v1\/recipes\/create\";<\/mark>\r\n\r\n    <mark>if (name.length == 0 || ingredients.length == 0 || instruction.length == 0)<\/mark>\r\n      <mark>return;<\/mark>\r\n\r\n    <mark>const body = {<\/mark>\r\n      <mark>name,<\/mark>\r\n      <mark>ingredients,<\/mark>\r\n      <mark>instruction: stripHtmlEntities(instruction),<\/mark>\r\n    <mark>};<\/mark>\r\n\r\n    <mark>const token = document.querySelector('meta[name=\"csrf-token\"]').content;<\/mark>\r\n    <mark>fetch(url, {<\/mark>\r\n      <mark>method: \"POST\",<\/mark>\r\n      <mark>headers: {<\/mark>\r\n        <mark>\"X-CSRF-Token\": token,<\/mark>\r\n        <mark>\"Content-Type\": \"application\/json\",<\/mark>\r\n      <mark>},<\/mark>\r\n      <mark>body: JSON.stringify(body),<\/mark>\r\n    <mark>})<\/mark>\r\n      <mark>.then((response) => {<\/mark>\r\n        <mark>if (response.ok) {<\/mark>\r\n          <mark>return response.json();<\/mark>\r\n        <mark>}<\/mark>\r\n        <mark>throw new Error(\"Network response was not ok.\");<\/mark>\r\n      <mark>})<\/mark>\r\n      <mark>.then((response) => navigate(`\/recipe\/${response.id}`))<\/mark>\r\n      <mark>.catch((error) => console.log(error.message));<\/mark>\r\n  <mark>};<\/mark>\r\n};\r\n\r\nexport default NewRecipe;\r\n<\/code><\/pre>\n<p>onChange\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u4e8b\u4ef6\u548c\u72b6\u6001\u8bbe\u7f6e\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528\u7528\u6237\u8f93\u5165\u7684\u503c\u66f4\u65b0\u72b6\u6001\u3002\u5728onSubmit\u51fd\u6570\u4e2d\uff0c\u60a8\u68c0\u67e5\u6240\u6709\u5fc5\u586b\u8f93\u5165\u6846\u4e0d\u80fd\u4e3a\u7a7a\u3002\u7136\u540e\uff0c\u60a8\u6784\u5efa\u4e00\u4e2a\u5305\u542b\u521b\u5efa\u65b0\u98df\u8c31\u6240\u9700\u53c2\u6570\u7684\u5bf9\u8c61\u3002\u4f7f\u7528stripHtmlEntities\u51fd\u6570\uff0c\u60a8\u5c06\u98df\u8c31\u8bf4\u660e\u4e2d\u7684<\u548c>\u5b57\u7b26\u66ff\u6362\u4e3a\u5b83\u4eec\u7684\u8f6c\u4e49\u503c\uff0c\u5e76\u5c06\u6bcf\u4e2a\u6362\u884c\u5b57\u7b26\u66ff\u6362\u4e3a\u6362\u884c\u6807\u7b7e\uff0c\u4ee5\u4fdd\u7559\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\u683c\u5f0f\u3002\u6700\u540e\uff0c\u60a8\u53d1\u9001POST HTTP\u8bf7\u6c42\u6765\u521b\u5efa\u65b0\u98df\u8c31\uff0c\u5e76\u5728\u6210\u529f\u54cd\u5e94\u65f6\u91cd\u5b9a\u5411\u5230\u5176\u9875\u9762\u3002<\/p>\n<p>\u4e3a\u4e86\u9632\u6b62\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff08CSRF\uff09\u653b\u51fb\uff0cRails\u4f1a\u5c06\u4e00\u4e2aCSRF\u5b89\u5168\u6807\u8bb0\u9644\u52a0\u5230HTML\u6587\u6863\u4e0a\u3002\u53ea\u8981\u53d1\u8d77\u4e00\u4e2a\u975eGET\u8bf7\u6c42\uff0c\u5c31\u9700\u8981\u8fd9\u4e2a\u6807\u8bb0\u3002\u901a\u8fc7\u4e4b\u524d\u4ee3\u7801\u4e2d\u7684\u6807\u8bb0\uff0c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u4e0a\u9a8c\u8bc1\u6807\u8bb0\uff0c\u5e76\u5728\u5b89\u5168\u6807\u8bb0\u4e0e\u9884\u671f\u4e0d\u7b26\u65f6\u629b\u51fa\u5f02\u5e38\u3002\u5728onSubmit\u51fd\u6570\u4e2d\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u4eceRails\u5d4c\u5165\u5728HTML\u6587\u6863\u4e2d\u7684CSRF\u6807\u8bb0\u4e2d\u68c0\u7d22\u51fa\u6807\u8bb0\u5e76\u53d1\u9001\u4e00\u4e2a\u5e26\u6709JSON\u5b57\u7b26\u4e32\u7684HTTP\u8bf7\u6c42\u3002\u5982\u679c\u6210\u529f\u521b\u5efa\u4e86\u8be5\u98df\u8c31\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230\u98df\u8c31\u9875\u9762\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u90a3\u91cc\u67e5\u770b\u4ed6\u4eec\u65b0\u521b\u5efa\u7684\u98df\u8c31\u3002<\/p>\n<p>\u6700\u540e\uff0c\u8fd4\u56de\u4e00\u4e2a\u6807\u8bb0\uff0c\u7528\u4e8e\u6e32\u67d3\u4e00\u4e2a\u8868\u5355\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u60f3\u8981\u521b\u5efa\u7684\u83dc\u8c31\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u6dfb\u52a0\u9ad8\u4eae\u7684\u884c\uff1a<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/NewRecipe.jsx \u7684\u4e2d\u6587\u672c\u5730\u5316\u91ca\u4e49\uff1a\u65b0\u98df\u8c31\u7ec4\u4ef6\u3002<\/div>\n<pre class=\"post-pre\"><code>import React, { useState } from \"react\";\r\nimport { Link, useNavigate } from \"react-router-dom\";\r\n\r\nconst NewRecipe = () => {\r\n  const navigate = useNavigate();\r\n  const [name, setName] = useState(\"\");\r\n  const [ingredients, setIngredients] = useState(\"\");\r\n  const [instruction, setInstruction] = useState(\"\");\r\n\r\n  const stripHtmlEntities = (str) => {\r\n    return String(str)\r\n      .replace(\/\\n\/g, \"&lt;br&gt; &lt;br&gt;\")\r\n      .replace(\/&lt;\/g, \"&amp;lt;\")\r\n      .replace(\/&gt;\/g, \"&amp;gt;\");\r\n  };\r\n\r\n  const onChange = (event, setFunction) => {\r\n    setFunction(event.target.value);\r\n  };\r\n\r\n  const onSubmit = (event) => {\r\n    event.preventDefault();\r\n    const url = \"\/api\/v1\/recipes\/create\";\r\n\r\n    if (name.length == 0 || ingredients.length == 0 || instruction.length == 0)\r\n      return;\r\n\r\n    const body = {\r\n      name,\r\n      ingredients,\r\n      instruction: stripHtmlEntities(instruction),\r\n    };\r\n\r\n    const token = document.querySelector('meta[name=\"csrf-token\"]').content;\r\n    fetch(url, {\r\n      method: \"POST\",\r\n      headers: {\r\n        \"X-CSRF-Token\": token,\r\n        \"Content-Type\": \"application\/json\",\r\n      },\r\n      body: JSON.stringify(body),\r\n    })\r\n      .then((response) => {\r\n        if (response.ok) {\r\n          return response.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then((response) => navigate(`\/recipe\/${response.id}`))\r\n      .catch((error) => console.log(error.message));\r\n  };\r\n\r\n  <mark>return (<\/mark>\r\n    <mark>&lt;div className=\"container mt-5\"&gt;<\/mark>\r\n      <mark>&lt;div className=\"row\"&gt;<\/mark>\r\n        <mark>&lt;div className=\"col-sm-12 col-lg-6 offset-lg-3\"&gt;<\/mark>\r\n          <mark>&lt;h1 className=\"font-weight-normal mb-5\"&gt;<\/mark>\r\n            <mark>\u5411\u6211\u4eec\u7cbe\u5f69\u7684\u98df\u8c31\u96c6\u5408\u6dfb\u52a0\u4e00\u4e2a\u65b0\u98df\u8c31\u3002<\/mark>\r\n          <mark>&lt;\/h1&gt;<\/mark>\r\n          <mark>&lt;form onSubmit={onSubmit}&gt;<\/mark>\r\n            <mark>&lt;div className=\"form-group\"&gt;<\/mark>\r\n              <mark>&lt;label htmlFor=\"recipeName\"&gt;\u98df\u8c31\u540d\u79f0&lt;\/label&gt;<\/mark>\r\n              <mark>&lt;input<\/mark>\r\n                <mark>type=\"text\"<\/mark>\r\n                <mark>name=\"name\"<\/mark>\r\n                <mark>id=\"recipeName\"<\/mark>\r\n                <mark>className=\"form-control\"<\/mark>\r\n                <mark>required<\/mark>\r\n                <mark>onChange={(event) =&gt; onChange(event, setName)}<\/mark>\r\n              <mark>\/&gt;<\/mark>\r\n            <mark>&lt;\/div&gt;<\/mark>\r\n            <mark>&lt;div className=\"form-group\"&gt;<\/mark>\r\n              <mark>&lt;label htmlFor=\"recipeIngredients\"&gt;\u98df\u6750&lt;\/label&gt;<\/mark>\r\n              <mark>&lt;input<\/mark>\r\n                <mark>type=\"text\"<\/mark>\r\n                <mark>name=\"ingredients\"<\/mark>\r\n                <mark>id=\"recipeIngredients\"<\/mark>\r\n                <mark>className=\"form-control\"<\/mark>\r\n                <mark>required<\/mark>\r\n                <mark>onChange={(event) =&gt; onChange(event, setIngredients)}<\/mark>\r\n              <mark>\/&gt;<\/mark>\r\n              <mark>&lt;small id=\"ingredientsHelp\" className=\"form-text text-muted\"&gt;<\/mark>\r\n                <mark>\u8bf7\u7528\u9017\u53f7\u5206\u9694\u6bcf\u79cd\u98df\u6750\u3002<\/mark>\r\n              <mark>&lt;\/small&gt;<\/mark>\r\n            <mark>&lt;\/div&gt;<\/mark>\r\n            <mark>&lt;label htmlFor=\"instruction\"&gt;\u5236\u4f5c\u8bf4\u660e&lt;\/label&gt;<\/mark>\r\n            <mark>&lt;textarea<\/mark>\r\n              <mark>className=\"form-control\"<\/mark>\r\n              <mark>id=\"instruction\"<\/mark>\r\n              <mark>name=\"instruction\"<\/mark>\r\n              <mark>rows=\"5\"<\/mark>\r\n              <mark>required<\/mark>\r\n              <mark>onChange={(event) =&gt; onChange(event, setInstruction)}<\/mark>\r\n            <mark>\/&gt;<\/mark>\r\n            <mark>&lt;button type=\"submit\" className=\"btn custom-button mt-3\"&gt;<\/mark>\r\n              <mark>\u521b\u5efa\u98df\u8c31<\/mark>\r\n            <mark>&lt;\/button&gt;<\/mark>\r\n            <mark>&lt;Link to=\"\/recipes\" className=\"btn btn-link mt-3\"&gt;<\/mark>\r\n              <mark>\u8fd4\u56de\u98df\u8c31\u5217\u8868<\/mark>\r\n            <mark>&lt;\/Link&gt;<\/mark>\r\n          <mark>&lt;\/form&gt;<\/mark>\r\n        <mark>&lt;\/div&gt;<\/mark>\r\n      <mark>&lt;\/div&gt;<\/mark>\r\n    <mark>&lt;\/div&gt;<\/mark>\r\n  <mark>);<\/mark>\r\n};\r\n\r\nexport default NewRecipe;\r\n<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u6807\u8bb0\u5305\u62ec\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u8f93\u5165\u5b57\u6bb5\u7684\u8868\u5355\uff1b\u5176\u4e2d\u4e00\u4e2a\u7528\u4e8e\u914d\u65b9\u540d\u79f0\uff08recipeName\uff09\uff0c\u4e00\u4e2a\u7528\u4e8e\u914d\u65b9\u6210\u5206\uff08recipeIngredients\uff09\uff0c\u4e00\u4e2a\u7528\u4e8e\u6307\u4ee4\uff08instruction\uff09\u3002\u6bcf\u4e2a\u8f93\u5165\u5b57\u6bb5\u90fd\u6709\u4e00\u4e2aonChange\u4e8b\u4ef6\u5904\u7406\u5668\uff0c\u8c03\u7528onChange\u51fd\u6570\u3002\u8fd8\u6709\u4e00\u4e2aonSubmit\u4e8b\u4ef6\u5904\u7406\u5668\u9644\u52a0\u5230\u63d0\u4ea4\u6309\u94ae\u4e0a\uff0c\u5e76\u8c03\u7528\u63d0\u4ea4\u8868\u5355\u6570\u636e\u7684onSubmit\u51fd\u6570\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u4e3a\u4e86\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u6b64\u7ec4\u4ef6\uff0c\u8bf7\u5728\u60a8\u7684\u8def\u7531\u6587\u4ef6\u4e2d\u66f4\u65b0\u5b83\u7684\u8def\u7531\u8bbe\u7f6e\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> app\/javascript\/routes\/index.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u66f4\u65b0\u60a8\u7684\u8def\u7531\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u9ad8\u4eae\u663e\u793a\u7684\u4ee3\u7801\u884c\u3002<\/p>\n<div>\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u5185\u5bb9\u66f4\u65b0\u6587\u4ef6\uff1a~\/rails_react_recipe\/app\/javascript\/routes\/index.jsx<\/p>\n<pre class=\"post-pre\"><code>import React from \"react\";\r\nimport { BrowserRouter as Router, Routes, Route } from \"react-router-dom\";\r\nimport Home from \"..\/components\/Home\";\r\nimport Recipes from \"..\/components\/Recipes\";\r\nimport Recipe from \"..\/components\/Recipe\";\r\n<mark>import NewRecipe from \"..\/components\/NewRecipe\";<\/mark>\r\n\r\nexport default (\r\n  &lt;Router&gt;\r\n    &lt;Routes&gt;\r\n      &lt;Route path=\"\/\" exact component={Home} \/&gt;\r\n      &lt;Route path=\"\/recipes\" exact component={Recipes} \/&gt;\r\n      &lt;Route path=\"\/recipe\/:id\" exact component={Recipe} \/&gt;\r\n      <mark>&lt;Route path=\"\/recipe\" element={&lt;NewRecipe \/&gt;} \/&gt;<\/mark>\r\n    &lt;\/Routes&gt;\r\n  &lt;\/Router&gt;\r\n);\r\n<\/code><\/pre>\n<\/div>\n<p>\u8bbe\u7f6e\u597d\u8def\u7531\u540e\uff0c\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u91cd\u542f\u60a8\u7684\u5f00\u53d1\u670d\u52a1\u5668\uff0c\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:3000\u3002\u5bfc\u822a\u5230\u98df\u8c31\u9875\u9762\uff0c\u70b9\u51fb\"\u521b\u5efa\u65b0\u98df\u8c31\"\u6309\u94ae\u3002\u60a8\u5c06\u8df3\u8f6c\u5230\u4e00\u4e2a\u5305\u542b\u8868\u5355\u7684\u9875\u9762\uff0c\u8be5\u8868\u5355\u7528\u4e8e\u5411\u6570\u636e\u5e93\u6dfb\u52a0\u98df\u8c31\u3002<\/p>\n<div>\n                            <img decoding=\"async\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/317-0.png\" class='post-images' alt=\"\u521b\u5efa\u98df\u8c31\u9875\u9762\" title=\"\">\n                        <\/div>\n<p>\u8f93\u5165\u6240\u9700\u7684\u98df\u8c31\u8be6\u7ec6\u4fe1\u606f\uff0c\u7136\u540e\u70b9\u51fb\"\u521b\u5efa\u98df\u8c31\"\u6309\u94ae\u3002\u65b0\u521b\u5efa\u7684\u98df\u8c31\u5c06\u663e\u793a\u5728\u9875\u9762\u4e0a\u3002\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u60a8\u4e3a\u98df\u8c31\u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u4e86\u521b\u5efa\u98df\u8c31\u7684\u529f\u80fd\u3002\u4e0b\u4e00\u6b65\uff0c\u60a8\u5c06\u6dfb\u52a0\u5220\u9664\u98df\u8c31\u7684\u529f\u80fd\u3002<\/p>\n<h2>\u7b2c\u4e5d\u6b65\u2014\u2014\u5220\u9664\u98df\u8c31<\/h2>\n<p>\u5728\u672c\u90e8\u5206\u4e2d\uff0c\u60a8\u5c06\u4fee\u6539\u98df\u8c31\u7ec4\u4ef6\uff0c\u4ee5\u5305\u542b\u5220\u9664\u98df\u8c31\u7684\u9009\u9879\u3002\u5f53\u60a8\u5728\u98df\u8c31\u9875\u9762\u4e0a\u70b9\u51fb\u5220\u9664\u6309\u94ae\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u5c06\u5411\u6570\u636e\u5e93\u53d1\u9001\u5220\u9664\u8be5\u98df\u8c31\u7684\u8bf7\u6c42\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6253\u5f00\u60a8\u7684Recipe.jsx\u6587\u4ef6\u8fdb\u884c\u7f16\u8f91\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> app\/javascript\/components\/Recipe.jsx\r\n<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u5728Recipe\u7ec4\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5305\u542b\u9ad8\u4eae\u4ee3\u7801\u884c\u7684deleteRecipe\u51fd\u6570\u3002<\/p>\n<div>\u4ee5\u4e0b\u662f~\/rails_react_recipe\/app\/javascript\/components\/Recipe.jsx\u6587\u4ef6\u7684\u5185\u5bb9\uff1a<\/p>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n\r\nconst Recipe = () =&gt; {\r\n  const params = useParams();\r\n  const navigate = useNavigate();\r\n  const [recipe, setRecipe] = useState({ ingredients: \"\" });\r\n\r\n  useEffect(() =&gt; {\r\n    const url = `\/api\/v1\/show\/${params.id}`;\r\n    fetch(url)\r\n      .then((response) =&gt; {\r\n        if (response.ok) {\r\n          return response.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then((response) =&gt; setRecipe(response))\r\n      .catch(() =&gt; navigate(\"\/recipes\"));\r\n  }, [params.id]);\r\n\r\n  const addHtmlEntities = (str) =&gt; {\r\n    return String(str).replace(\/&amp;lt;\/g, \"&lt;\").replace(\/&amp;gt;\/g, \"&gt;\");\r\n  };\r\n\r\n  <mark>const deleteRecipe = () =&gt; {<\/mark>\r\n    <mark>const url = `\/api\/v1\/destroy\/${params.id}`;<\/mark>\r\n    <mark>const token = document.querySelector('meta[name=\"csrf-token\"]').content;<\/mark>\r\n\r\n    <mark>fetch(url, {<\/mark>\r\n      <mark>method: \"DELETE\",<\/mark>\r\n      <mark>headers: {<\/mark>\r\n        <mark>\"X-CSRF-Token\": token,<\/mark>\r\n        <mark>\"Content-Type\": \"application\/json\",<\/mark>\r\n      <mark>},<\/mark>\r\n    <mark>})<\/mark>\r\n      <mark>.then((response) =&gt; {<\/mark>\r\n        <mark>if (response.ok) {<\/mark>\r\n          <mark>return response.json();<\/mark>\r\n        <mark>}<\/mark>\r\n        <mark>throw new Error(\"Network response was not ok.\");<\/mark>\r\n      <mark>})<\/mark>\r\n      <mark>.then(() =&gt; navigate(\"\/recipes\"))<\/mark>\r\n      <mark>.catch((error) =&gt; console.log(error.message));<\/mark>\r\n  <mark>};<\/mark>\r\n\r\n  const ingredientList = () =&gt; {\r\n    let ingredientList = \"No ingredients available\";\r\n\r\n    if (recipe.ingredients.length &gt; 0) {\r\n      ingredientList = recipe.ingredients\r\n        .split(\",\")\r\n        .map((ingredient, index) =&gt; (\r\n          &lt;li key={index} className=\"list-group-item\"&gt;\r\n            {ingredient}\r\n          &lt;\/li&gt;\r\n        ));\r\n    }\r\n\r\n    return ingredientList;\r\n  };\r\n\r\n  const recipeInstruction = addHtmlEntities(recipe.instruction);\r\n\r\n  return (\r\n    &lt;div className=\"\"&gt;\r\n...\r\n<\/code><\/pre>\n<\/div>\n<p>\u5728deleteRecipe\u51fd\u6570\u4e2d\uff0c\u60a8\u83b7\u53d6\u8981\u5220\u9664\u7684\u98df\u8c31\u7684ID\uff0c\u7136\u540e\u6784\u5efaURL\u5e76\u83b7\u53d6CSRF\u4ee4\u724c\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u5411Recipes\u63a7\u5236\u5668\u53d1\u9001DELETE\u8bf7\u6c42\u6765\u5220\u9664\u8be5\u98df\u8c31\u3002\u5982\u679c\u6210\u529f\u5220\u9664\u98df\u8c31\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230\u98df\u8c31\u9875\u9762\u3002<\/p>\n<p>\u5728\u7ec4\u4ef6\u4e2d\u7684\u5220\u9664\u6309\u94ae\u5143\u7d20\u4e0a\u6dfb\u52a0\u4e00\u4e2aonClick\u4e8b\u4ef6\uff0c\u5e76\u5c06deleteRecipe\u51fd\u6570\u4f5c\u4e3a\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4f20\u9012\u7ed9\u8be5\u6309\u94ae\uff0c\u4ee5\u4fbf\u5728\u70b9\u51fb\u5220\u9664\u6309\u94ae\u65f6\u8fd0\u884c\u8be5\u51fd\u6570\u3002<\/p>\n<pre class=\"post-pre\"><code>...\r\nreturn (\r\n    &lt;div className=\"\"&gt;\r\n      &lt;div className=\"hero position-relative d-flex align-items-center justify-content-center\"&gt;\r\n        &lt;img\r\n          src={recipe.image}\r\n          alt={`${recipe.name} \u56fe\u7247`}\r\n          className=\"img-fluid position-absolute\"\r\n        \/&gt;\r\n        &lt;div className=\"overlay bg-dark position-absolute\" \/&gt;\r\n        &lt;h1 className=\"display-4 position-relative text-white\"&gt;\r\n          {recipe.name}\r\n        &lt;\/h1&gt;\r\n      &lt;\/div&gt;\r\n      &lt;div className=\"container py-5\"&gt;\r\n        &lt;div className=\"row\"&gt;\r\n          &lt;div className=\"col-sm-12 col-lg-3\"&gt;\r\n            &lt;ul className=\"list-group\"&gt;\r\n              &lt;h5 className=\"mb-2\"&gt;\u914d\u6599&lt;\/h5&gt;\r\n              {ingredientList()}\r\n            &lt;\/ul&gt;\r\n          &lt;\/div&gt;\r\n          &lt;div className=\"col-sm-12 col-lg-7\"&gt;\r\n            &lt;h5 className=\"mb-2\"&gt;\u5236\u4f5c\u8bf4\u660e&lt;\/h5&gt;\r\n            &lt;div\r\n              dangerouslySetInnerHTML={{\r\n                __html: `${recipeInstruction}`,\r\n              }}\r\n            \/&gt;\r\n          &lt;\/div&gt;\r\n          &lt;div className=\"col-sm-12 col-lg-2\"&gt;\r\n            &lt;button\r\n              type=\"button\"\r\n              className=\"btn btn-danger\"\r\n              <mark>onClick={deleteRecipe}<\/mark>\r\n            &gt;\r\n              \u5220\u9664\u98df\u8c31\r\n            &lt;\/button&gt;\r\n          &lt;\/div&gt;\r\n        &lt;\/div&gt;\r\n        &lt;Link to=\"\/recipes\" className=\"btn btn-link\"&gt;\r\n          \u8fd4\u56de\u98df\u8c31\u5217\u8868\r\n        &lt;\/Link&gt;\r\n      &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n...\r\n<\/code><\/pre>\n<p>\u5728\u6559\u7a0b\u5230\u8fbe\u8fd9\u4e00\u70b9\u65f6\uff0c\u4f60\u7684\u5b8c\u6574\u7684Recipe.jsx\u6587\u4ef6\u5e94\u8be5\u4e0e\u8fd9\u4e2a\u6587\u4ef6\u76f8\u5339\u914d\u3002<\/p>\n<div>~\/rails_react_recipe\/app\/javascript\/components\/\u98df\u8c31.jsx<\/div>\n<pre class=\"post-pre\"><code>import React, { useState, useEffect } from \"react\";\r\nimport { Link, useNavigate, useParams } from \"react-router-dom\";\r\n\r\nconst Recipe = () =&gt; {\r\n  const params = useParams();\r\n  const navigate = useNavigate();\r\n  const [recipe, setRecipe] = useState({ ingredients: \"\" });\r\n\r\n  useEffect(() =&gt; {\r\n    const url = `\/api\/v1\/show\/${params.id}`;\r\n    fetch(url)\r\n      .then((response) =&gt; {\r\n        if (response.ok) {\r\n          return response.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then((response) =&gt; setRecipe(response))\r\n      .catch(() =&gt; navigate(\"\/recipes\"));\r\n  }, [params.id]);\r\n\r\n  const addHtmlEntities = (str) =&gt; {\r\n    return String(str).replace(\/&amp;lt;\/g, \"&lt;\").replace(\/&amp;gt;\/g, \"&gt;\");\r\n  };\r\n\r\n  const deleteRecipe = () =&gt; {\r\n    const url = `\/api\/v1\/destroy\/${params.id}`;\r\n    const token = document.querySelector('meta[name=\"csrf-token\"]').content;\r\n\r\n    fetch(url, {\r\n      method: \"DELETE\",\r\n      headers: {\r\n        \"X-CSRF-Token\": token,\r\n        \"Content-Type\": \"application\/json\",\r\n      },\r\n    })\r\n      .then((response) =&gt; {\r\n        if (response.ok) {\r\n          return response.json();\r\n        }\r\n        throw new Error(\"Network response was not ok.\");\r\n      })\r\n      .then(() =&gt; navigate(\"\/recipes\"))\r\n      .catch((error) =&gt; console.log(error.message));\r\n  };\r\n\r\n  const ingredientList = () =&gt; {\r\n    let ingredientList = \"No ingredients available\";\r\n\r\n    if (recipe.ingredients.length &gt; 0) {\r\n      ingredientList = recipe.ingredients\r\n        .split(\",\")\r\n        .map((ingredient, index) =&gt; (\r\n          &lt;li key={index} className=\"list-group-item\"&gt;\r\n            {ingredient}\r\n          &lt;\/li&gt;\r\n        ));\r\n    }\r\n\r\n    return ingredientList;\r\n  };\r\n\r\n  const recipeInstruction = addHtmlEntities(recipe.instruction);\r\n\r\n  return (\r\n    &lt;div className=\"\"&gt;\r\n      &lt;div className=\"hero position-relative d-flex align-items-center justify-content-center\"&gt;\r\n        &lt;img\r\n          src={recipe.image}\r\n          alt={`${recipe.name} image`}\r\n          className=\"img-fluid position-absolute\"\r\n        \/&gt;\r\n        &lt;div className=\"overlay bg-dark position-absolute\" \/&gt;\r\n        &lt;h1 className=\"display-4 position-relative text-white\"&gt;\r\n          {recipe.name}\r\n        &lt;\/h1&gt;\r\n      &lt;\/div&gt;\r\n      &lt;div className=\"container py-5\"&gt;\r\n        &lt;div className=\"row\"&gt;\r\n          &lt;div className=\"col-sm-12 col-lg-3\"&gt;\r\n            &lt;ul className=\"list-group\"&gt;\r\n              &lt;h5 className=\"mb-2\"&gt;Ingredients&lt;\/h5&gt;\r\n              {ingredientList()}\r\n            &lt;\/ul&gt;\r\n          &lt;\/div&gt;\r\n          &lt;div className=\"col-sm-12 col-lg-7\"&gt;\r\n            &lt;h5 className=\"mb-2\"&gt;Preparation Instructions&lt;\/h5&gt;\r\n            &lt;div\r\n              dangerouslySetInnerHTML={{\r\n                __html: `${recipeInstruction}`,\r\n              }}\r\n            \/&gt;\r\n          &lt;\/div&gt;\r\n          &lt;div className=\"col-sm-12 col-lg-2\"&gt;\r\n            &lt;button\r\n              type=\"button\"\r\n              className=\"btn btn-danger\"\r\n              onClick={deleteRecipe}\r\n            &gt;\r\n              Delete Recipe\r\n            &lt;\/button&gt;\r\n          &lt;\/div&gt;\r\n        &lt;\/div&gt;\r\n        &lt;Link to=\"\/recipes\" className=\"btn btn-link\"&gt;\r\n          Back to recipes\r\n        &lt;\/Link&gt;\r\n      &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n  );\r\n};\r\n\r\nexport default Recipe;\r\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u91cd\u65b0\u542f\u52a8\u5e94\u7528\u670d\u52a1\u5668\u5e76\u5bfc\u822a\u81f3\u4e3b\u9875\u3002\u70b9\u51fb\u67e5\u770b\u83dc\u8c31\u6309\u94ae\u4ee5\u8bbf\u95ee\u6240\u6709\u5df2\u5b58\u5728\u7684\u83dc\u8c31\uff0c\u7136\u540e\u6253\u5f00\u4efb\u4f55\u7279\u5b9a\u7684\u83dc\u8c31\u5e76\u70b9\u51fb\u9875\u9762\u4e0a\u7684\u5220\u9664\u83dc\u8c31\u6309\u94ae\u4ee5\u5220\u9664\u6587\u7ae0\u3002\u60a8\u5c06\u88ab\u91cd\u5b9a\u5411\u81f3\u83dc\u8c31\u9875\u9762\uff0c\u88ab\u5220\u9664\u7684\u83dc\u8c31\u5c06\u4e0d\u518d\u5b58\u5728\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u968f\u7740\u5220\u9664\u6309\u94ae\u53ef\u7528\uff0c\u4f60\u62e5\u6709\u4e86\u4e00\u4e2a\u5b8c\u5168\u529f\u80fd\u7684\u98df\u8c31\u5e94\u7528\u7a0b\u5e8f\uff01<\/p>\n<h2>\u7ed3\u8bba<\/p>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u4f7f\u7528Ruby on Rails\u548cReact\u524d\u7aef\u521b\u5efa\u4e86\u4e00\u4e2a\u98df\u8c31\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528PostgreSQL\u4f5c\u4e3a\u6570\u636e\u5e93\uff0c\u5e76\u4f7f\u7528Bootstrap\u8fdb\u884c\u6837\u5f0f\u8bbe\u8ba1\u3002\u5982\u679c\u60a8\u60f3\u7ee7\u7eed\u4f7f\u7528Ruby on Rails\u8fdb\u884c\u5f00\u53d1\uff0c\u8bf7\u8003\u8651\u6309\u7167\u6211\u4eec\u7684\"\u4f7f\u7528SSH\u96a7\u9053\u5b89\u5168\u901a\u4fe1\u7684\u4e09\u5c42Rails\u5e94\u7528\u7a0b\u5e8f\"\u6559\u7a0b\u8fdb\u884c\u64cd\u4f5c\uff0c\u6216\u8005\u8bbf\u95ee\u6211\u4eec\u7684\"\u5982\u4f55\u4f7f\u7528Ruby\u8fdb\u884c\u7f16\u7801\"\u7cfb\u5217\u6765\u590d\u4e60\u60a8\u7684Ruby\u6280\u80fd\u3002\u5982\u679c\u60a8\u60f3\u66f4\u6df1\u5165\u5730\u5b66\u4e60React\uff0c\u8bf7\u5c1d\u8bd5\"\u5982\u4f55\u4f7f\u7528React\u5c55\u793a\u6765\u81eaSilicon Cloud API\u7684\u6570\u636e\"\u6559\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 Ruby on Rails\u662f\u4e00\u6b3e\u70ed\u95e8\u7684\u670d\u52a1\u5668\u7aefWeb\u5e94\u7528\u6846\u67b6\u3002\u5b83\u9a71\u52a8\u7740\u4eca\u5929\u7f51\u7edc\u4e0a\u8bb8\u591a\u6d41\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982G [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[357,322,187,323,1300],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-react","tag-ruby-on-rails","tag-ubuntu","tag-web","tag-1300"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357 - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"\u672c\u6559\u7a0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu 20.04\u7cfb\u7edf\u4e0a\u642d\u5efaRuby on Rails 7\u4e0eReact\u7ed3\u5408\u7684\u5168\u6808\u5f00\u53d1\u73af\u5883\u3002\u5305\u542b\u5b89\u88c5\u914d\u7f6e\u3001\u9879\u76ee\u521b\u5efa\u3001\u524d\u540e\u7aef\u96c6\u6210\u7b49\u5b8c\u6574\u6b65\u9aa4\uff0c\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624bRails 7\u4e0eReact\u7684\u73b0\u4ee3Web\u5f00\u53d1\u3002\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ubuntu-20-04\u4e0a\u8bbe\u7f6e\u4e00\u4e2a\u5e26\u6709react\u524d\u7aef\u7684ruby-on-rails-v7\u9879\u76ee\u3002\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6559\u7a0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu 20.04\u7cfb\u7edf\u4e0a\u642d\u5efaRuby on Rails 7\u4e0eReact\u7ed3\u5408\u7684\u5168\u6808\u5f00\u53d1\u73af\u5883\u3002\u5305\u542b\u5b89\u88c5\u914d\u7f6e\u3001\u9879\u76ee\u521b\u5efa\u3001\u524d\u540e\u7aef\u96c6\u6210\u7b49\u5b8c\u6574\u6b65\u9aa4\uff0c\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624bRails 7\u4e0eReact\u7684\u73b0\u4ee3Web\u5f00\u53d1\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ubuntu-20-04\u4e0a\u8bbe\u7f6e\u4e00\u4e2a\u5e26\u6709react\u524d\u7aef\u7684ruby-on-rails-v7\u9879\u76ee\u3002\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-24T04:22:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-01T21:27:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/6-0.png\" \/>\n<meta name=\"author\" content=\"\u79d1, \u96c5\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u79d1, \u96c5\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/\",\"name\":\"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2023-11-24T04:22:22+00:00\",\"dateModified\":\"2025-08-01T21:27:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/41e222757cdd2a3365361328bd79970a\"},\"description\":\"\u672c\u6559\u7a0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu 20.04\u7cfb\u7edf\u4e0a\u642d\u5efaRuby on Rails 7\u4e0eReact\u7ed3\u5408\u7684\u5168\u6808\u5f00\u53d1\u73af\u5883\u3002\u5305\u542b\u5b89\u88c5\u914d\u7f6e\u3001\u9879\u76ee\u521b\u5efa\u3001\u524d\u540e\u7aef\u96c6\u6210\u7b49\u5b8c\u6574\u6b65\u9aa4\uff0c\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624bRails 7\u4e0eReact\u7684\u73b0\u4ee3Web\u5f00\u53d1\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/41e222757cdd2a3365361328bd79970a\",\"name\":\"\u79d1, \u96c5\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1b2d3e00a7df03689797ebd4af8c5827ba5af936849a71050ec331f4cf902c5d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1b2d3e00a7df03689797ebd4af8c5827ba5af936849a71050ec331f4cf902c5d?s=96&d=mm&r=g\",\"caption\":\"\u79d1, \u96c5\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/keya\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357 - Blog - Silicon Cloud","description":"\u672c\u6559\u7a0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu 20.04\u7cfb\u7edf\u4e0a\u642d\u5efaRuby on Rails 7\u4e0eReact\u7ed3\u5408\u7684\u5168\u6808\u5f00\u53d1\u73af\u5883\u3002\u5305\u542b\u5b89\u88c5\u914d\u7f6e\u3001\u9879\u76ee\u521b\u5efa\u3001\u524d\u540e\u7aef\u96c6\u6210\u7b49\u5b8c\u6574\u6b65\u9aa4\uff0c\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624bRails 7\u4e0eReact\u7684\u73b0\u4ee3Web\u5f00\u53d1\u3002","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ubuntu-20-04\u4e0a\u8bbe\u7f6e\u4e00\u4e2a\u5e26\u6709react\u524d\u7aef\u7684ruby-on-rails-v7\u9879\u76ee\u3002\/","og_locale":"zh_CN","og_type":"article","og_title":"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357","og_description":"\u672c\u6559\u7a0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu 20.04\u7cfb\u7edf\u4e0a\u642d\u5efaRuby on Rails 7\u4e0eReact\u7ed3\u5408\u7684\u5168\u6808\u5f00\u53d1\u73af\u5883\u3002\u5305\u542b\u5b89\u88c5\u914d\u7f6e\u3001\u9879\u76ee\u521b\u5efa\u3001\u524d\u540e\u7aef\u96c6\u6210\u7b49\u5b8c\u6574\u6b65\u9aa4\uff0c\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624bRails 7\u4e0eReact\u7684\u73b0\u4ee3Web\u5f00\u53d1\u3002","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ubuntu-20-04\u4e0a\u8bbe\u7f6e\u4e00\u4e2a\u5e26\u6709react\u524d\u7aef\u7684ruby-on-rails-v7\u9879\u76ee\u3002\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2023-11-24T04:22:22+00:00","article_modified_time":"2025-08-01T21:27:42+00:00","og_image":[{"url":"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/65645ceba4b2f92e6c723011\/6-0.png"}],"author":"\u79d1, \u96c5","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u79d1, \u96c5","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/","name":"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2023-11-24T04:22:22+00:00","dateModified":"2025-08-01T21:27:42+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/41e222757cdd2a3365361328bd79970a"},"description":"\u672c\u6559\u7a0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu 20.04\u7cfb\u7edf\u4e0a\u642d\u5efaRuby on Rails 7\u4e0eReact\u7ed3\u5408\u7684\u5168\u6808\u5f00\u53d1\u73af\u5883\u3002\u5305\u542b\u5b89\u88c5\u914d\u7f6e\u3001\u9879\u76ee\u521b\u5efa\u3001\u524d\u540e\u7aef\u96c6\u6210\u7b49\u5b8c\u6574\u6b65\u9aa4\uff0c\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624bRails 7\u4e0eReact\u7684\u73b0\u4ee3Web\u5f00\u53d1\u3002","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"Ubuntu 20.04\u642d\u5efaRuby on Rails 7\u4e0eReact\u5168\u6808\u9879\u76ee\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website","url":"https:\/\/www.silicloud.com\/zh\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/41e222757cdd2a3365361328bd79970a","name":"\u79d1, \u96c5","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1b2d3e00a7df03689797ebd4af8c5827ba5af936849a71050ec331f4cf902c5d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1b2d3e00a7df03689797ebd4af8c5827ba5af936849a71050ec331f4cf902c5d?s=96&d=mm&r=g","caption":"\u79d1, \u96c5"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/keya\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ubuntu-20-04%e4%b8%8a%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e5%b8%a6%e6%9c%89react%e5%89%8d%e7%ab%af%e7%9a%84ruby-on-rails-v7%e9%a1%b9%e7%9b%ae%e3%80%82\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":3,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":109983,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions\/109983"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}