I have a 1:1 has_one / belongs_to relationship between users and registrations. One user has one registration.
When I try to iterate through users in a view and display their registration info (source to follow), I get the following error:
ActionView::TemplateError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.registration_code
Here's the offending view code:
<% #users.each do |user| %>
<%= user.registration.registration_code %>
<% end %>
In my users_controller.rb:
def users_registration_codes
#users = User.find(:all)
end
The likely issue here is that you're finding a particular User without an associated Registration - i.e. it's not that user == nil, but that user.registration == nil so it complains when you try to call registration_code() on the non-existent associated registration object
Try
<% #users.each do |user| %>
<%= user.registration.registration_code if user.registration %>
<% end %>
Related
I have a Rails model called User. There are two different types of users. Some users have company_profiles in a separate table.
In my controller for my page view, I'd only like to display those users who have company_profiles. I'd also like to display their user info and their company_profile info too.
I'm not sure how to handle this in my controller and view.
What should be in my index method?
def index
#users = User.scoped # ?????
end
And how do I loop through each user with a company profile on the index page?
<% #users.each do |user| %>
<p>
<%= user.email %>
<%= user.company_profile.poc_first_name %>
</p>
<% end %>
Now you mentioned you want to show users only for which company_profile exists. So in your controller method following should be in the index method
def index
#users = User.left_outer_joins(:company_profile).where("company_profiles.id is NOT NULL")
end
Then in your views you can get company_profile's poc first name as follows
<% #users.each do |user| %>
<p><%= user.email %>
<%= user.company_profile.poc_first_name %>
</p>
<% end %>
Making an INNER JOIN when loading the records from the database should work:
def index
#users = User.joins(:company_profile)
end
I want to show the user a part of the page if he has an attribute with a certain value.
something like this
<% if user.st == "Completed" %>
<p>just for him</p>
<% end %>
I get undefined method for nil class, how can I select the class in my views directly so I can access the attribute. I cannot use any params
edit: If I do this it works, but I want to check the attribute not if he is signed in
<% if user_signed_in? %>
<% end %>
thanks
In place of user use current_user, I'm assuming that it is for signed in user only
<% if current_user.st == "Completed" %>
<p>just for him</p>
<% end %>
You can do something like this:
In your controller:
#user = User.find(params[:id]) #or whatever user you want
In your view:
<% if #user.st == "Completed" %>
<p>just for him</p>
<% end %>
I use Devise gem for authentication.
In database I have users table and posts table in my database schema (and Post controller).
In post controller I want to find all posts assigned to specific user. I have user_id in posts table.
How to get all user's posts or how to check if specific post is assigned for SIGNED IN user.
I thought about something like this (of course is only pseudocode:
current_user.id == Post.where(params:[post_id]).user_id
So how to get current user id in Devise and how to check the current user id is the same like eg. user_id assigned to viewing post (I want to add 'edit' function when current user is post owner) and how to find all post which current user is owner.
Associations
Firstly, your user_id column in your posts table is what's known as a foreign_key
Foreign keys are used in relational database systems to give you the ability to call associative data from a single record. Simply, it means that you'll be able to use the ActiveRecord associations to call the data you require, rather than having to call it individually:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :posts
end
#app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
end
This will give you the ability to use the following call:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
#posts = current_user.posts
end
end
You'll be best served looking up the has_many association:
Fix
In regards to showing your posts for your users, you need to be sure that you have the correct "flow" set up. What I mean is you need some condition to know whether your user is signed in & that #posts is set:
#app/views/posts/index.html.erb
<% if #posts.present? %>
<% #posts.each do |post| %>
<%= post.title %>
<% end %>
<% end %>
Maybe this is the first time you use Devise. You can access current_user inside controllers or views. I imagine you could do something like this
In controller (posts_controller.rb):
#posts = current_user.posts
In view (posts/show.html.erb, I guess):
if current_user.id = #post.current_user
#render something here
end
Get all post which current user is owner.
#posts = Post.where(:user_id => current_user.id)
and on your view
<%-# commented : checking if #posts is empty -%>
<% if #posts.empty? %>
<span>Sorry, post is empty </span>
<% else %>
<%= #posts.each do |p| %>
<% if p.user_id == current_user.id %>
<% link_to "edit", edit_path(p) %>
<% end %>
<% end %>
<% end %>
There are many ways you could get current_user posts. I'll go the long way.
we need
an action
an action view and a partial
a route
a link_to
* action *
def my_posts
#posts = current_user.posts.all.order(created_at: 'DESC')
end
* view *
my_posts.html.erb
<% if #posts.present? %>
<%= render 'posts' posts: #posts %>
<% else %>
<h1>You don't have any posts yet! create one</h1>
<% end %>
_posts.html.erb
<%posts.each do |post| %>
<%= post.title %>
<% end %>
index.html.erb
<%= render 'posts' posts: #posts %>
route
get 'post' => 'posts#my_posts', as: :my_posts
link_to
<%= link_to 'My posts', my_posts_path %>
I may be late but someone can find it useful :)
I am trying to validate what kind of role does the current logged in user has.
Role is a string column on the Users Model.
I am trying this:
<% if current_user.try(:role == 'admin') %>
#Show something
<% end %>
And I get the following error:
TypeError in Merchants#index
false is not a symbol
I have Devise as my authentication system, that is why I am using the current_user method
I used to have a boolean User.admin and it worked like this:
<% if current_user.try(:admin?) %>
#Show something
<% end %>
I need to change to a string field since I will have several roles.
It should be
<% if current_user.try(:role) == 'admin' %>
The relation is that a user has many treatments and a treatment belong to user, one-to-many.
Now i want to print out all the users that have this particular treatment
Inside my treatments show view i have this double loop
<% User.all do |user| %>
<%= user.treatments.each do |t| %>
<% if (t.id).to_i == (#treatment.id).to_i %>
<%= link_to user.name, user_path(user) %><br />
<% end %>
<% end %>
<% end %>
if i change <% User.all do |user| %> to <%= User.all do |user| %> it prints out everything in my users table
can you guys spot why im not getting any users ?
i put a message in the beginning of the inner loop and it didnt display either, guess the problem is there but im not seeing it
.all returns an array. Array doesn't accept a block. Most likely, you want to do .each but forgot to write it. Try this:
<% User.all.each do |user| %>
but a better way is to not iterate all users like this, but get the correct list from the database directly.