显示发布帖子的用户的姓名

时间:2015-08-03 17:49:36

标签: ruby-on-rails-4 devise

我正在创建一个rails应用程序,其中我具有用户可以使用设计创建帐户的功能,然后发布货件。我已经这样做了它可以显示货物,但如何显示发货人的名字?

我的观点

index.html.erb
<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: "Hey! Are you sure! You wanna delete this shipment??"} %>
  </span>
    </div>
  </div>
  <% end %>

我的货件控制器

class ShipmentsController < ApplicationController
  before_action :set_shipment, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # 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(:name, :description, :from, :to, :date, :pay)
    end
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
         has_many :shipments
end

货件型号

class Shipment < ActiveRecord::Base
    belongs_to :user
end

在我的货件索引视图文件中添加shipment.user.first_name后,我的应用程序发出错误:

undefined method `first_name' for nil:NilClass

1 个答案:

答案 0 :(得分:0)

首先,您需要通过向var test = 'Hello world my name is Amy Anderson.'; var atest = test.split(' '); var result = ''; var i = 0; var maxlength = 13; do { result = result + ' ' + atest[i]; i += 1; } while (atest.length >= i && result.length < maxlength); document.body.innerHTML = result + ' ...';模型添加user_id列来完成数据库关联。这可以通过终端来完成。

Shipment

然后输入下一个命令

rails g migration AddColumnUserIdToShipment user_id:integer

更新数据库。然后在您的控制器中将以下行rake db:migrate 更改为@shipment = Shipment.new(shipment_params)

这将使用当前用户的ID填写user_id。