无法显示用户名

时间:2016-09-17 19:40:51

标签: ruby-on-rails ruby-on-rails-4

我再次使用old question。在你的帮助下,我解决了一些麻烦,但我仍然无法显示已发布“tuto”的用户的名字。以前我无法在tuto创建中获得user_id,现在这项工作。我希望你能帮忙!

此致

我有这个错误:undefined method for nil class

这是我的表格:

= simple_form_for @tuto do |f|
  - if @tuto.errors.any?
    #error_explanation
      h2 = "#{pluralize(@tuto.errors.count, "error")} prohibited this tuto from being saved:"
      ul
        - @tuto.errors.full_messages.each do |message|
          li = message
  = f.hidden_field :user_id, value: current_user.id
  = f.input  :title
  = f.input  :content    
  = f.button :submit, "Save"

tutos_controller:

class TutosController < ApplicationController
  before_action :authenticate_user!
  before_action :set_tuto, only: [:show, :edit, :update, :destroy]


  def index
    @tutos = Tuto.all.includes(:user)
  end

  def show
    @tuto = Tuto.find(params[:id])
  end


  def new
    @tuto = Tuto.new
  end

  def edit
  end

  def create

    @tuto = Tuto.new(tuto_params)
    respond_to do |format|
      if @tuto.save
        flash[:success] = "Test"
        format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
        format.json { render :show, status: :created, location: @tuto }
      else
        format.html { render :new }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    respond_to do |format|
      if @tuto.update(tuto_params)
        format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' }
        format.json { render :show, status: :ok, location: @tuto }
      else
        format.html { render :edit }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @tuto.destroy
    respond_to do |format|
      format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # def get_user
    #   @user = User.find(@tuto.user_id)
    # end

    def set_tuto
      @tuto = Tuto.find(params[:id])
    end

    def tuto_params
      params.require(:tuto).permit(:title, :content, :id, :user_id)
    end
end

users_controller

class UsersController < ApplicationController
  before_action :authenticate_user!
  def show
    @user = User.find(current_user)
  end

  def index
    @user = User.all
  end

  private
  def params_user
    params.require(:users).permit(:first_name, :last_name, :email, :id)
  end
end

tuto_model

class Tuto < ActiveRecord::Base
  belongs_to :user

end

user_model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates :first_name, presence: true
  validates :last_name,  presence: true
  has_many :tutos

  def full_name
    "#{last_name} #{first_name}"
  end

end

和视图tutos / index.html.slim

.container
  .row
    .col-xs-12.col-sm-12
      h1.text-gray Tutorials 
      br
      -if user_signed_in?
        = link_to "Create a tuto", new_tuto_path, class:"btn btn-success"
  .row

    table.board
      thead
        tr
          th Title
          th User

    hr

      tbody
        - @tutos.each do |tuto|
          .row
            .col-xs-4
              h6 = link_to tuto.title, tuto_path(tuto)
            .col-xs-4 
              h6 = tuto.user.full_name
    hr

1 个答案:

答案 0 :(得分:1)

实际上,在创建Tuto时,您没有保存用户信息。

您需要在保存user_id对象之前更新@tuto

试试这个

def create
    @tuto = Tuto.new(tuto_params)

    #add this line to capture user for your tuto.
    @tuto.user_id = current_user.id

    respond_to do |format|
      if @tuto.save
        flash[:success] = "Test"
        format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
        format.json { render :show, status: :created, location: @tuto }
      else
        format.html { render :new }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
end