vendredi 31 juillet 2015

Associated Id Not saved in database

getflows_controller.rb

    class GetflowsController < ApplicationController
  before_action :set_getflow, only: [:show, :edit, :update, :destroy]
  # GET /getflows
  # GET /getflows.json
  def index
    @getflows = Getflow.all
    @todos = Todo.includes(:getflow)



  end

  # GET /getflows/1
  # GET /getflows/1.json
  def show
    @todos = @getflow.todos

  end

  # GET /getflows/new
  def new
    @getflows = Getflow.all
    @getflow = Getflow.new
  end

  # GET /getflows/1/edit
  def edit
  end

  # POST /getflows
  # POST /getflows.json
  def create
    @getflow = Getflow.new(getflow_params)

    respond_to do |format|
      if @getflow.save
        format.html { redirect_to @todo, notice: 'Todo was successfully created.' }
        format.json { render :show, status: :created, location: @getflow }
      else
        format.html { render :new }
        format.json { render json: @getflow.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /getflows/1
  # PATCH/PUT /getflows/1.json
  def update
    respond_to do |format|
      if @getflow.update(getflow_params)
        format.js 
        format.json { render :show, status: :ok, location: @getflow }
      else
        format.html { render :edit }
        format.json { render json: @getflow.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /getflows/1
  # DELETE /getflows/1.json
  def destroy
    @getflow.destroy
    respond_to do |format|
      format.html { redirect_to getflows_url, notice: 'Getflow was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_getflow
      @getflow = Getflow.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def getflow_params
      params.require(:getflow).permit(:name, :title, :completed, :group_id)
    end
end

groups_controller.rb

    class GroupsController < ApplicationController
  before_action :set_group, only: [:show, :edit, :update, :destroy]

  # GET /groups
  # GET /groups.json
  def index
    @groups = Group.all

  end

  # GET /groups/1
  # GET /groups/1.json
  def show
    @getflows = @.group.getflows
  end

  # GET /groups/new
  def new
    @group = Group.new
    @getflows = Getflow.all
  end

  # GET /groups/1/edit
  def edit
  end

  # POST /groups
  # POST /groups.json
  def create
    @group = Group.new(group_params)

    respond_to do |format|
      if @group.save
        format.html { redirect_to @group, notice: 'Group was successfully created.' }
        format.json { render :show, status: :created, location: @group }
      else
        format.html { render :new }
        format.json { render json: @group.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /groups/1
  # PATCH/PUT /groups/1.json
  def update
    respond_to do |format|
      if @group.update(group_params)
        format.html { redirect_to @group, notice: 'Group was successfully updated.' }
        format.json { render :show, status: :ok, location: @group }
      else
        format.html { render :edit }
        format.json { render json: @group.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /groups/1
  # DELETE /groups/1.json
  def destroy
    @group.destroy
    respond_to do |format|
      format.html { redirect_to groups_url, notice: 'Group was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_group
      @group = Group.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def group_params
      params.require(:group).permit(:name)
    end
end

group.rb

    class Group < ActiveRecord::Base
    has_many :getflow
end

getflow.rb

    class Getflow < ActiveRecord::Base
    has_many :todos
    belongs_to :group
end

schema.rb

    ActiveRecord::Schema.define(version: 20150731055716) do

  create_table "events", force: :cascade do |t|
    t.string   "title",      limit: 255
    t.string   "color",      limit: 255, default: "blue"
    t.datetime "end"
    t.datetime "start"
    t.datetime "created_at",                              null: false
    t.datetime "updated_at",                              null: false
  end

  add_index "events", ["end"], name: "end", unique: true, using: :btree
  add_index "events", ["start"], name: "start", unique: true, using: :btree

  create_table "getflows", force: :cascade do |t|
    t.string   "name",       limit: 255
    t.string   "title",      limit: 255
    t.boolean  "completed",  limit: 1
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
    t.integer  "group_id",   limit: 4
  end

  create_table "groups", force: :cascade do |t|
    t.string   "name",       limit: 255
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

  create_table "meetings", force: :cascade do |t|
    t.string   "name",       limit: 255
    t.datetime "starts_at"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

  create_table "todos", force: :cascade do |t|
    t.text     "content",    limit: 65535
    t.integer  "order",      limit: 4
    t.boolean  "done",       limit: 1
    t.datetime "created_at",               null: false
    t.datetime "updated_at",               null: false
    t.integer  "getflow_id", limit: 4
  end

end

I have two models 1.Getflow 2.Group

Here i am using belongs_to, has_many association between those model in rails 4.

But group_id is not saved in getflows model database.

If i keep @group = Group.find(params[:id]) in getflows_controller index then it says Couldn't find Group with 'id'=

Please help me how to save it.

Aucun commentaire:

Enregistrer un commentaire