0

I am trying to implement a Parking Permit application page using ROR. I couldn't get my data saved into the database. The permit database is associated with the user also. The program won't save the data and execute the else statement. There is no error generated, i think i have missed something but i don't know the exact problem. Any help is appreciated!

Permit_controller.rb

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 root_path
    else
      redirect_to contact_path
    end
  end

  def destroy
  end

  def show
    @permits = Permit.find(params[:id])
  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,:permitstart, :permitend)
  end
end

Permit.rb

class Permit < ApplicationRecord
  belongs_to :user
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.date :permitstart
      t.date :permitend
      t.references :user, foreign_key: true

      t.timestamps
    end
    add_foreign_key :permits, :user
    add_index :permits, [:user_id, :created_at]
  end
end

User.rb

    class User < ApplicationRecord
      has_secure_password
      has_many :permits
    end

    #book pg 264 Validation

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>

schema.rb

ActiveRecord::Schema.define(version: 20160921071908) 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.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

8
  • Are there any log entries in your log/development.log when you try to create a permit? Commented Sep 21, 2016 at 7:54
  • 1
    As per the code , the permit form should gets saved without user as you havn't bind user somewhere in the form , paste the logs when you click on the submit button Commented Sep 21, 2016 at 8:00
  • Yep there is, the main thing from the log is direct me to the contact.html which is the ELSE statement Commented Sep 21, 2016 at 8:00
  • Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX", "permit"=>{"vehicle_type"=>"safd", "carplate"=>"aef", "studentid"=>"awer", "name"=>"ear", "department"=>"wera", "permitstart(1i)"=>"2016", "permitstart(2i)"=>"9", "permitstart(3i)"=>"21", "permitend(1i)"=>"2016", "permitend(2i)"=>"9", "permitend(3i)"=>"21"}, "commit"=>"Submit"} [1m[35m (0.0ms)[0m [1m[36mbegin transaction[0m [1m[35m (0.0ms)[0m [1m[31mrollback transaction[0m Redirected to 127.0.0.1:3000/contact Completed 302 Found in 17ms (ActiveRecord: 1.0ms) Commented Sep 21, 2016 at 8:04
  • Use user_id in the params in the permit_params. I guess it can solve, but I don't see anything wrong here. Give it a try though if you are in a page with a user logged in. Commented Sep 21, 2016 at 8:05

3 Answers 3

3

check with this @permits.save!. it shows the exact error.

Sign up to request clarification or add additional context in comments.

7 Comments

When i changed it to @permit.save!, it shows ActiveRecord::RecordInvalid in PermitsController#create Validation failed: User must exist
Do you have a validation for user_id ? Are you doing this from a page with a user logged in ?
I don't have any validation on this, yep I set a condition which the user must logged in first
Then follow my comment on your question and add user_id in permit _params
@GaryVlc: you have belongs_to on your Permit. It is a required field in rails 4.2+. Assign user to your permit so that it can be saved.
|
0
    module ApplicationHelper
     #for current user to use through out the app
        def current_user
                 @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
               end
 end

and

def create
        @permits = Permit.new(permit_params)
        @permits.user = current_user
        if @permits.save
          redirect_to root_path
        else
          redirect_to contact_path
        end
      end

test it

Comments

0

Or you can just say that a permit has a single user and avoid the confusion.

#models/permit.rb
class Permit < ApplicationRecord
    has_one :user
end

#controllers/permit_controller.rb
def create
    @user = User.find(session[:user_id]) #use your session variable
    @permits = Permit.new(permit_params)

    if @permits.save
      @user.permits << @permits
      redirect_to root_path
    else
      redirect_to contact_path
    end
end

It will save permits for the logged in user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.