Why is my object printing to the page in rails? - ruby-on-rails

When I run my rails server in chrome, at the end of the unordered list, the object hash is printing to the page. I am not sure why this is happening. Here is my show.html.erb -
<h1><%= current_user.email %>'s Cars</h1>
<ul>
<%= current_user.cars.each do |car| %>
<li><%= car.color %></li>
<li><%= car.make %></li>
<li><%= car.model %></li>
<li><%= car.year %></li>
<button class="btn btn-default"><%= link_to 'Edit', edit_car_path(car) %>
</button>
<button class="btn btn-default"><%= link_to 'Delete', car, :data => {:confirm
=> "You Sure?", :method => :delete} %></button>
<%end%>
</ul>
<% if user_signed_in? %>
<button class="btn btn-default"><%=link_to "Add a car",
new_user_car_path(current_user)%></button>
<%end%>

you are itreating that hash and printing it at the same time
remove the # from this line<%= current_user.cars.each do |car| %>
to following <% current_user.cars.each do |car| %>

Related

How to build a search form in rails for soundcloud

Hi i'm new to rails and trying to figure out how to build a search for to get info back from Soundcloud.
If i build everything static then i get back the info but i cannot get it to work with a search form
Method in Controller:
def search
#client = Soundcloud.new(:client_id => ENV["SOUNDCLOUD_CLIENT_ID"])
end
THIS WORKS
#search.html.erb
<div class="form">
<% #client.get('/tracks', :q => 'djsneak' ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>
What i would like to use:
<!--search.html.erb-->
<div class="form">
<%= form_tag("soundcloud_search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
</div>
<div>
<h3>results</h3>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
</div>
i solved it myself, but for who is interested:
<!--search.html.erb-->
<!--search form-->
<div class="form">
<%= form_tag("searchit", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q, params[:q]) %>
<%= submit_tag("Search", :name => nil) %>
<% end %>
</div>
<!--Results-->
<div class="form">
Search Results
<% #client.get('/tracks', :q => params[:q] ).each do |track| %>
<ul>
<li><%= track.title %></li>
<li><%= image_tag track.artwork_url %></li>
</ul>
<% end %>
</div>

How to Render Current User into Nav-Bar Button

Running:
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
Rails 4.2.5
I want to render the current user name into the nav-bar button using the dropdown-menu class. I would like to replace "Account Info" with "Hi "current_user".
<li class="dropdown">
Account Info <span class="caret"></span>
<ul class="dropdown-menu">
<li> <%= link_to "Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li>
<li> <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li>
</ul>
<ul class = "nav pull-right">
</ul>
<% else %>
<!-- <%= link_to "Sign up", new_user_registration_path, class: "fa fa-sign-in" %> -->
<li><%= link_to "Sign in", new_user_session_path, class: "fa fa-sign-in" %></li>
<% end %>
</ul>
I'm using devise (3.5.3) for user authentication.
You'll just need to use an if statement to work out if the user is signed_in (I presume this method exists in Devise) and show a different button if the user is signed in:
<li class="dropdown">
<% if signed_in? %>
Hi, <%= current_user.user_name %> <span class="caret"></span>
<% else %>
Account Info <span class="caret"></span>
<% end %>
<ul class="dropdown-menu">
<li> <%= link_to "Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li>
<li> <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li>
</ul>
<ul class = "nav pull-right">
</ul>
<% else %>
<!-- <%= link_to "Sign up", new_user_registration_path, class: "fa fa-sign-in" %> -->
<li><%= link_to "Sign in", new_user_session_path, class: "fa fa-sign-in" %></li>
<% end %>
</ul>
You'll probably want to use a similar if, else, end statement to hide the sign_in/sign_up buttons when the current_user is signed_in too - as they won't want to see that.
This would suffice in your design simply.
<%- if user_signed_in? %>
<%= current_user.user_name %>
<% end %>
You have to first ask if the user exists. The reason why is because if you run .user_name on a user who is not logged in it is essentially running a method on a nil value. Which is a big no no.
Now if you still have an issue you have to make sure you have this in your controller:
before_filter :authenticate_user!
<%= content_tag :li, class: "dropdown" %>
<% if user_signed_in? %>
<%= link_to "Hi, #{current_user}", "#", data: {toggle: "dropdown"}, role: :button, aria: {haspopup: "true", expanded: "false"} %>
<%= content_tag :ul, class: "dropdown-menu" do %>
<%= content_tag :li, link_to("Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o") %>
<%= content_tag :li, link_to("Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out") %>
<% end %>
<% else %>
<%= content_tag :li, link_to("Sign in", new_user_session_path, class: "fa fa-sign-in") %>
<% end %>
<% end %>
Refs:
content_tag
With some testing <%= current_user.first_name %> in Hi, <%= current_user.first_name %> <span class="caret"></span> seem to give me the outcome I was looking for. Thanks for your help and pointing me in the right direction. #Alain-Goldman.

Changing nav-bar links according to whether a user has created a profile or not (RoR)

I'm making a rails app and have created a simple navbar that works fine. I have set up devise and created a profile model which I would like users to create after they sign up, and (I believe) I have correctly made the necessary associations.
As a new user signs in for the first time, I would like a "Create profile" link to show in the nav-bar using the new_profile_path, but once that specific user has created his/her profile, i would like the "create Profile" to change to "View Profile" and remain that way permanently, which should redirect to the profile_path.
The code I currently have in my nav-bar is the following:
<ul>
<li><%= link_to root_path do %>
<i class="fa fa-home"></i>Main
<% end %>
</li>
<% if current_user.profile %>
<li><%= link_to profile_path(current_user.profile) do %>
<i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
<% end %>
<% end %>
<% else %>
<li><%= link_to "New Profile", new_profile_path %></li>
<ul>
<li><%= link_to "Tasks / Activities", tasks_path %></li>
<li><%= link_to "Vacations", vacations_path %></li>
</ul>
</li>
<li><%= link_to projects_path do %>
<i class="fa fa-building-o"></i>Projects<i class="fa fa-sort-desc"></i>
<% end %>
<ul>
However even after creating the new profile the link "Create Profile" still remains. How can I fix this?
Try this:
<% if !current_user.profile.blank? %>
<%= link_to profile_path(current_user.profile) do %>
<i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
<% end %>
<% else %>
<%= link_to "New Profile", new_profile_path %>
<% end %>
Your error is here <% end %><% else %>. You close the if test before your else. Result: <%= link_to "New Profile", new_profile_path %> is always displayed!
Remove the 10th line and it'll work fine.

Where should I set the current_user so I don't get a missing key error?

My question assumes a specific answer but it may not but accurate.
I've created a route:
get 'users/:id/groups/' => 'users#groups', as: "my_groups"
but when I go to access this path, conditionally, in a view, i get an exception:
ActionController::UrlGenerationError at /
No route matches {:controller=>"users", :action=>"groups"} missing required keys: [:id]
Here is the partial with the offending line:
<% if user_signed_in? %>
<li id="groups-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Groups <b class="caret"></b>
</a>
<ul class="dropdown-menu">
#offending line:
<li><%= link_to 'My Groups', my_groups_path %></li>
<li><%= link_to 'Create a Group', new_group_path %></li>
</ul>
</li>
<li><%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
<li><%= link_to 'Login', new_user_session_path %></li>
<% end %>
<% if user_signed_in? %>
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
<% end %>
<% if user_signed_in? %>
<% if current_user.has_role? :admin %>
<li><%= link_to 'Admin', users_path %></li>
<% end %>
<% end %>
it seems that there is no current_user set, perhaps?
It's unclear how this could break if the user_signed_in? method is working.
You need to pass in the :id parameter to the url helper function
<li><%= link_to 'My Groups', my_groups_path(current_user) %></li>

Render different partials with ajax - rails 3

I am trying to get a link_to to display different partial via jQuery ajax, for every nav tab link.
But can't seem to get it to work, it redirects every link to the root index.
I would like to click the links in my nav menu and display the partials: _grundskola.html.erb , _gymnasium.html.erb, _universitet.html.erb for that link in the profile-data div.
Example:
I understand that for example nav link "universitet" should call the action def universitet which calls universitet.js.erb which renders the partial _universitet.html.erb in the div.
The nav-menu - in the show.html.erb
<ul class="nav nav-tabs">
<li><%= link_to "Grundskola", :action => 'grundskola', :remote => true %></li>
<li class="active"><%= link_to "Gymnasium", :action => 'gymnasium', :remote => true %></li>
<li><%= link_to "Universitet & Högskola", :action => 'universitet', :remote => true %></li>
</ul>
user_controller.rb
def universitet
respond_to do |format|
format.js
end
end
universitet.js.rb
$("profile-data").html("<%= escape_javascript(render(:partial => 'universitet')) %>");
_universitet.html.erb
<% groups = #user.friends.group_by(&:college_name) %>
<% sorted_groups = groups.sort_by{|key, values| values.count}.reverse %>
<% sorted_groups.each do |collegename, friends| %>
<% next if collegename.blank? %>
<div class="contentbox">
<div class="box-header">
<h3><%= collegename %></h3>
<div class="meta-info">
<p><i class="icon-map-marker"></i> Malmö</p>
<p><i class="icon-user"></i><span class="count"> <%= friends.count %></span> vänner</p>
</div>
</div>
<ul class="friends-list">
<% friends.map do |friend| %>
<li><%= image_tag(friend.image) %>
<% end %>
</ul>
</div>
<% end %>
I get the wrong link structure to, the html output:
<li>Universitet & Högskola</li>
Any help would be appreciated.
Try adding format to the link
example
<li><%= link_to "Grundskola", {:action => 'grundskola',:format=>:js}, :remote => true %></li>

Resources