2017-02-11 73 views
0

好吧,所以可能是一個非常簡單的問題來回答我的猜測。我似乎已經破壞了我的應用程序的一個功能,該功能顯示提交帖子的人的用戶名(在我的例子中是'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

+1

你應該調整你的問題是一個最小的完整可驗證例如:http://stackoverflow.com/help/mcve。現在,任何能夠幫助你的人都必須通過你的整個代碼庫來了解發生了什麼。 – JChrist

+1

@JChrist感謝您的提示!我會看看我可以如何清理它。 – Ultralight

+0

對不起。我不太清楚haml,但是,在您的代碼上瀏覽我在pins_controller中找到了「attr_accessor:username」。似乎沒有任何關於你的模式的參考。也許做一些「干擾」? – rfellons

回答

0

刪除

attr_accessor :username 

def username=(username) 
    @username = username 
end 

user.rb模型文件,它攪亂了的ActiveRecord

+1

謝謝。我感謝您的幫助! – Ultralight

0

你的問題不是haml它是你試圖顯示的用戶名似乎是空的。你的哈姆結構看起來很好。 正如您在pin_params中所述,不允許username,所以請確保允許並將其保存到數據庫中。比你的哈姆應該工作。

+0

我已經更新了我pins_controller包括: \t高清pin_params \t \t params.require(:引腳).permit(:標題,:說明:圖片,:用戶名) \t年底 和檢查,如果後用戶擁有@u = User的用戶名。它全部顯示用戶名: created_at:「2017-02-06 18:47:44」,updated_at:「2017-02-11 12:54:04」,用戶名:「ultralight」> 但是我的索引.html.haml仍然不顯示創建引腳的人的用戶名。 – Ultralight

+0

您應該確保您可以在控制檯中訪問像這樣的用戶名。 pin.user.username。哈姆很好。 –