ruby - Rails partial template with collection not rendering correctly -
i have been troubleshooting error hours , have no idea i’m doing wrong. can’t items collection display individual user. each item belongs single user , each user has many items. right now, partial not rendering on users show view.
if change <%= render partial: "/users/item", collection: @user.items %>
<%= render partial: "/users/item", collection: @items %>
partial template rendered, items displayed regardless of user belong to. after checking database, each item entry being stored foreign key user_id, isn’t issue.
how correct items display each user? have used partials several times before , i’ve cross-referenced old , new code. can’t see difference , i’m losing mind. i’ve searched general internets, stack overflow, , reviewed rails guides layouts , rendering. i've posted relevant code below , happy post else. please help!
views/users/_item.html.erb
<div> <h4>item: <%= item.title %></h4> <p class="item"><%= item.cost %></p> </div>
views/users/show.html.erb
<h1>users#show</h1> <p>find me in app/views/users/show.html.erb</p> <div><%= @user.first_name %></div> <h3>my list</h3> <%= render partial: "/users/item", collection: @user.items %> <span>add item <%= link_to image_tag("plus-50.png"), new_user_item_path(@user), class: "items-nved" %> </span>
controllers/users_controller.rb
class userscontroller < applicationcontroller before_action :set_user, only: [:show, :edit, :update, :destroy] def index @users = user.all render :index end def show @user = user.find(params[:id]) # @item = item.find(params[:id]) @items = item.all @item = item.new @item.user_id = @user.id end def new @user = user.new end def create @user = user.new(user_params) if @user.save redirect_to new_session_path, method: :get else render "new" end end def edit end def update end def destroy end private def set_user @user = user.find(params[:id]) end def user_params params.require(:user).permit(:first_name, :last_name, :birthday, :city, :state, :email, :password, :password_confirmation) end end
controllers/items_controller.rb
class itemscontroller < applicationcontroller include itemshelper before_filter :require_login, only: [:new, :create, :update, :destroy] def index @current_user = current_user @items = item.all end def show @user = current_user @item = item.find(params[:id]) end def new @user = current_user @item = item.new end def create @user = current_user @item = item.new(item_params) @item.user_id = params[:user_id] # set user_id param database current_user id @item.save redirect_to user_item_path(@user, @item) end def edit @user = current_user @item = item.find(params[:id]) end def update @user = current_user @item = item.find(params[:id]) @item.update(item_params) redirect_to user_path(@user) end def destroy @user = user.find params[:user_id] @item = item.find(params[:id]) @item.destroy redirect_to user_path(@user) end end private def user_params params.require(:user).permit(:first_name, :last_name, :birthday, :city, :state, :email, :password, :password_confirmation) end def require_login unless logged_in? flash[:error] = "you must logged in access section" redirect_to new_login_url # halts request cycle end end
install byebug gem, either running gem install byebug
on command line or putting byebug in gemfile (then run bundle install
).
put byebug
after @user declaration in userscontroller, this:
class userscontroller < applicationcontroller before_action :set_user, only: [:show, :edit, :update, :destroy] def index @users = user.all render :index end def show @user = user.find(params[:id]) byebug
then start local server , go user show view in browser. page won't render, because when hits byebug
server pauses.
when happens, go terminal , @ server log. waiting commands. type @user
, press enter. return?
also try @user.inspect
. return user object?
then @user.items
. return? if @user.items
returns expected collection of items, there wrong how passing view, or you're doing in view.
if @user.items
doesn't return collection of items, that's why isn't showing in view! in case, it's activerecord associations and/or migrations.
another possibility current items in database may have been created before had associations setup, won't have user_id. can check in rails console. make sure have items in database have same user_id current_user.
another thing in rails console assign user, think has items, variable eg user = user.first
, , user.items
. work?
i recommend writing rspec tests, @ least @ model level test proper associations exist. shoulda gem great this.
comment here findings.