Current user information is displaying on all profiles Ruby on Rails - ruby-on-rails

So I am making a Social media website where user join events and compete against each other.
Each time a user join an event a status appears on his profile. The issue is that when I look at other user profiles, the statuses just match who ever the current user is.
If someone could help me write some code that shows the different statuses for each of the users that would be great. I know the question may sound a little vague.
I just need to know a way to see only post based on which profile I am viewing not the current user. Thanks!
<div class="center">
<div class="col-xs-6">
<div class="panel panel-default">
<div class="panel-body">
<% if !#user_events.present? %>
<h4>Hi <%= User.find_by_username(params[:id]).username %>, thanks for joining!</h4>
<h4>Join events, complete them, and compete</h4>
<% end %>
<% #user_events.each do |ue|%>
<% if ue.event.present? %>
<h4 class="blue">
<%= User.find_by_username(params[:id]).username %>
</h4>
<% userevent = User.find_by(params[:event]) %>
<p>I'm going to <%= link_to ue.event.title %>.</p>
<% if ue.hours.present? %>
<h5> Completed Hours: <%= ue.hours %> </h5>
<% end %>
<% if ue.hours.present? %>
<img src="#" style="width: 10%;" alt="">
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>

First of all, it's very bad to have queries to the database inside of your view. Please, move expression inside <%= User.find_by_username(params[:id]).username %> to your controller and then use instance variable to show username.
It would be great if you provide your controller code too. I guess you should pick user events depending on the user you found by params[:id].

Without the controller, it is a bit hard to help. But I would suggest trying something on the user controller like
def get_status_by_user
user_id = params[:user_id]
user = User.find_by(id: user_id)
user.event
end
where the params[user_id] is the id of hte user you want to get the event.
Also what I can see is <% userevent = User.find_by(params[:event]) %> you do not specify a user so the method will return the last one
Finds the first record matching the specified conditions.
Find_by docuemntaion
So if you want to keep your code mostly as it is you need to first find the user you want to get the event and then get the status for that specific user
Also giving a little information on the DB schema could help

Related

Rails HTML Multi Layered IF checking

I am working on a layout that requires a couple of checks to the database, but I cannot figure out the logic on the IF statement. I have a People table where each record holds a Position field. There are times that specific positions will be held by two people. In which cases they want to be Co-Position. So I current am listing said Position on my page with:
<li>
<% #people.each do |person| %>
<% if person.position == 'Office1' %>
<div class="image">
<%= link_to(person_path) do %>
<%= image_tag("profiles/#{person.uname}S.jpg") %>
<% end %>
</div>
<div class="body2">
<h4>Office</h4>
<%= link_to(person_path) do %>
<h5><%= person.fname %> <%= person.lname %></h5>
<% end %>
</div>
<% end %>
<% end %>
</li>
Now within that I need to run a check at <h4>Office</h4> to see if another record holds Office2, but if I place an IF statement around the H4 tags will it be checking the already fetched record or will it check the entirety of the Person database? Do I need to do this check first and then do an IF/Then or can this be done in a more streamlined way?
Basically I need to check if Office2 is present somewhere in the Position field, and if it is I need the H4 to read Co-Office, else I need it to read Office.
If your condition is if person.position.include?('Office2') then no, it won't touch the database again. The person object is loaded with all its attributes.

How to access specific object fields?

I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>

How to create more than one index page in Ruby on Rails

I have two different plans (plan with an ID of 1 and plan with an ID of 2). I've created a partial for each to show on the home page based on which plan the user is logged in as. I would like to create two different index pages for each plan to be directed to.
Plan ID 1 users need to be directed to an index of users with a plan ID of 2, and plan ID 2 users need to be directed to an index of users with a plan ID of 1. Here's the part of the code that controls this feature. How can I create an index feature which sends plan ID 1 and plan ID 2 user to different pages after click on the relative partial?
pages/home.html.erb
<div class="col-md-6">
<% if current_user.plan.name == "mentor" %>
<%= render partial: "pages/mentor" %>
<% else %>
<%= render partial: "pages/mentee" %>
<% end %>
</div>
pages/_mentee.html.erb
<div class="well">
<h2 class="text-center">Mentor Community</h2>
<h4 class='text-center'>Get the support you need.</h4>
<br><%= link_to "Find a Mentor", "#", class: 'btn btn-default btn-lg btn-block' %>
</div>
pages/_mentor.html.erb
<div class="well">
<h2 class="text-center">Mentee Com</h2>
<h4 class='text-center'>Give the support that's needed.</h4>
<br><%= link_to "Find a Mentee", "#", class: 'btn btn-default btn-lg btn-block' %>
</div>
Do this:
#config/routes.rb
root "pages#home"
resources :plans, only: :show #-> url.com/plans/1
#app/controllers/plans_controller.rb
class PlansController < ApplicationController
def show
#plan = Plan.find params[:id]
end
end
#app/views/plans/show.html.erb
<% #plan.users.each do |user| %>
<%= user.name %>
<% end %>
after click on the relative partial
You could send users to the specific plan page by using the following:
#app/views/pages/home.html.erb
<% #plans.each do |plan| %>
<%= link_to plan.id, plan %>
<% end %>
There is so much more you need to consider, but for now, the above should help you get an understanding on the overall structure.
Let me explain a little on how you need to adapt your thinking (this might seem off topic, but will help you profusely, I guarantee it)...
The above is how Rails is meant to work -- it takes a request from the user, matches it to a controller action, and then populates it with model data.
The correct way for Rails to work is with something called object orientated programming - each time you initiate an action / request, it has to invoke & manipulate objects of data.
Whilst this may seem complicated, the sooner you get your head around it, the quicker you'll be able to make much more intricate rails applications.
--
Your question implies that you've not considered the full potential of a data-driven rails application.
Not that it matters, but if you changed your approach so that you got rid of pages and instead had plans with users, you'd be able to have as many plans as you required.
That is the correct way to think about programming (to make a system, not just a quick fix), which you can then use to expand the functionality as you need.
A good way to remove the conditional would be using the plan name to find the partial. It would be something like this
<div class="col-md-6">
<%= render partial: "pages/#{current_user.plan.name}" %>
</div>
But you need to make sure that each plan has its correct partial file. Otherwise you'll get a rendering error, because the partial isn't found.

Combining two associations in my "each do" loop method

I'm a little new to back-end programming...I'm currently running the following code in my rails 4 app to show a basic list of all the admins on a project (if there are any)...
<% if project.projectadmins.any? %>
<div class="row-fluid">
<% project.projectadmins.each do |user| %>
<div class="collaborator">
<%= link_to user do %>
<%= image_tag user.image_url(:thumb).to_s, :class => "profile-pic-thumb" %>
<% end %>
</div>
<% end %>
</div>
<% end %>
However, I also have projectcollaborators for each project, so I'd like to know what the most effective way would be to combine those and provide a list of both projectadmins AND projectcollaborators (with project admins being listed first if there are any...other than that ordering is not important).
I assume the if statement at the beginning would change to...
<% if project.projectadmins.any? || project.projectcollaborators.any? %>
but I'm not 100% sure and am lost on the rest...any help is much appreciated.
You could create a scope, for example project_admins_and_collaborators, which gets all the needed records and then use it in your loop.
You can also do this in following way
project_admins_and_collaborators = project.projectadmins
project_admins_and_collaborators << project.projectcollaborators
project_admins_and_collaborators.flatten.uniq do |user|
#your code
end

combining 2 tables, looping through the results and displaying items from each

So I'm trying to combine two tables and show the results in order of the start_date.
I've tried a few things but because its technically a nested loop its giving me double results for each item.
The code i currently have is as follows
<% #subcategory = Subcategory.all %>
<% #product = Product.all %>
<% (#product + #subcategory).each do |product, subcategory|%>
<% if product.display_on_home_page and !product.is_highlight_product and !(product == '..') or subcategory.
display_on_home_page and !subcategory.is_highlight_product and !(subcategory == '..')%>
<div class="column_entry">
<%= link_to image_tag(subcategory.image_attachment.url(:normal_page_size)), subcategories_content_url(subcategory.id), :controller=>'subcategories' %>
</div>
<% end %>
<% if product.price_from %>
<div class="column_entry">
<div class="product_special">
<span class="a">From Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% else %>
<div class="column_entry">
<div class="product_special">
<span class="a">Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% end %>
<% end %>
I know this is quite a long an complex statement, its supposed to loop through all of the subcategories and all of the products and display the images, there are also two different ways of displaying the price based on a boolean that says whether the price is a specific amount or it starts from a given price.
at the moment its reading through the loop but its giving me the error
undefined method `is_highlight_product' for nil:NilClass
since this is the first column in the table that is referenced and its breaking here I think that there must be some conflict in its ability to see the information stored in the table.
I'm still quite new to ruby on rails so any help or even just a nudge in the right direction would be very much appreciated.
If you would like more information just ask in the comments and I'll put it up as fast as I can.
The problem here is, when you do something like this:
(#product + #subcategory).each do |product, subcategory|
The local variable product will iterate firstly through products, then through subcategories, and the local variable subcategory will always be nil.
What you can do, a dirty way - check
if product.is_a?(Product)
# do your things
elsif product.is_a?(Subcategory)
# do other things
end

Resources