Rails 提交创建操作成功
Rails submit create action succeeding
我正在学习 ruby rails 我有一个#create 问题(我认为)
当我用 form_for 创建一个新的 "stat" 时,一旦我按下提交,我就会重定向到 index.html.erb (如我的控制器中所写)但是除了id(即使我选择了自己的 id,它是由 rails 生成的)
奇怪的是,如果我编辑新条目(除了 :ID 之外的所有内容都是空白的),数据将被保存。
我希望我足够清楚这是我关于 Whosebug 的第一个问题
非常感谢!
我的控制器:统计数据
def new
@stat = Stat.new
end
def create
@stat = Stat.new
if @stat.save
redirect_to "/stats"
flash[:notice] = "work"
else
render "new"
flash[:notice] = "didn't work"
end
end
创建操作的日志
tarted POST "/stats" for 92.133.16.18 at 2015-06-09 12:09:11 +0000
Processing by StatsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bCV+ymN4NxuMM6441OLaRyu/cuLXIcX5fu1g/rG+gqg=", "stat"=>{"id"=>"", "cc"=>"ok", "ct"=>"ok", "force"=>"ok", "endurance"=>"ok", "blessure"=>"ok", "init"=>"ok", "attaque"=>"ok", "ld"=>"ok", "sv"=>"ok"}, "commit"=>"Save"}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "stats" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-06-09 12:09:11.561787"], ["updated_at", "2015-06-09 12:09:11.561787"]]
(10.8ms) commit transaction
Redirected to https://codex-bobix.c9.io/stats
Completed 302 Found in 16ms (ActiveRecord: 11.2ms)
更新操作的日志
Started PATCH "/stats/11" for 92.133.16.18 at 2015-06-09 12:04:09 +0000
Processing by StatsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bCV+ymN4NxuMM6441OLaRyu/cuLXIcX5fu1g/rG+gqg=", "stat"=>{"id"=>"11", "statname"=>"tt", "cc"=>"tt", "ct"=>"t", "force"=>"ttt", "endurance"=>"tt", "blessure"=>"t", "init"=>"ttt", "attaque"=>"tt", "ld"=>"ttt", "sv"=>"tt"}, "commit"=>"save", "id"=>"11"}
Stat Load (0.2ms) SELECT "stats".* FROM "stats" WHERE "stats"."id" = ? LIMIT 1 [["id", 11]]
(0.1ms) begin transaction
SQL (0.5ms) UPDATE "stats" SET "attaque" = ?, "blessure" = ?, "cc" = ?, "ct" = ?, "endurance" = ?, "force" = ?, "init" = ?, "ld" = ?, "statname" = ?, "sv" = ?, "updated_at" = ? WHERE "stats"."id" = 11 [["attaque", "tt"], ["blessure", "t"], ["cc", "tt"], ["ct", "t"], ["endurance", "tt"], ["force", "ttt"], ["init", "ttt"], ["ld", "ttt"], ["statname", "tt"], ["sv", "tt"], ["updated_at", "2015-06-09 12:04:09.051306"]]
(15.9ms) commit transaction
Redirected to https://codex-bobix.c9.io/stats
Completed 302 Found in 25ms (ActiveRecord: 16.8ms)
.
class StatsController < ApplicationController
def index
@stats = Stat.all
end
def show
@stat = Stat.find(params[:id])
@units = @stat.units
end
def new
@stat = Stat.new
end
def edit
@stat = Stat.find(params[:id])
end
def update
@stat = Stat.find(params[:id])
@stat.update(stat_params)
redirect_to "/stats"
flash[:notice] = "work"
end
def new
@stat = Stat.new
end
def create
@stat = Stat.new
if @stat.save
redirect_to "/stats"
flash[:notice] = "work"
else
render "new"
flash[:notice] = "didn't work"
end
end
private
def stat_params
params.require(:stat).permit(:cc, :ct, :force, :endurance, :blessure, :init, :attaque, :ld, :sv, :id, :statname)
end
end
在您的 create
操作中,您没有为您的 Stat
传递任何参数,它应该类似于:
class StatsController < ApplicationController
def create
@stat = Stat.new stat_params
# ...
end
private
def stat_params
params.require(:stat).permit(:id, :cc, :ct, :force, :endurance, :blessure, :init, :attaque, :ld, :sv)
end
end
希望对您有所帮助!
当您说 @stat = Stat.new
时,您正在实例化一个没有指定属性的新 Stat 对象,然后保存它。因此,无论您将什么参数传递给控制器,您都只会拥有自动生成的字段(在本例中为 ID)。您需要做的是获取参数并从中创建一个新对象(即 @stat = Stat.new( stat_params )
)。
由于您在控制器底部定义了一个 stat_params 方法(当您生成脚手架时通常默认情况下就是这种情况),那么仅此行代码就可以修复它。本质上,您需要做的就是获取统计数据的参数(从新提交),将它们转换为散列,然后将它们放入 Stat.new( ... )
语句中。
我正在学习 ruby rails 我有一个#create 问题(我认为)
当我用 form_for 创建一个新的 "stat" 时,一旦我按下提交,我就会重定向到 index.html.erb (如我的控制器中所写)但是除了id(即使我选择了自己的 id,它是由 rails 生成的)
奇怪的是,如果我编辑新条目(除了 :ID 之外的所有内容都是空白的),数据将被保存。
我希望我足够清楚这是我关于 Whosebug 的第一个问题
非常感谢!
我的控制器:统计数据
def new
@stat = Stat.new
end
def create
@stat = Stat.new
if @stat.save
redirect_to "/stats"
flash[:notice] = "work"
else
render "new"
flash[:notice] = "didn't work"
end
end
创建操作的日志
tarted POST "/stats" for 92.133.16.18 at 2015-06-09 12:09:11 +0000
Processing by StatsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bCV+ymN4NxuMM6441OLaRyu/cuLXIcX5fu1g/rG+gqg=", "stat"=>{"id"=>"", "cc"=>"ok", "ct"=>"ok", "force"=>"ok", "endurance"=>"ok", "blessure"=>"ok", "init"=>"ok", "attaque"=>"ok", "ld"=>"ok", "sv"=>"ok"}, "commit"=>"Save"}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "stats" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-06-09 12:09:11.561787"], ["updated_at", "2015-06-09 12:09:11.561787"]]
(10.8ms) commit transaction
Redirected to https://codex-bobix.c9.io/stats
Completed 302 Found in 16ms (ActiveRecord: 11.2ms)
更新操作的日志
Started PATCH "/stats/11" for 92.133.16.18 at 2015-06-09 12:04:09 +0000
Processing by StatsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bCV+ymN4NxuMM6441OLaRyu/cuLXIcX5fu1g/rG+gqg=", "stat"=>{"id"=>"11", "statname"=>"tt", "cc"=>"tt", "ct"=>"t", "force"=>"ttt", "endurance"=>"tt", "blessure"=>"t", "init"=>"ttt", "attaque"=>"tt", "ld"=>"ttt", "sv"=>"tt"}, "commit"=>"save", "id"=>"11"}
Stat Load (0.2ms) SELECT "stats".* FROM "stats" WHERE "stats"."id" = ? LIMIT 1 [["id", 11]]
(0.1ms) begin transaction
SQL (0.5ms) UPDATE "stats" SET "attaque" = ?, "blessure" = ?, "cc" = ?, "ct" = ?, "endurance" = ?, "force" = ?, "init" = ?, "ld" = ?, "statname" = ?, "sv" = ?, "updated_at" = ? WHERE "stats"."id" = 11 [["attaque", "tt"], ["blessure", "t"], ["cc", "tt"], ["ct", "t"], ["endurance", "tt"], ["force", "ttt"], ["init", "ttt"], ["ld", "ttt"], ["statname", "tt"], ["sv", "tt"], ["updated_at", "2015-06-09 12:04:09.051306"]]
(15.9ms) commit transaction
Redirected to https://codex-bobix.c9.io/stats
Completed 302 Found in 25ms (ActiveRecord: 16.8ms)
.
class StatsController < ApplicationController
def index
@stats = Stat.all
end
def show
@stat = Stat.find(params[:id])
@units = @stat.units
end
def new
@stat = Stat.new
end
def edit
@stat = Stat.find(params[:id])
end
def update
@stat = Stat.find(params[:id])
@stat.update(stat_params)
redirect_to "/stats"
flash[:notice] = "work"
end
def new
@stat = Stat.new
end
def create
@stat = Stat.new
if @stat.save
redirect_to "/stats"
flash[:notice] = "work"
else
render "new"
flash[:notice] = "didn't work"
end
end
private
def stat_params
params.require(:stat).permit(:cc, :ct, :force, :endurance, :blessure, :init, :attaque, :ld, :sv, :id, :statname)
end
end
在您的 create
操作中,您没有为您的 Stat
传递任何参数,它应该类似于:
class StatsController < ApplicationController
def create
@stat = Stat.new stat_params
# ...
end
private
def stat_params
params.require(:stat).permit(:id, :cc, :ct, :force, :endurance, :blessure, :init, :attaque, :ld, :sv)
end
end
希望对您有所帮助!
当您说 @stat = Stat.new
时,您正在实例化一个没有指定属性的新 Stat 对象,然后保存它。因此,无论您将什么参数传递给控制器,您都只会拥有自动生成的字段(在本例中为 ID)。您需要做的是获取参数并从中创建一个新对象(即 @stat = Stat.new( stat_params )
)。
由于您在控制器底部定义了一个 stat_params 方法(当您生成脚手架时通常默认情况下就是这种情况),那么仅此行代码就可以修复它。本质上,您需要做的就是获取统计数据的参数(从新提交),将它们转换为散列,然后将它们放入 Stat.new( ... )
语句中。