does anyone know how to pull a list of facebook friends uid using fb_graph? My current code is below but it does not work. I'm using devise and omniauth.
In my user controller
def profile
profile ||= FbGraph::User.me(self.authentications.find_by_provider('facebook').token).fetch
end
In my view
<% current_user.profile.friends.each do |friend| %>
<%= friend.uid %>
<% end %>
I've been looking for solution for days but still in vain and will appreciate if anyone could tell me the clue.
Use friend.identifier instead of friend.uid.
Related
I have a little problem with my first Ruby on Rails app.
I have 3 tables (Software, User and Library), a user can follow a software and see its follows in his library. But i want to see only no follows in his library.
I have no problem to see the follow but when I want to see only the softwares not followed by USER, i can not do it ...
Software.left_outer_joins(:libraries).where(libraries: {software_id: nil})
This code shows me all software that does not follow, but I need this information BY USER.
And this show follow softwares
#library_softwares = current_user.library_additions
With index.html
<% if #library_softwares.exists? %>
...
<%end>
Do you have an idea ?
Sorry for my english
Thx
EDIT :
I would like to reach this result:
#library_softwares = current_user.library_additions
#software = Software.all
#result = #software - #library_softwares
I hope I have understood what you want, saves the software information in a variable
#softwares = Software.left_outer_joins(:libraries).where(libraries: {software_id: nil})
and if you have a relationship between the user and the softawre you can get user information simply by going through the array of #softwares
<% unless #softwares.nil? %>
<% #softwares.each do |software| %>
<% software.user %>
<%end %>
<%end%>
So I currently have this code in the html.erb:
<%= f.label :user_id %>
<%= f.field :user_id %>
I want the user to enter an email into the field in which it converts it into user_id using this:
User.find_by_email("xyz#abc.com").id
However, I have no idea how to use it or know whether this will even work. Will appreciate some help on this as I am quite new to rails. Thanks!
Are you trying to get the value of the user's email after they press the submit button?
Update
Try this:
User.find_by(email: params[:user_id])
This will work after the user presses the submit button, but you have to assign it to a variable if you want to use it somewhere else.
I have a model called Note. Each note belongs_to :call_reason. And call_reason has_many :notes.
What I want to do in a view is display a list of call_reasons and a total count of each next to it so we can see what the most popular call reasons are.
Here's what I have so far:
dashboard_controller:
def index
#notes = Note.all
end
dashboard view:
<% #notes.each do |n| %>
<%= n.call_reason.reason %>
<% end %>
This lists all notes' call_reasons.
I'm stumbling on how to list each call_reason once with a total count next to it. What I have now just lists all the call_reasons per note which is a mess. I think I could scope this out somehow or change the instance variable but I'm having a hard time getting it right.
Any thoughts?
Since you want to list call reasons, you should start with that:
def index
#call_reasons = CallReason.all
end
Then in your view you can do this:
<% #call_reasons.each do |call_reason| %>
<%= call_reason.reason %> <%= call_reason.notes.count %>
<% end %>
Note that this will perform a query for every call reason in your database. To prevent this you can use a counter cache. Check out the section on counter cache on Rails Guides too.
In the documentation I found this part:
<% permitted_to? :create, :employees do %>
<%= link_to 'New', new_employee_path %>
<% end %>
This is good, but I need to test, if the current user is admin, and if does, so then display some text... Something like
if admin?
I can get this information from associations, like:
if current_user.role == 0
but this is a bit dirty solution... support Declarative Authorization some cleaner and nicer way to do it?
This is what I have been looking for:
if has_role?(:admin)
...
end
I am using devise and cancan with rails 3.2.1 and I'm having an unusual problem that I know can be refactored and simplified, but after much searching, I'm stuck.
I have first_name and last_name fields in the user model and have successfully displayed the full name of the user in the users/show.html.erb view using this code:
<%= #user.first_name + " " + #user.last_name %>
And I was successfully displaying a link to the user's show page in the header using:
<% if user_signed_in? %>
<li><%= link_to #user.first_name + " " + #user.last_name, current_user %></li>
<% end %>
But, and I can't figure this one out, after working on another part of the app, the header started giving me an undefined method error message...
In any case, I know there is a much better way to define and work with user names and I have tried some things out, such as adding a name method to the user model, but as you can clearly tell from this post, I am a beginner and have not been able to make that work.
Please let me know if you have any ideas on how to best work with users names in an application like this.
Where are you defining #user? I'm willing to bet that it's in your UsersController show action, which means it's defined for your header in that action only.
Try replacing your header code with this:
<% if user_signed_in? %>
<li><%= link_to current_user.first_name + " " + current_user.last_name, current_user %></li>
<% end %>
The trick is that Devise makes current_user always available when the user is signed in. You don't have to worry about setting a #user instance variable.