2017-02-19 9 views
1

Meine Frage ist, wie ich Kommentare + Benutzerinformationen aufrufen kann, die für den spezifischen Beitrag angegeben sind, unter dem der Kommentar erstellt wurde. Zum Beispiel:Benötigen Sie Hilfe beim Aufruf bestimmter Kommentare aus der Datenbank?

/articles/8 

new comment created with user_id = 3 

Page will show Username + comment + created_at 

Dies ist mein aktueller Code:

Beitrag anzeigen Seite

<%= @post.title %> 
<%= render '/comments/form' %>  
<% @post.user.comments.reverse.each do |comment| %> 
    <li> 
    <%= comment.user.email %> 
    <%= comment.comment %> 
    </li> 
<% end %> 

Von dem, was ich geschrieben habe, ich bin in der Lage mit dem Kommentar zugeordnet Benutzerinformationen greifen aber das Problem ist, es listet alle Kommentare auf. Wie gehe ich vor, um Kommentare mit article_id von 8 zum Beispiel nur erscheinen zu lassen.

Weitere Code bitte unten aussehen:

Beitrag Controller-

def create 
@post = Post.new(post_params) 
@post.user = current_user 
if @post.save! 
    flash[:notice] = "Successfully created..." 
    redirect_to posts_path 
else 
    flash[:danger] = "failed to add a post" 
    render 'new'  
end 
end 

def show 
@comment = Comment.new 
@comments = Comment.find(params[:id]) 
end 

Kommentar-Controller

def create 
@post = Post.find(params[:post_id]) 
@comment = @post.comments.build(comment_params) 
@comment.user = current_user 

if @comment.save 
    flash[:notice] = "Successfully created..." 
    redirect_to post_path(@post) 
else 
    flash[:alert] = "failed" 
    redirect_to root_path 
end 
end 

Routen

resources :sessions, only: [:new, :create, :destroy] 
resources :users 
resources :posts do 
resources :comments 
end 

Wenn zusätzliche Dateien benötigt werden, um mich bei diesem Problem zu helfen, zögern Sie nicht zu fragen. Ich hoffe auf ein besseres Verständnis beim Aufrufen von Daten wie dem aktuellen Problem, das ich habe.

Vielen Dank noch einmal im Voraus an alle.

EDIT

create_table "comments", force: :cascade do |t| 
    t.text  "comment" 
    t.integer "user_id" 
    t.integer "post_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 
+0

können Sie das Schema der Kommentare teilen? – MZaragoza

+0

@MZaragoza Mein Schema hinzugefügt. –

+0

und Sie möchten alle Kommentare für Post 8? – MZaragoza

Antwort

1

Ich gehe davon aus, dass Ihr Modell wie

class Comments < ApplicationRecord 
    belongs_to :user 
    belongs_to :post 
end 
class User < ApplicationRecord 
    has_many :posts 
    has_many :comments 
end 
class Post < ApplicationRecord 
    belongs_to :user 
    has_many :comments 
end 

sieht Wir wollen, dass die Kommentare zu einem Beitrag alle bekommen, damit wir uns so etwas wie dieses

tun können
# Note that I took the user from this line 
<% @post.comments.reverse.each do |comment| %> 
    <li> 
    <%= comment.user.email %> 
    <%= comment.comment %> 
    </li> 
<% end %> 

Ich hoffe, dass dies funktionieren sollte.

+0

funktioniert perfekt. Vielen Dank. Nur eine Frage. Wie wird auf Benutzerinformationen zugegriffen, wenn das Modell nicht aufgerufen wird? liegt es daran, dass es Teil des Kommentars ist, damit wir darauf zugreifen können? –

+0

ein Beitrag haben sowohl Benutzer und Kommentare. Du siehst also die falsche Zuordnung – MZaragoza

Verwandte Themen