Link_to with anchor doesn't go to the specific id - ruby-on-rails

In the footer of my website, I have a menu, where I create links to other pages. A part of that menu looks like this:
<ul class="footer-menu-content">
<li><%= link_to 'All games', games_path %></li>
<% Category.all.each do |category| %>
<li><%= link_to category.name, games_path(anchor: category.name.downcase.parameterize) %></li>
<% end %>
</ul>
And on the page, where games_path leads, the code is as follows:
<% #categories.each_with_index do |category, index| %>
<div id="<%= category.name.downcase.parameterize %>" class="row game-category">
<div class="col-lg-12 col-md-12 col-sm-12">
<p class="all-games-category-title"><span style="background: url(<%= category.cat_image %>) no-repeat">
</span><%= category.name %>
</p>
</div>
</div>
<% end %>
The link_to generates HTML, for example: Blackjack, and when I click the link, it redirects me to http://localhost:3000/games#blackjack, but it doesn't go to the specific element with id blackjack. If I copy the url and paste it into the browser, everything works fine. So, what can be wrong, and what can be done, to make the click work? Thanks.

Related

<% if !current_page?(action: 'edit') %> error . No route matches {:action=>"edit", :controller=>"…"}

My _footer.html.erb is (simplified):
<footer class="footer">
<nav class="navbar navbar-default navbar-fixed-bottom navbar-inverse">
<% if logged_in? %>
<li class="btn btn-primary"> <%= link_to 'Add New Connection', new_year_path %></li>
<li class="btn btn-primary"> <%= link_to 'Add New Person', new_person_path %> </li>
<li class="btn btn-primary"> <%= link_to 'Add New Address/Location', new_location_path %> </li>
<% end %>
</nav>
</footer>
But I don't want the footer to show up while editing; too easy to click on one of these buttons instead of Cancel or Submit which is down the page and my footer is stuck to the bottom. views/layouts/application.html.erb has <%= render 'layouts/footer' %>
I suppose I could have my three edit.html.ergs not use the defaults above, but seems easier if I could add another condition on the _footer.html.erg. Or some other way. Maybe something about leaving page without submitting?
Based on #tsrandrei comment I tried wrapping the footer in <% if !current_page?(action: 'edit') %> blah-blah <% end %> but then get error No route matches {:action=>"edit", :controller=>"…"} with the controller name being whatever the page is relevant to.
<% if current_page?(action: 'edit') %>
<footer class="footer">
<nav class="navbar navbar-default navbar-fixed-bottom navbar-inverse">
<% if logged_in? %>
<li class="btn btn-primary"> <%= link_to 'Add New Connection', new_year_path %></li>
<li class="btn btn-primary"> <%= link_to 'Add New Person', new_person_path %> </li>
<li class="btn btn-primary"> <%= link_to 'Add New Address/Location', new_location_path %> </li>
<% end %>
</nav>
</footer>
<% end %>
See https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F
You will also have other methods for your views:
controller_path
controller_name
action_name
Also check Rails: How to determine controller/action in view

How do you use pundit for partial authorization

Consider the following block of ruby on rails code:
<% unless current_user %>
<%= card title: 'Things you can do', bodyless: true do %>
<ul class="list-group list-group-flush">
<li class="list-group-item" style="background: transparent"><i class="fa fa-key"></i> MEET OTHERS LIKE YOU</li>
<li class="list-group-item" style="background: transparent"><i class="fa fa-question-circle-o"></i> ASK A QUESTION</li>
<li class="list-group-item" style="background: transparent"><i class="fa fa-users"></i> SHARE YOUR JOURNEY</li>
</u>
<% end %>
<% end %>
The "card" is shown, but only to logged in users.
Can Pundit Authorization policies be used determine which divs/partials/cards are shown to the visitor on a website?
Yes you can, if you defined a policy and then want to show some div/partial just if that policy, then you can do it like this for example
<% if policy(#post).update? %>
<%= link_to "Edit post", edit_post_path(#post) %>
<% end %>

Best way to show empty lists in Rails

I have been working to follow the best practices in Rails in order to save time in the future.
I am displaying all terms in a view which has more than 0 results.
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end %>
What is the best way to display a different view if the term count is zero? I could do something like a simple if statement in the view doing a count but that seems cluttered. Any suggestions?
How about this:
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end.empty? and begin %>
<li class="media">
<!-- nothing to see -->
</li>
<% end %>
You can also move the code to a partial.
<%= render(:partial => 'terms', :collection => #terms) || 'There are no terms' %>
The partial:
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{term.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, term) %></span>
</div>
</li>
</li>

link_to tag not including all divs

I currently have a link to tag which should wrap around all the content within it, but currently it's not doing that. It's wrapping around the code until it hits another div with a rails query inside it?
index.html.erb
<% #posts.each do |post| %>
<div class="widget" >
<%= link_to post do %>
<div class="image b-lazy" data-src="<%= post.image %>">
</div>
<div class="caption">
<h4><%= post.title %></h4>
<p>by <%= post.affiliate %></p>
</div>
<!-- LINK TO TAG ENDS HERE FOR SOME REASON -->
<div class="caption-top">
<% post.categories.each do |category| %>
<%= link_to category_path(category) do %>
<div class="tag <%= category.name %>"><%= category.name %></div>
<% end %>
<% end %>
</div>
<% end %>
</div>
Any help is appreciated!
Jonathan
Two things:
You are using link_to inside another call to link_to. That is probably not what you want.
The result of a block will be what you return from a block, normally the last line. Take a look at this question for a solution.

Update SVG using gagejs and rails

I'm experimenting with Raphael, SVG, and rails. I want the gage to dynamically update as a progress indicator of sorts as each task is crossed off via the completed button.
Something like tasks completed/total task created.
Attached is the view in the browser and below is the show page.
<h1 class="page-header"> Getting it up</h1>
<div class="well span9 offset2">
<h2 class="page-header"><%= #list.name %></h2>
<h2 class="lead"><%= #list.description %></h2>
<div class="pull-right" id="smallbuddy" style="width:340px; height:200px;"></div>
<h3>Still Not Finished</h3>
<ul>
<% #list.tasks.incomplete.each do |task| %>
<li style="list-style:none"><%= task.description %> <%= button_to "Completed", complete_task_path(#list.id,task.id), :class =>"btn-mini" %></li>
<% end %>
</ul>
<hr>
<h3>Finished</h3>
<ul id="completed">
<% #list.tasks.completed.each do |task| %>
<li style="list-style:none; opacity:.5; text-decoration:line-through;"><em><%= task.description %></em></li>
<% end %>
</ul>
<hr>
<%= form_for [#list, #list.tasks.new] do |form| %>
<p><%= form.text_field :description %><%= form.submit %></p>
<% end %>
</div>
<span class="btn affix"><%= link_to "Back to all Installs", lists_url %></span>
<div>
</div>
<script>
var g = new JustGage({
id: "smallbuddy",
value: 27,
min: 0,
max: 100,
title: "Until Complete"
});
</script>
Do I need to manipulate the Raphael Lib? or the justgagejs lib?
Still working my way through it but some veteran advice to point me in the correct direction on how to achieve this would be helpful. Thanks in advance.
You need your rails controller to respond to the a JS request, read a little into this: Having trouble rendering Javascript in Rails question.
That way you can update your gauge in the JS returned by your controller.

Resources