여기 다시 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이 내 양식이입니다 :
감사
이 오류가 10class 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
처럼
는보십시오. "unidentified method ... for nil : NilClass"오류는 작업 할 데이터가 없음을 의미합니다.이 경우'@ tuto.user'는 nil입니다. –
그래, 마지막으로 @CharanKumarBorra에서 가져왔다. 도와 주셔서 다시 한번 감사드립니다. –