Rails submit create action succeeding

98 views Asked by At

I am learning ruby rails and i have a #create problem (i think)

when i am creating a new "stat" with form_for, once i press submit i get redirect to index.html.erb (as written in my controller) but there is no data apart from the id (even if i chose my self an id, it get generated by rails)

the weird part is, if i edit the new entry (where everything is blank but :ID) data are saved.

I hope i am clear enough it's my first question on stackoverflow

Thx a lot !

My controller : 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 

the log for create action

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)

the log for the update action

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
2

There are 2 answers

0
Paweł Dawczak On BEST ANSWER

In your create action you are not passing any arguments for your Stat, it should be something like:

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

Hope that helps!

0
ConnorCMcKee On

When you say @stat = Stat.new, you're instantiating a new Stat object with no assigned attributes, and then saving that. As such, no matter what parameters you pass to the controller, you will only have the fields which are automatically generated (in this case ID). What you need to do is take the parameters and create a new object from them (i.e. @stat = Stat.new( stat_params )).

Since you have a stat_params method defined at the bottom of your controller (as is generally the case by default when you generate a scaffold), then that line of code alone will fix it. Essentially all you need to do is grab the params for the stat (submitted from new), turn them into a hash, and put them into the Stat.new( ... ) statement.