Creating a mutual friends list - ruby-on-rails-4

I've been through the Treebook tutorial on Treehouse and I've been trying to enhance it.
I want to add a list of mutual friends on the profile_page that shows the friends you have in common with that user.
I can write an if statement to show the profile_name of the common users.
E.g.
<% if current_user.friends.include?(friend) and #user.friends.include?(friend) %>
<%= friend.profile_name %>
<% end %>
But I'd like to write a block to go through and show other information. How would I create the query in the profiles controller so that I can use it in the block?

So you want something like:
def common_friends
Friends.where(friends: [friend1, friend2])
end
This way you are querying for friends where friend list includes both friend1 and friend2.

Received some help elsewhere. I'm guilty of overthinking the problem.
#common_friends = current_user.friends & #user.friends
Then used:
<% #common_friends.each do |friend|%>
<%= friend.profile_name %>
<% end %>

Related

Rails 4, Draper: authenticated user and views

<% if user_signed_in? %>
<!-- lots of html/erb -->
<% end %>
This view pattern seems to not separate concerns.
I wrap several views in my app with logic demanding the user is signed in and would instead like to separate concerns and put the <% if user_signed_in? %> logic where it belongs...this seems like a decorator thing to me (hence the Draper tag).
What is best practice here?
Not sure understood your question, but try to answer.
At first to separate logic you dont need to use decorators in front of all, they serves for a little another thing.
To separate logic you can use simple partials depending on current user state, for ex:
<% if user_signed_in? %>
<%= render 'file_with_html_for_signed_user' %>
<% else %>
<%= render 'file_with_html_for_non_signed_user' %>
<% end %>
You can declare this statement in your layouts/application.html.erb

Rails 4: (acts as taggable) creating a tag cloud based only on a specific users posts

I am attempting to create a tag cloud based on a users microposts (obvious the counts on each tag for that user) using this in my controller
#tags = #user.microposts.tag_counts_on(:tags)
and this in my view
<% tag_cloud #tags, %w[xxs xs s m l xl xxl] do |tag, css_class| %>
<%= link_to tag, questions_by_tag_user_path(#user, tag: tag.name), class: css_class %>
<% end %>
The problem is, having checked this over and over, this is returning the tag cloud with the sizes based on tag counts based on all microposts and not just the ones by the #user.
I can't see why this is and the documentation is deprecated:
User.find(:first).posts.tag_counts_on(:tags)
Any thoughts?
This would work, if you would call the tag_counts_on on the Posts scope:
Post.where(user_id: 1).tag_counts_on(:tag)

RoR: How to set the value in a collection_select from the database in the edit view (1:many relation)

I am at the moment creating a complicated rails form for 1:n relationship with nested form and collection select with values from yet another data table.
So far, it overwrites the database value with the first entry in the values list of the collection_select whenever the user does not select the correct value before update. I still need to set the initial value in the collection_select correctly.
I have read a lot of questions on SO already, most relevant was:
f-collection-select-not-displaying-the-selected-value
The only thing still missing (I hope!), is the setting of the value of the form field from the database, so it does not get overwritten with a default value from the selects selectable values, even though the user has not touched the select.
This snippet is derived from my code and the solution to the abovementioned question and is wrong.
Let a person have many things and validthings contains the possible values for things:
In the things table there will only be Thing strings, that are also in the validthings table. It is possible to give the collection_select selected param a string from the things table that can be identified in the list of values from the validthings table.
<div class="col-md-12">
<%= form_for(#person) do |f| %>
<%= f.fields_for :things do |d| %>
<%= d.hidden_field :id %><%= d.hidden_field :person_id %>
<%= d.collection_select(:Thing, Validthings.all, :Thing, :Thing, {:selected => #person.things.map(&:id).Thing.to_s} ) %>
<% end %>
<% end %>
</div>
This is what is wrong:
#person.things.map(&:id).Thing.to_s
And yes, in tables persons and things and validthings the column is named "Thing". It is a unique string in table validthings - the database structure was not my idea, I only work with it.
Found a helpful answer here: rails-accessing-current-value-of-a-symbol
to another subject, but my problem was that I did not know how to access the information that I knew must already be loaded.
This is how I can specify the default value of a collection_select to be the data from the database:
<div class="col-md-12">
<%= form_for(#person) do |f| %>
<%= f.fields_for :things do |d| %>
<%= d.hidden_field :id %><%= d.hidden_field :person_id %>
<%= d.collection_select(:Thing, Validthings.all, :Thing, :Thing, {:selected => d.object.Thing} ) %>
<% end %>
<% end %>
</div>
where d.object.Thing is the value of the respective object of the form element for the attribute "Thing", which is already present in the form.
I'd be very grateful for constructive ideas, in case my approach is un-ruby-like or some such. I am rather new to ruby, rails etc.

Rails object method returning full array

I'm having an issue with an association. I've got an Employee model that belongs_to a Role model. When I try to display the association, I get the full array displayed back.
Here's the show action from my Employee controller. As you can see, I've tried a few different methods to make the proper association in the first place:
def show
#employee = Employee.find(params[:id])
# #role = Role.where(:id => #employee)
# #role = Role.find_by_sql("select roles.role_title from roles where roles.id in (select role_id from employees where role_id='1')")
#role = Role.where(id: #employee)
end
And here's the view:
<p>
<strong>Role:</strong>
<%= #role.each do |r|
r.role_title
end %>
</p>
My output comes back as:
Role: [#<Role id: 3, role_title: "Support Engineer", created_at: "2014-08-20 16:09:22", updated_at: "2014-08-20 16:09:22">]
What am I missing here?
You need to actually iterate and display something for each role.
<%= %> means "display the result of the expression", which in your case, is an each.
each returns the collection you were iterating over. You want something closer to:
<% #role.each do |r| %>
<%= r.role_title %><br/>
<% end %>
Although it obviously depends on what you actually want to display, for example:
<%= #role.collect(&:role_title).join(', ') %>
Unrelated: I might argue that Role#role_title is redundant and Role#title would be sufficient.
If the employee belongs_to a role there is only one role for each employee.
You can retrieve it as easily as specifying...
#employee.role
but if you insist on constructing a separate retrieval then
#role = Role.where(id: #employee.role_id).first
EDIT
So talking about the views... if there's only one #role you don't need to iterate through an array...
<p>
<strong>Role:</strong> <%= #role.role_title %>
</p>
You're seeing an array because the where returns an array, you could bypass that with...
#role = Role.where(id: #employee).first
As Dave Newton pointed out, if it really was an array you'd need to do...
<p>
<strong>Role:</strong>
<% #role.each do |r| %>
<%= r.role_title %>
<% end %>
</p>

Adding a Class to a link_to Helper With Only 1 Parameter

I have the following:
<% #users.each do |user| %>
<% if user.profile %>
<%= link_to user do %>
<h2><%= user.profile.first_name %> <%= user.profile.last_name %></h2>
<% end %>
<% end %>
<% end %>
The above code works fine. What this code does is that it will output the first and last names of every user. These names are clickable and will take me to that user's page. My main issue is with the 3rd line. The issue I am having is that I am trying to get rid of the link underline, but I am unsure as to how to pass a class into it. Below is my attempt. My class "no-text-dec" is just one line of "text-decoration: none;"
<%= link_to (user, class: "no-text-dec") do %>
I'm new to Rails, but I understand that link_to has a body, url options, and then html options in that specific order, but how can I make it work in this case? The above line makes my application is crash, but it's the only thing I can think of that makes sense. I'm assuming it's because I am not giving it its body argument, but I'm not sure what that would be.
This should work fine if user contains url/path correct
<%= link_to(user, class: 'some_class') %> do
<span>Delete</span>
<% end %>
The space after method in sending argument in helper method link_to is crashing your application
you can give a try at irb
def test(a,b)
puts a; puts b;
end
test ("Ad","Cd")
It should throw an error