Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 未定义的方法'first#u name';对于nil:NilClass错误_Ruby On Rails 4_Devise_Ruby 2.1 - Fatal编程技术网

Ruby on rails 4 未定义的方法'first#u name';对于nil:NilClass错误

Ruby on rails 4 未定义的方法'first#u name';对于nil:NilClass错误,ruby-on-rails-4,devise,ruby-2.1,Ruby On Rails 4,Devise,Ruby 2.1,我试图在用户和发货模型之间建立关系。我使用[Desive][1]生成用户一切正常,但现在我停止了。我得到这个错误: undefined method `first_name' for nil:NilClass 我的模型 发货。rb class Shipment < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base # Include default devise modules.

我试图在
用户
发货
模型之间建立关系。我使用[Desive][1]生成
用户
一切正常,但现在我停止了。我得到这个错误:

undefined method `first_name' for nil:NilClass
我的模型 发货。rb

class Shipment < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.all
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

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

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

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
    end
end
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string :first_name
      t.string :last_name
      t.string :city_name

      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end



add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
  def change
    add_column :shipments, :user_id, :integer
    add_index :shipments, :user_id
    remove_column :shipments, :name
  end
end
<%= simple_form_for(@shipment, html: {class: "form-horizontal"}) do |f| %>
  <% if @shipment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>

      <ul>
      <% @shipment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :description %>

  <div class="field">
    <%= f.label :ship_from %><br>
    <%= f.select :from, ['New York']%>
  </div>

  <div class="field">
    <%= f.label :ship_to %><br>
        <%= f.select :to, [ 'New york', 'Orlanda' ] %>
  </div>
    <%= f.input :date %>
    <%= f.input :pay %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<div class="page-header">
<h1>Listing Shipments</h1>
</div>

<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>

<% @shipments.each do |shipment| %>
<div class="shipment">
  <h3><strong><%= shipment.user.first_name %></strong></h3>
  <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
  <div class="meta">
    <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
    <%= link_to "show", shipment %>
    <span class="admin"> 
        | <%= link_to "Edit", edit_shipment_path(shipment) %> |
        <%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
  </span>
    </div>
  </div>
  <% end %>
class-shipping
User.rb

class Shipment < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.all
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

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

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

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
    end
end
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string :first_name
      t.string :last_name
      t.string :city_name

      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end



add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
  def change
    add_column :shipments, :user_id, :integer
    add_index :shipments, :user_id
    remove_column :shipments, :name
  end
end
<%= simple_form_for(@shipment, html: {class: "form-horizontal"}) do |f| %>
  <% if @shipment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>

      <ul>
      <% @shipment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :description %>

  <div class="field">
    <%= f.label :ship_from %><br>
    <%= f.select :from, ['New York']%>
  </div>

  <div class="field">
    <%= f.label :ship_to %><br>
        <%= f.select :to, [ 'New york', 'Orlanda' ] %>
  </div>
    <%= f.input :date %>
    <%= f.input :pay %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<div class="page-header">
<h1>Listing Shipments</h1>
</div>

<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>

<% @shipments.each do |shipment| %>
<div class="shipment">
  <h3><strong><%= shipment.user.first_name %></strong></h3>
  <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
  <div class="meta">
    <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
    <%= link_to "show", shipment %>
    <span class="admin"> 
        | <%= link_to "Edit", edit_shipment_path(shipment) %> |
        <%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
  </span>
    </div>
  </div>
  <% end %>
class用户
我的控制器 出货量\u Controller.rb

class Shipment < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.all
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

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

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

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
    end
end
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string :first_name
      t.string :last_name
      t.string :city_name

      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end



add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
  def change
    add_column :shipments, :user_id, :integer
    add_index :shipments, :user_id
    remove_column :shipments, :name
  end
end
<%= simple_form_for(@shipment, html: {class: "form-horizontal"}) do |f| %>
  <% if @shipment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>

      <ul>
      <% @shipment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :description %>

  <div class="field">
    <%= f.label :ship_from %><br>
    <%= f.select :from, ['New York']%>
  </div>

  <div class="field">
    <%= f.label :ship_to %><br>
        <%= f.select :to, [ 'New york', 'Orlanda' ] %>
  </div>
    <%= f.input :date %>
    <%= f.input :pay %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<div class="page-header">
<h1>Listing Shipments</h1>
</div>

<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>

<% @shipments.each do |shipment| %>
<div class="shipment">
  <h3><strong><%= shipment.user.first_name %></strong></h3>
  <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
  <div class="meta">
    <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
    <%= link_to "show", shipment %>
    <span class="admin"> 
        | <%= link_to "Edit", edit_shipment_path(shipment) %> |
        <%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
  </span>
    </div>
  </div>
  <% end %>
class ShipmentsController
我的数据库迁移文件是:

设计和创建用户。rb

class Shipment < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.all
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

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

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

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
    end
end
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string :first_name
      t.string :last_name
      t.string :city_name

      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end



add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
  def change
    add_column :shipments, :user_id, :integer
    add_index :shipments, :user_id
    remove_column :shipments, :name
  end
end
<%= simple_form_for(@shipment, html: {class: "form-horizontal"}) do |f| %>
  <% if @shipment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>

      <ul>
      <% @shipment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :description %>

  <div class="field">
    <%= f.label :ship_from %><br>
    <%= f.select :from, ['New York']%>
  </div>

  <div class="field">
    <%= f.label :ship_to %><br>
        <%= f.select :to, [ 'New york', 'Orlanda' ] %>
  </div>
    <%= f.input :date %>
    <%= f.input :pay %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<div class="page-header">
<h1>Listing Shipments</h1>
</div>

<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>

<% @shipments.each do |shipment| %>
<div class="shipment">
  <h3><strong><%= shipment.user.first_name %></strong></h3>
  <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
  <div class="meta">
    <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
    <%= link_to "show", shipment %>
    <span class="admin"> 
        | <%= link_to "Edit", edit_shipment_path(shipment) %> |
        <%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
  </span>
    </div>
  </div>
  <% end %>
classdeveliecCreateUsers
我的装运视图文件:

\u form.html.erb

class Shipment < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.all
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

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

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

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
    end
end
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string :first_name
      t.string :last_name
      t.string :city_name

      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end



add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
  def change
    add_column :shipments, :user_id, :integer
    add_index :shipments, :user_id
    remove_column :shipments, :name
  end
end
<%= simple_form_for(@shipment, html: {class: "form-horizontal"}) do |f| %>
  <% if @shipment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>

      <ul>
      <% @shipment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :description %>

  <div class="field">
    <%= f.label :ship_from %><br>
    <%= f.select :from, ['New York']%>
  </div>

  <div class="field">
    <%= f.label :ship_to %><br>
        <%= f.select :to, [ 'New york', 'Orlanda' ] %>
  </div>
    <%= f.input :date %>
    <%= f.input :pay %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<div class="page-header">
<h1>Listing Shipments</h1>
</div>

<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>

<% @shipments.each do |shipment| %>
<div class="shipment">
  <h3><strong><%= shipment.user.first_name %></strong></h3>
  <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
  <div class="meta">
    <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
    <%= link_to "show", shipment %>
    <span class="admin"> 
        | <%= link_to "Edit", edit_shipment_path(shipment) %> |
        <%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
  </span>
    </div>
  </div>
  <% end %>

禁止保存此装运:


Index.html.erb

class Shipment < ActiveRecord::Base
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]

  # GET /shipments
  # GET /shipments.json
  def index
    @shipments = Shipment.all
  end

  # GET /shipments/1
  # GET /shipments/1.json
  def show
  end

  # GET /shipments/new
  def new
    @shipment = Shipment.new
  end

  # GET /shipments/1/edit
  def edit
  end

  # POST /shipments
  # POST /shipments.json
  def create
    @shipment = Shipment.new(shipment_params)

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

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

  # DELETE /shipments/1
  # DELETE /shipments/1.json
  def destroy
    @shipment.destroy
    respond_to do |format|
      format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def shipment_params
      params.require(:shipment).permit(:user_id, :description, :from, :to, :date, :pay)
    end
end
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|

      t.string :first_name
      t.string :last_name
      t.string :city_name

      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end



add_user_id_to_shipment.rb
class AddUserIdToShipments < ActiveRecord::Migration
  def change
    add_column :shipments, :user_id, :integer
    add_index :shipments, :user_id
    remove_column :shipments, :name
  end
end
<%= simple_form_for(@shipment, html: {class: "form-horizontal"}) do |f| %>
  <% if @shipment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipment.errors.count, "error") %> prohibited this shipment from being saved:</h2>

      <ul>
      <% @shipment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :description %>

  <div class="field">
    <%= f.label :ship_from %><br>
    <%= f.select :from, ['New York']%>
  </div>

  <div class="field">
    <%= f.label :ship_to %><br>
        <%= f.select :to, [ 'New york', 'Orlanda' ] %>
  </div>
    <%= f.input :date %>
    <%= f.input :pay %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<div class="page-header">
<h1>Listing Shipments</h1>
</div>

<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>

<% @shipments.each do |shipment| %>
<div class="shipment">
  <h3><strong><%= shipment.user.first_name %></strong></h3>
  <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
  <div class="meta">
    <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
    <%= link_to "show", shipment %>
    <span class="admin"> 
        | <%= link_to "Edit", edit_shipment_path(shipment) %> |
        <%= link_to "Delete", shipment, method: :delete, data: { confirm: "Are you sure?"} %>
  </span>
    </div>
  </div>
  <% end %>

货物清单

说明:
|
|  |
Show.html.erb


名称:

装运说明:

发货地点:

发送至:

装运日期:

支付:

|
我的命令行显示此日志:

Started GET "/" for ::1 at 2015-07-28 16:07:24 +0530
Processing by ShipmentsController#index as HTML
  Shipment Load (0.0ms)  SELECT "shipments".* FROM "shipments"
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT
 1  [["id", 1]]
  Rendered shipments/index.html.erb within layouts/application (0.0ms)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined method `first_name' for nil:NilClass):
     6:
     7: <% @shipments.each do |shipment| %>
     8: <div class="shipment">
     9:   <h3><strong><%= shipment.user.first_name %></strong></h3>
    10:   <h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
    11:   <div class="meta">
    12:         <%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |

  app/views/shipments/index.html.erb:9:in `block in _app_views_shipments_index_h
tml_erb__634505161_51269712'
  app/views/shipments/index.html.erb:7:in `_app_views_shipments_index_html_erb__
634505161_51269712'


  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
 (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within
 rescues/layout (31.2ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_markup.html.erb (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inline
d_string (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_s
tring (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/console.js.erb within layouts/javascript (31.2ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/index.html.erb (78.0ms)
于2015-07-28 16:07:24+0530开始获取“/”for::1
ShipmentsController#将索引作为HTML进行处理
装运装载(0.0ms)从“装运”中选择“装运”。*
用户加载(0.0ms)从“用户”中选择“用户”。*其中“用户”。“id”=?极限
1[[“id”,1]]
布局/应用程序中呈现的装运/index.html.erb(0.0ms)
在16毫秒内完成500个内部服务器错误(ActiveRecord:0.0毫秒)
ActionView::Template::Error(未定义nil:NilClass的“first_name”方法):
6:
7: 
8: 
9:
10:说明:
11:   
12:          |
app/views/shippings/index.html.erb:9:in`block-in\u-app\u-views\u-shippings\u-index\h
T