Display Elements Based On Logged In User (Ruby on Rails) - ruby-on-rails

I am stuck here. LOL
Basically, I am working on a new RoR project and curious to figure this step out.
I am creating a Dating site for Sugar Daddies/Babbies. (Paid project by another person).
Basically, when a "Sugar Baby" logs in, they can set their;
Date Price | Text Price
What I am trying to accomplish is either;
<% if user = User.find_by_email('email_address') %>
<%= render 'users/paypal_buttons/user_email_paid %>
<% end %>
It works for the most part.
However, when I add more than 1 user find by email, the display page shows ALL users paypal buttons.
Please advise.
Second option.
Integrate PayPal in order to allow logged in users to create their own buttons in order to set the payment prices.
I appreciate your efforts in advanced.
Here is my code. ** Please Note. Using "babies or baby" in exchange for "user or users"**
Discover User Text
<% if current_baby = Baby.find_by_email("b10-h4ck3r#protonmail.com") %>
<%= render 'babies/paypal_buttons/bio_h4ck3r_text' %>
<% end %>
<% if current_baby = Baby.find_by_email("b10-h4ck3r#icloud.com") %>
<%= render 'babies/paypal_buttons/b10_h4ck3r1_text' %>
<% end %>
Show User;
<td>
<%= render 'babies/paypal_functions/discover_user_date' %>
</td>
<td>
<%= render 'babies/paypal_functions/discover_user_text' %>
</td>
As mentioned, when I try to view it live, it shows both users PayPal buttons.

Related

Current user information is displaying on all profiles 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

rails if current id do

I'm developing an online course site using Ruby on Rails and bootstrap. I have a model for Courses and a model for Lessons. In the lesson controller show view I am not only showing the lesson page with video, discussion and notes but also a list of all the lessons belonging to the course. This is done by:
<%= #lessons.each do |lesson| %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
The user can, from the individual lesson page, pick the next lesson to watch.
Question:
The URL generated is: localhost:3000/courses/1/lessons/2 (I'll change this with Friendly Id gem later). I would like to show in the list of all lessons on the individual lesson page which lesson the user is watching right now. So basically maybe have something say "You are currently watching : Lesson with id 2" and have it in a different background color with some custom html. How can I have different HTML and CSS for the currently watching lesson in the all lessons list?
Thanks a lot in advance!
/Jacob
A simple way would be to add a conditional, in the case you have the current lesson available in a local variable or method current_lesson. Then it can look like this:
<%= #lessons.each do |lesson| %>
<% if lesson.id == current_lesson.id %>
... other html ...
<% else %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
<% end %>

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.

Dynamically adding pages to rails app

This may be a more fundamental aspect of Rails but I am very new.
I have a basic app which is sort of a basic craigslist clone.
Users - I used devise for this, users can sign up/sign in/sign out/edit their profile.
Listings - Users can add multiple listings. Currently I have a few basic fields populating the database (title, content, phonenumber, price, location).
What I want to be able to do is the following:
I want to have a page which lists all of the ads. Currently I can do this by accessing the database and displaying all the contents. Like this:
<h2>LISTINGS</h2>
<% #users.each do |u| %>
<% u.listings.each do |i|%>
<%=u.email %>
<%=i.title %>
<%=i.content %>
<%=i.number %>
<%=i.price %>
<%=i.location %>
<% end %>
<% end %>
What I want to do is have this page only list the titles, each title would link to the appropriate ad. Users could then access their ad with a URL. Ideally the URL would be the title similar to what stackoverflow does "IE in the URL the title is included with "-" instead of spaces" but that is a minor concern at this point.
How is the best way to go about doing this? I may be using incorrect terminology here as I am having trouble finding information.
I think that you should use as below code -
Eg :
<% #users.each do |u| %>
<%= link_to u.name, :controller => 'user', :action => 'show', :id => u.id %>
<% end %>
I hope that you can solve your problem by using this way.

Resources