2017-02-11 14 views
0

私はおそらく、本当にまっすぐな質問に答えてくれるはずです。私はポストを提出した人のユーザ名を表示する私のアプリの機能を壊しているようだ(私の場合は「ピン」)。Post.Usernameはhaml内に表示されませんか?

これは以前私が電子メールアドレスを表示していて、それが本当に私が望んでいないと感じたように、ユーザー名をデータベース(デバイス)に追加した頃に起きました。

Tlの; DR:= 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

を台無しに。今のところ、何が起きているのかを知るために、コードベース全体を調べなければならない人は誰でも助けてくれるかもしれません。 – JChrist

+1

@JChristヒントありがとう!私はこれをどのようにきれいにすることができるか見ていきます。 – Ultralight

+0

申し訳ありません。私はよく知らないけど、あなたのコードをサーフィンして、あなたのpins_controllerに "attr_accessor:username"が見つかりました。あなたのスキーマについての参照がないようです。おそらく、いくつかの "干渉"をするでしょうか? – rfellons

答えて

0

あなたuser.rbモデルファイルから

attr_accessor :username 

def username=(username) 
    @username = username 
end 

を削除し、それはあなたが最小限の完全な検証例であるためにあなたの質問を再構築すべきであるActiveRecordの

+1

ありがとうございます。私はあなたの助けに感謝します! – Ultralight

0

あなたの問題は、表示しようとしているユーザー名が空である可能性があります。あなたのhaml構造はうまく見えます。 pin_paramsに記載されている通り、usernameは許可されていませんので、許可してDBに保存してください。あなたのハムよりはうまくいくはずです。 http://stackoverflow.com/help/mcve:

+0

私は含めるように私のpins_controllerを更新しました:(:ピン).permit(:タイトル:説明:イメージ、:ユーザ名)デフ \t \t params.require pin_params \tを \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。ハムルは大丈夫です。 –

関連する問題