ruby on rails - My index file is only showing one database entry -
i'm trying list "pits" bascially same thing "posts" in app. however, pits/index showing first pit created versus listing of them. can't see errors in view or controller. have feeling small , may need pair of eyes point out me. thanks.
index.html.erb
<div class = "container list-pits"> <% @pit.each |pit| %> <div class = "row"> <div class = "container"> <div class = "well pit-well"> <h3 id="pit-title"><%= link_to pit.topic, pit_path(pit) %></h3> <br> <p><%= pit.summary %></p> <p>replies (<%= pit.comments.count %>)</p> <br> <p>pit created by: <%= link_to pit.user.name, current_user %> on <%= pit.created_at %></p> <%= link_to "join pit", '#', class: "btn btn-primary" %> </div> </div> <% end %> </div> </div>
pits_controller
class pitscontroller < applicationcontroller def new @pit = pit.new end def index @pit = pit.all @user = user.find_by(params[:id]) @pit = @user.pits @pits = pit.order('created_at desc').group_by { |pit| pit.created_at.strftime("%b %y") } end def create @user = current_user @pit = current_user.pits.create(pit_params) if @pit.save redirect_to @pit else render 'new' end end def show @pit = pit.find(params[:id]) end def edit end def update end private def pit_params params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id) end end
def index @pit = pit.all @user = user.find_by(params[:id]) @pit = @user.pits @pits = pit.order('created_at desc').group_by { |pit| pit.created_at.strftime("%b %y") } end
firstly, dont need @pit = pit.all
, because overwriting @pit
@pit = @user.pits
. guess @user
chosen id (@user = user.find(params[:id])
) has 1 object of pit type.
if want show pits, remove @pit = @user.pits
, keep @pit = pit.all
ps: in create action there current_user.pits.create(pit_params)
, after trying @pit.save
. should change first 1 current_user.pits.new(pit_params)
Comments
Post a Comment