mercredi 21 septembre 2016

Rails Not able to save data, association

I'm doing a parking permit website. The problem I met is that I'm not able to save my data to the PERMIT database which associated with the USER database. The problem i think is I didn't bring the user to the permit(Maybe i missed something). I found out the error when I trying to save from Permit.errors.full_messages is ["User must exist"]. Any help is appreciated, Thank you!

Schema.rb

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

  create_table "permits", force: :cascade do |t|
    t.string   "vehicle_type"
    t.string   "name"
    t.string   "studentid"
    t.string   "department"
    t.string   "carplate"
    t.string   "duration"
    t.date     "permitstart"
    t.date     "permitend"
    t.integer  "user_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
    t.index ["user_id"], name: "index_permits_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
    t.integer  "user_type"
  end

end

Create_permit.rb

class CreatePermits < ActiveRecord::Migration[5.0]
  def change
    create_table :permits do |t|
      t.string :vehicle_type
      t.string :name
      t.string :studentid
      t.string :department
      t.string :carplate
      t.string :duration
      t.date :permitstart
      t.date :permitend
      t.references :user, foreign_key: true

      t.timestamps
    end
    add_index :permits, :user_id
  end
end

Permit_controller

class PermitsController < ApplicationController
  before_action :set_permit, only: [:show, :destroy]
  def index
    @permits = Permit.all
  end

  def new
    @permits = Permit.new
  end

  def create
    @permits = Permit.new(permit_params)

      if @permits.save
        redirect_to @permits
      else
        redirect_to contact_path
      end

  end

  def destroy
    Permit.destroy_all(user_id: 1)
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'Permit was successfully canceled.' }
      format.json { head :no_content }
    end
  end

  def show
    @permits = Permit.find(params[:id])
  end

  def update
    respond_to do |format|
      if @permits.update(user_params)
        format.html { redirect_to @user, notice: 'Permit was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def permit_params
    params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
  end
end

user.rb

class User < ApplicationRecord
  has_many :permits
  has_secure_password
end

Permit.rb

    class Permit < ApplicationRecord
    belongs_to :user
    end

permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@permits) do |f| %>

        <%= f.label :"Vehicle" %>
        <%= f.text_field :vehicle_type, class: 'form-control' %>

        <%= f.label :"License Plate" %>
        <%= f.text_field :carplate, class: 'form-control' %>

        <%= f.label :"Student ID" %>
        <%= f.text_field :studentid, class: 'form-control' %>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control' %>

        <%= f.label :"Department of applicant" %>
        <%= f.text_field :department, class: 'form-control' %>

        <%= f.label :permit_start %>
        <%= f.date_select :permitstart, class: 'form-control' %>

        <%= f.label :permit_end %>
        <%= f.date_select :permitend,  class: 'form-control'  %>


        <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

Aucun commentaire:

Enregistrer un commentaire