{"id":24087,"date":"2024-11-21T08:44:59","date_gmt":"2024-11-21T00:44:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24087\/"},"modified":"2024-11-21T08:44:59","modified_gmt":"2024-11-21T00:44:59","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87webman%e6%a1%86%e6%9e%b6%e5%ae%9e%e7%8e%b0%e7%a4%be%e5%8c%ba%e8%ae%ba%e5%9d%9b%e5%92%8c%e8%af%9d%e9%a2%98%e8%ae%a8%e8%ae%ba%e5%8a%9f%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24087\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7Webman\u6846\u67b6\u5b9e\u73b0\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\uff1f"},"content":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7webman\u6846\u67b6\u5b9e\u73b0\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\uff1f<\/p>\n<p>Webman\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u3001\u7075\u6d3b\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6846\u67b6\u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684Webman\u5e94\u7528\u7a0b\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Webman\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u76ee\u5f55\u5e76\u751f\u6210\u5fc5\u8981\u7684\u6587\u4ef6\u7ed3\u6784\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>webman new forum_app\ncd forum_app<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e9b\u5fc5\u8981\u7684\u4f9d\u8d56\u9879\u3002\u6253\u5f00mix.exs\u6587\u4ef6\uff0c\u627e\u5230deps\u90e8\u5206\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>{:phoenix, \"~&amp;gt; 1.5\"},\n{:phoenix_html, \"~&amp;gt; 2.14\"},\n{:phoenix_live_view, \"~&amp;gt; 0.15\"},\n{:phoenix_pubsub, \"~&amp;gt; 2.0\"},\n{:phoenix_ecto, \"~&amp;gt; 4.1\"},\n{:ecto_sql, \"~&amp;gt; 3.4\"},\n{:sqlite_ecto2, \"~&amp;gt; 2.0\"}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u5b89\u88c5\u4f9d\u8d56\u9879\uff1a<\/p>\n<pre>mix deps.get<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0b\u4e00\u6b65\u662f\u751f\u6210\u5fc5\u8981\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>mix ecto.create\nmix phx.gen.html Accounts User users name:string email:string password_hash:string\nmix ecto.migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2aUser\u6a21\u578b\u548c\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u751f\u6210\u4e00\u4e9b\u89c6\u56fe\u548c\u63a7\u5236\u5668\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>mix phx.gen.html Forum Topic topics title:string content:string user_id:references:users\nmix phx.gen.html Forum Comment comments content:string user_id:references:users topic_id:references:topics\nmix ecto.migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u751f\u6210\u7684\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2aTopic\u6a21\u578b\u548c\u4e00\u4e2aComment\u6a21\u578b\uff0c\u7528\u4e8e\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u66f4\u65b0\u8def\u7531\u6587\u4ef6router.ex\uff0c\u6dfb\u52a0\u76f8\u5e94\u7684\u8def\u7531\u89c4\u5219\u3002\u6253\u5f00router.ex\u6587\u4ef6\uff0c\u627e\u5230scope &#8220;\/&#8221;\u90e8\u5206\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>scope \"\/forum\", Forum do\n  resources \"\/topics\", TopicController\nend<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u542f\u52a8Webman\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>mix phx.server<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u5c31\u5df2\u7ecf\u542f\u52a8\u4e86\u3002\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:4000\/forum\/topics\u5373\u53ef\u67e5\u770b\u8bba\u575b\u4e3b\u9898\u5217\u8868\u3002\u4f46\u662f\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0\u4e00\u4e9b\u9875\u9762\u548c\u529f\u80fd\u6765\u521b\u5efa\u65b0\u7684\u4e3b\u9898\u548c\u8bc4\u8bba\u3002<\/p>\n<p>\u6253\u5f00Topic\u63a7\u5236\u5668\u6587\u4ef6topic_controller.ex\uff0c\u627e\u5230def create\u51fd\u6570\uff0c\u5e76\u66ff\u6362\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>def create(conn, %{\"topic\" =&amp;gt; topic_params}) do\n  user_id = conn.assigns.current_user.id\n  changeset = Topic.changeset(%Topic{user_id: user_id}, topic_params)\n\n  case Repo.insert(changeset) do\n    {:ok, _topic} -&amp;gt;\n      conn\n      |&amp;gt; put_flash(:info, \"Topic created successfully.\")\n      |&amp;gt; redirect(to: Routes.topic_index_path(conn, :index))\n    {:error, changeset} -&amp;gt;\n      render(conn, \"new.html\", changeset: changeset)\n  end\nend<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6253\u5f00Comment\u63a7\u5236\u5668\u6587\u4ef6comment_controller.ex\uff0c\u627e\u5230def create\u51fd\u6570\uff0c\u5e76\u66ff\u6362\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>def create(conn, %{\"comment\" =&amp;gt; comment_params}) do\n  user_id = conn.assigns.current_user.id\n  changeset = Comment.changeset(%Comment{user_id: user_id}, comment_params)\n\n  case Repo.insert(changeset) do\n    {:ok, _comment} -&amp;gt;\n      conn\n      |&amp;gt; put_flash(:info, \"Comment created successfully.\")\n      |&amp;gt; redirect(to: Routes.topic_path(conn, :show, topic_id))\n    {:error, changeset} -&amp;gt;\n      render(conn, \"new.html\", changeset: changeset)\n  end\nend<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\u7684\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u65b0\u7684\u4e3b\u9898\u548c\u8bc4\u8bba\uff0c\u6765\u4e0e\u5176\u4ed6\u7528\u6237\u8fdb\u884c\u4ea4\u6d41\u548c\u8ba8\u8bba\u3002<\/p>\n<p>\u603b\u7ed3\u8d77\u6765\uff0c\u901a\u8fc7Webman\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u6574\u7684\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u5e73\u53f0\u3002\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u57fa\u7840\u7248\u672c\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5f00\u53d1\u548c\u5b9a\u5236\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u901a\u8fc7Webman\u6846\u67b6\u5b9e\u73b0\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7webman\u6846\u67b6\u5b9e\u73b0\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\uff1f Webman\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u3001\u7075\u6d3b\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6846\u67b6\u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u793e\u533a\u8bba\u575b\u548c\u8bdd\u9898\u8ba8\u8bba\u529f\u80fd\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684Webman\u5e94\u7528\u7a0b\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Webman\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u76ee\u5f55\u5e76\u751f\u6210\u5fc5\u8981\u7684\u6587\u4ef6\u7ed3\u6784\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a webman new forum_app cd forum_app \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4e00\u4e9b\u5fc5\u8981\u7684\u4f9d\u8d56\u9879\u3002\u6253\u5f00mix.exs\u6587\u4ef6\uff0c\u627e\u5230deps\u90e8\u5206\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a {:phoenix, &#8220;~&amp;gt; 1.5&#8221;}, {:phoenix_html, &#8220;~&amp;gt; 2.14&#8221;}, {:phoenix_live_view, &#8220;~&amp;gt; 0.15&#8221;}, {:phoenix_pubsub, &#8220;~&amp;gt; 2.0&#8221;}, {:phoenix_ecto, &#8220;~&amp;gt; 4.1&#8221;}, {:ecto_sql, &#8220;~&amp;gt; 3.4&#8221;}, {:sqlite_ecto2, &#8220;~&amp;gt; 2.0&#8221;} \u767b\u5f55\u540e\u590d\u5236 \u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u5b89\u88c5\u4f9d\u8d56\u9879\uff1a mix deps.get \u767b\u5f55\u540e\u590d\u5236 \u4e0b\u4e00\u6b65\u662f\u751f\u6210\u5fc5\u8981\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a mix ecto.create mix phx.gen.html Accounts User users name:string email:string password_hash:string mix ecto.migrate \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2aUser\u6a21\u578b\u548c\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u751f\u6210\u4e00\u4e9b\u89c6\u56fe\u548c\u63a7\u5236\u5668\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a mix phx.gen.html Forum Topic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24087","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24087"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24087\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}