好吧,所以可能是一個非常簡單的問題來回答我的猜測。我似乎已經破壞了我的應用程序的一個功能,該功能顯示提交帖子的人的用戶名(在我的例子中是'pin')。Post.Username不會顯示內部哈姆?
這一切都發生在我將用戶名添加到數據庫(devise)的時候,因爲之前我只顯示電子郵件地址,並且覺得這不是我想要的。
鉈;博士:= pin.user.username(應用程序/視圖/銷/ index.html.haml)不顯示誰提交的職位的人的用戶名。但是,它會顯示電子郵件pin.user.email。
這裏是我的PinsController(針在我的情況是郵政色器件)
class PinsController < ApplicationController
before_action :find_pin, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show]
attr_accessor :username
def index
@pins = Pin.all.order("created_at DESC")
end
def show
end
def new
@pin = current_user.pins.build
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: "Fit was successfully created"
else
render 'new'
end
end
def edit
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: "Fit was successfully updated"
else
render 'edit'
end
end
def destroy
@pin.destroy
redirect_to root_path
end
def upvote
@pin.upvote_by current_user
redirect_to :back
end
private
def pin_params
params.require(:pin).permit(:title, :description, :image)
end
def find_pin
@pin = Pin.find(params[:id])
end
end
這裏是我的index.html.haml
#pins.transitions-enabled
- @pins.each do |pin|
.box.panel.panel-default
= link_to (image_tag pin.image.url), pin
%h2= link_to pin.title, pin
%p.user
Submitted by
= pin.user.username
我的模式顯示了這種對用戶
add_index "pins", ["user_id"], name: "index_pins_on_user_id"
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
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"
t.datetime "created_at"
t.datetime "updated_at"
t.string "username"
end
我已經附上了我的git,因爲您需要一些具體的細節 https://github.com/thgilartlU/MyFitIdentity
你應該調整你的問題是一個最小的完整可驗證例如:http://stackoverflow.com/help/mcve。現在,任何能夠幫助你的人都必須通過你的整個代碼庫來了解發生了什麼。 – JChrist
@JChrist感謝您的提示!我會看看我可以如何清理它。 – Ultralight
對不起。我不太清楚haml,但是,在您的代碼上瀏覽我在pins_controller中找到了「attr_accessor:username」。似乎沒有任何關於你的模式的參考。也許做一些「干擾」? – rfellons