【问题标题】:Rails Not able to save data, associationRails 无法保存数据,关联
【发布时间】:2016-09-21 14:16:09
【问题描述】:

我正在做一个停车许可证网站。我遇到的问题是我无法将数据保存到与 USER 数据库关联的 PERMIT 数据库中。我认为的问题是我没有将用户带到许可证(也许我错过了一些东西)。当我尝试从 Permit.errors.full_messages 保存时发现错误是 [“用户必须存在”]。感谢您的帮助,谢谢!

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

用户.rb

class User < ApplicationRecord
  has_many :permits
  has_secure_password
end

许可证.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>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-5


    【解决方案1】:

    我猜你正在使用 Rails 5。问题是你在 permit 上有 belongs_to 关联,它属于 user,但是在创建 permit 时,你没有将任何用户与它关联,并且在 Rails 5强制将数据分配给belongs_to 关联,即当它没有user_id 时,您无法保存permit,因此请尝试将user_id 分配给permitrefer this for change to rails 5 belongs_to association

    【讨论】:

    • 这解决了我目前的问题。许可证已保存,但它保存在每个用户下,导致每个用户都可以访问它。在许可控制器创建操作中,我尝试将此行更改为 @permits = Permit.new(permit_params) 到 @permit = current_user.Permit(permit_parasm)。此错误显示“PermitsController 中的 NoMethodError#create undefined method `Permit' for #<0xa20ce10>
    【解决方案2】:

    你的错误到底在哪里?如果在update 操作中,您必须更改您的before_action。您必须在此处添加:update 操作。

    before_action :set_permit, only: [:show, :destroy, :update]
    

    【讨论】:

    • 我正在尝试创建一个新的 Permit,所以问题可能与 CREATE 操作有关。
    • 你必须这样做,尊重当前的问题,你能添加更多关于错误的信息吗?
    • 它没有显示任何箭头,只是由于未声明用户,许可数据无法保存到数据库中。应该声明哪个用户,因为他们有关系。
    猜你喜欢
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多