How do display activity owner name in Angular? - ruby-on-rails

I'm trying to build a activity stream in my rails/angular application using the Public Activity gem and the Railscast. It's a pretty easy set up on the rails side, but I'm having some trouble with the angular side of things.
I've tracked my movie model. So when a movie is added it's recorded as a new activity. I've created a template, service and controller (on the angular side) to display the activity.
The template,
%ul{"ng-repeat" => "activitie in activities"}
%li {{ activitie }}
When I view the template in my app it displays like this (which is expected).
{"id":2,"trackable_id":5,"trackable_type":"Movie","owner_id":1,"owner_type":"User","key":"movie.create","parameters":{},"recipient_id":null,"recipient_type":null,"created_at":"2015-12-30T11:55:06.766Z","updated_at":"2015-12-30T11:55:06.766Z"}
Ofcourse this is not how I want to display the data, but here it gets tricky. In the railscast Ryan adds this code to his template,
<% #activities.each do |activity| %>
<div class="activity">
<%= link_to activity.owner.name, activity.owner if activity.owner %>
<%= render_activity activity %>
</div>
<% end %>
So he uses activity.owner.name to present the name. But I have no idea how to get this result in Angular. The owner_id is the id of the user that added the movie, but I don't know how to display it through Angular.

You can join with the users table to incorporate the users name.
PublicActivity::Activity.
joins("INNER JOIN users ON users.id = activities.owner_id").
select("activities.*, users.name")
Now you should be able to call activity.name.

Related

Display Elements Based On Logged In User (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.

Ruby cannot access a property in database for image tag, though all other tags are working

I'm currently using Ruby to create a web e-commerce app, but suddenly am having an issue with retrieving a property.
I decided to update the image slider of my website with two images instead of one. In order to do so, I added a new property called 'img2_url' to my database of products (also entitled Products), for which there are several existing properties (name, title, year, image_url, etc). I updated the products.controller product_params to include the new property (img2_url), and migrated the change to the database. I uploaded img2_url images for each item in the database under assets/images, and amended the html.erb code for the slider to tag the asset for the second image, right next to the first image, like so:
<% #products.each do |product| %>
<div>
<%= image_tag(product.image_url, :class =>"product_image_thumbnail") %>
<%= image_tag(product.img2_url) %>
</div>
<%= product.name %>
Where product.image_url tags the property for the first image, product.img2_url tags the property for the second image, the new one which I've added, and product.name tags the property for the name of the item.
Unfortunately, what I get back is that the assets pipeline successfully retrieves product.image_url, product.name, but not the new property that I just added - product.img2_url. What I get returned from my localhost test site is the following:
nil is not a valid asset source
app/views/static_pages/_product.html.erb:3:in `_app_views_static_pages__product_html_erb___2209812928582123295_70176261759380'
app/views/static_pages/landing_page.html.erb:13:in `block in _app_views_static_pages_landing_page_html_erb___3644243706632459022_70176262254540'
app/views/static_pages/landing_page.html.erb:12:in `_app_views_static_pages_landing_page_html_erb___3644243706632459022_70176262254540'
I've used ActiveRecord to confirm that I've uploaded the correct corresponding URLs into the database files, that I've uploaded the photos into the assets/images folder, and I've tried inserting the code into different pages. Mind-bogglingly, it works on other pages. I'm beginning to think that maybe the <% #products.each do |product| %> tag is the problem, because those pages don't contain that tag. For example, here is code where it works:
<h2 class="page-header-form" id="notice"><%= notice %></h2>
<p>
<strong>Image 2 URL:</strong><br>
<%= #product.img2_url %><br><br>
<%= image_tag(#product.img2_url, :class =>"product_image_show") %>
</p>
But then again, <% #products.each do |product| %> successfully retrieves the data for all other properties, and DOES work, so there must be some way to fix the problem.
Any ideas on what I'm doing wrong?
Thank you, any help would be SO much appreciated, I've been tearing my hair out.

Rails 4 Generating application wide dynamic nav-bar links

I am trying to generate links in my nav-bar based on records.
I want to post a link in the patrols section of my app that correspond to a patrol route that an admin generated?
I have tried to place
#patrol_routes = PatrolRoute.all in the application controller
and then i want something like
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path %> so that it takes me to the show page of the patrol route i want to access?
<% end %>
Is this possible? i have tried to google and research it, but I'm not finding anything, perhaps I'm not hitting the correct key words?
Assuming you have a route (as in config/routes.rb) named patrol_route you should do:
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path(patrol_route) %>
<% end %>

Updating all with a dictionary in Rails

I have a small CMS-like program that has multiple pages that act like blog posts. Each page has content, and a position integer that identifies in what order they will appear on the page.
On my admin side, I have a draggable list of pages that I can reorder similar to how wordpress orders plugins. The page works as functions, and assigns the value of the dragged position to each page correctly. However, since all sortable pages have their own form, I cannot submit them all at once - only one at a time.
As an example, my code looks like this currently:
<div class="sortable">
<% #pages.each do |page| %>
<div class="dragBox">
<%= form_for(page) do |f| %>
<%= f.number_field :position, class: 'inPosition' %>
<% end %>
</div>
<% end %>
</div>
Because I can get every page_id tied to its new position, is it possible to submit those values in a new hash to get updated all at once in the controller? How would I go about doing this? Is there a better or easier way to do this? Thanks.

Question about checkbox on Ruby on Rails

I'm developing a website for my University and I got a problem.
There is a page where I have the list of all students of the university. The admin can select some students that will be able to go to a selective process and then he have to see them in other separate page.
How can I do that using Ruby On Rails?
Thanks,
Hugo Henley
Hi If you want use checkboxes you should write inside your form something similar to <td><%= check_box_tag "user_ids[]", "#{user.id}", true%><%= user.name%></td> then you'll get array od user ids as an params[:user_ids] and you may show only this users on other page
<h1>in your view </h1>
By checking those ones using checkboxes you can get the id's of those students like this
<%= check_box_tag "user_ids[]", "#{user.id}", true%>
passing those id's into the respective controller action
#users = User.where(:id => params[:user_ids])
Display those object details in to the required webpage using #users.each
<% #users.each do |user| %>
<%= user.name %>
<%end%>

Resources