Rails表单提交值为nil无法插入,且show动作缺失引发报错
问题与解决方法
问题现象
- 提交表单创建God记录时,数据库插入的
name、ph_no、bhakts字段均为nil,而非表单填写的值 - 记录创建成功后重定向到show页面,因GodsController未定义show动作,触发
AbstractController::ActionNotFound错误
相关代码与日志
GodsController
class GodsController < ApplicationController def index @g=God.all end def new @g=God.new() end def create @g=God.new(g_params) if @g.save redirect_to @g else render :new, status: :unprocessable_entity end end def g_params params.fetch(:g,{}).permit(:name , :ph_no , :bhakts) end end
schema.rb
ActiveRecord::Schema[7.0].define(version: 2023_04_18_112327) do create_table "gods", force: :cascade do |t| t.string "name" t.string "ph_no" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "bhakts" end end
God模型
class God < ApplicationRecord end
新建表单视图(new.html.erb)
<h1> Insert new God </h1> <%= form_with model: @g do |form| %> <div> <%= form.label :name %> <%= form.text_field :name %> </div> <div> <%= form.label :ph_no %> <%= form.text_field :ph_no %> </div> <div> <%= form.label :bhakts %> <%= form.text_field :bhakts %> </div> <div> <%= form.submit%> </div> <%end %>
列表视图(index.html.erb)
<h1> <b> MY GODS !! </b></h1> <table> <thead> <tr> <th> Name</th> <th> ph_no</th> <th> Bhakts</th> </tr> </thead> <tbody> <% @g.each do |x| %> <tr> <td><%= x.name%></td> <td><%= x.ph_no%></td> <td><%= x.bhakts%></td> </tr> <% end%> </tbody> </table>
终端日志
Started POST "/gods" for ::1 at 2023-04-20 12:29:09 +0530 Processing by GodsController#create as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "god"=>{"name"=>"Divya Pratap Singh", "ph_no"=>"76678", "bhakts"=>"45"}, "commit"=>"Create God"} TRANSACTION (0.2ms) begin transaction ↳ app/controllers/gods_controller.rb:10:in `create' God Create (1.0ms) INSERT INTO "gods" ("name", "ph_no", "created_at", "updated_at", "bhakts") VALUES (?, ?, ?, ?, ?) [["name", nil], ["ph_no", nil], ["created_at", "2023-04-20 06:59:09.914114"], ["updated_at", "2023-04-20 06:59:09.914114"], ["bhakts", nil]] ↳ app/controllers/gods_controller.rb:10:in `create' TRANSACTION (0.7ms) commit transaction ↳ app/controllers/gods_controller.rb:10:in `create' Redirected to http://localhost:3000/gods/15 Completed 302 Found in 10ms (ActiveRecord: 1.9ms | Allocations: 2305) Started GET "/gods/15" for ::1 at 2023-04-20 12:29:09 +0530 AbstractController::ActionNotFound (The action 'show' could not be found for GodsController): `block in spawn_thread' Started GET "/gods/new" for ::1 at 2023-04-20 12:29:10 +0530 Processing by GodsController#new as HTML Rendering layout layouts/application.html.erb Rendering gods/new.html.erb within layouts/application Rendered gods/new.html.erb within layouts/application (Duration: 1.1ms | Allocations: 1174) Rendered layout layouts/application.html.erb (Duration: 2.7ms | Allocations: 3313) Completed 200 OK in 3ms (Views: 3.0ms | ActiveRecord: 0.0ms | Allocations: 3585)
解决办法
1. 修复参数获取错误
从终端日志可见,表单提交的参数键是god,但控制器的g_params方法里误用了:g,导致无法正确读取表单值,最终所有字段插入为nil。修改g_params方法:
def g_params params.fetch(:god, {}).permit(:name, :ph_no, :bhakts) end
2. 解决show动作缺失问题
有两种方案可选:
方案一:添加show动作及对应视图
在GodsController中新增show动作:
def show @g = God.find(params[:id]) end
然后创建app/views/gods/show.html.erb视图文件,示例内容:
<h1><%= @g.name %></h1> <p>手机号: <%= @g.ph_no %></p> <p>信徒数量: <%= @g.bhakts %></p> <%= link_to '返回列表', gods_path %>
方案二:修改重定向目标到列表页
如果不需要单独的详情页面,可修改create动作的重定向逻辑,跳转到列表页:
def create @g=God.new(g_params) if @g.save redirect_to gods_path, notice: '记录创建成功' else render :new, status: :unprocessable_entity end end
内容的提问来源于stack exchange,提问作者BrownboyD
相关产品推荐
相关产品推荐

