Rails提交创建操作成功



我正在学习ruby rails,我有一个#create问题(我认为)

当我用form_for创建一个新的"stat"时,一旦我按下提交,我就会重定向到index.html.erb(如我的控制器中所写),但除了id之外没有数据(即使我选择了我自己的id,它也会由rails生成)

奇怪的是,如果我编辑新条目(其中所有内容都是空白的,但:ID)数据被保存。

我希望我足够清楚这是我关于stackoverflow的第一个问题

谢谢你!

我的控制器:stats

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方法(在生成scaffold时,默认情况下通常是这样),因此仅这一行代码就可以修复它。基本上,您需要做的就是获取stat(从new提交)的参数,将它们转换为散列,并将它们放入Stat.new( ... )语句中。

相关内容

  • 没有找到相关文章

最新更新