无法将我的数据保存到sqlite数据库中

时间:2016-09-21 07:53:35

标签: ruby-on-rails ruby ruby-on-rails-3 sqlite

我正在尝试使用ROR实现停车许可申请页面。我无法将我的数据保存到数据库中。许可证数据库也与用户相关联。程序不会保存数据并执行else语句。没有产生错误,我想我已经错过了一些但我不知道确切的问题。任何帮助表示赞赏!

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

3 个答案:

答案 0 :(得分:3)

查看此@permits.save!。 它显示了确切的错误。

答案 1 :(得分: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

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

测试它

答案 2 :(得分:0)

或者你可以说许可证只有一个用户并避免混淆。

#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

它将为登录用户保存许可。