2016-09-17 6 views
0

여기 다시 old question입니다. 도움을 받아 문제를 해결했지만 "tuto"를 게시 한 사용자의 이름을 표시 할 수 없습니다. 이전에 나는이 작업을 수행하면서 tuto 생성에서 user_id를 가져올 수 없었습니다. 네가 도울 수 있기를 바란다.사용자 이름을 표시 할 수 없습니다

= 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

,536,913,632이 내 양식이 undefined method for nil class

입니다 :

감사

이 오류가 10
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 TUTO을 만드는 동안 실제로 당신이 사용자 정보를 저장하지 않는

.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

처럼

는보십시오. "unidentified method ... for nil : NilClass"오류는 작업 할 데이터가 없음을 의미합니다.이 경우'@ tuto.user'는 nil입니다. –

+0

그래, 마지막으로 @CharanKumarBorra에서 가져왔다. 도와 주셔서 다시 한번 감사드립니다. –

답변

1

index.html.slim

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 /.

@tuto 개체를 저장하기 전에 user_id을 업데이트해야합니다. 아직 정확하게 사용자에게 연결 아니에요이

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 
+0

그게 내가 놓친 거예요! 이 작품은 완벽하게 작동합니다! 대단히 감사합니다 !! –

관련 문제