Reputation: 502
Good morning everyone! I am working on a rails assignment, and I am having some trouble rendering my items partial within a user profile. I believe the problem is that the @item variable is not initializing when within the user's views? Whenever I add a new item to a user's list, it should be attributed to him/her, and show up. At this point, I know the item is being created and specific to the user, but does not show. Here is some of my code:
Item Model:
class Item < ActiveRecord::Base
belongs_to :user
end
User Model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :items
end
Items Controller:
class ItemsController < ApplicationController
def new
@user = User.find(params[:user_id])
@item = Item.new
end
def create
@user = User.find(params[:user_id])
@item = Item.new(params.require(:item).permit(:name))
@item.user_id = @user.id
if @item.save
flash[:notice] = "Item was saved."
redirect_to [@user, @item]
else
flash[:error] = "There was an error saving the item. Please try again."
render :new
end
end
def show
@user = User.find(params[:user_id])
@item = Item.find(params[:id])
end
end
Users Controller:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@item = @user.items
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
_item.html.erb partial:
<%= @item.name %>
<br>
At this point, it only shows item on the profile view.
User#show:
<br>
<div class="container">
<div class="row">
<div class="col-md-11">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-12 lead">User profile<hr></div>
</div>
<div class="row">
<div class="col-md-4 text-center">
<img class="img-circle avatar avatar-original" style="-webkit-user-select:none;
display:block; margin:auto;" src="http://robohash.org/sitsequiquia.png?size=120x120">
</div>
<div class="col-md-8">
<div class="row">
<div class="col-md-12">
<h1 class="only-bottom-margin"><%= @user.email%></h1>
</div>
</div>
<div class="row">
<div class="col-md-6">
<span class="text-muted">Name:</span> <%= @user.name%><br>
<span class="text-muted">Id:</span> <%= @user.id %><br>
</div>
<div class="col-md-6">
<h2>User's To-Do List</h2>
<%= render '/items/item' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Thank you for your help, I couldn't have gone this far without the SO community.
Upvotes: 0
Views: 115
Reputation: 1601
You can modify the code like this:
users_controller.rb
def show
@user = User.find(params[:id])
@items = @user.items
end
users/show.html.erb
<div class="col-md-6">
<h2>User's To-Do List</h2>
<%= render partial: 'items/item', locals: {items: @items} %>
</div>
itemss/_item.html.erb
<% items.each do |item| %>
<%= item.name %>
<% end %>
Upvotes: 1
Reputation: 3477
You can try this, I hope this will help you.
Users Controller:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@items = @user.items
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
_item.html.erb partial:
<% @items.blank? %>
<% @items.each do |item| %>
<%= item.name %>
<% end %>
<% else %>
<h2> No Items Found. </h2>
<% end %>
User#show:
<div class="col-md-6">
<h2>User's To-Do List</h2>
<%= render 'items/item', object: @items %>
</div>
Upvotes: 0
Reputation: 4032
Try changing the following:
users_controller.rb
def show
@user = User.find(params[:id])
@items = @user.items
end
_item.html.erb
<% unless items.blank? %>
<% items.each do |it| %>
<%= it.name %>
<br/>
<% end %>
<% end %>
show.html.erb
<%= render 'items/item', items: @items %>
Upvotes: 0