I am listing previously used shipping addresses for user to select. I`m dynamically adding classes
<div class="row">
<% #shipping_addresses.each do |address| %>
<ul class=<%= "shipping_address_#{address.id}" %> >
<li><%= address['name'] %> </li>
<li><%= address['street'] %> </li>
<li><%= address['city'] %></li>
<li><%= address['country'] %></li>
<li><%= address['zip'] %></li>
<li><%= address['phone'] %> </li>
</ul>
<% end %>
</div> <!-- row -->
Problem is, that now I am trying to add a col-lg-3 bootstrap class to my ul`s, and it doesn`t work when I write it like this:
<ul class=<%= "shipping_address_#{address.id} col-lg-3" %> >
I get this output:
<ul class="shipping_address_38" col-lg-3="">
I also tried several different options and they don`t work.
Can anyone help?
Thank you
The actual html you are outputting is
<ul class=shipping_address_38 col-lg-3>
And your browser is interpreting this as best it can. The quotation marks in your template never make it to the actual html because they're not actually part of the string.
You could do something like
<ul class="<%= "shipping_address_#{address.id} col-lg-3" %>" >
Although in my opinion you're now past the point where ERB gets difficult to read - you may wish to refactor this into a helper.
I'm running through Michael Hartl's tutorial, and having reached the styling and layout chapter, seem to have got something wrong. Whereas in the tutorial, the site links are right aligned on a single line (like this http://railstutorial.org/images/figures/site_with_footer_bootstrap_4_0-full.png), I get them right aligned one beneath the other, and some (but not all) other styling elements missing.
I've tried updating gems.
Example code - _header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", '#', id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", '#' %></li>
<li><%= link_to "Help", '#' %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</div>
I think this means that bootstrap is unhappy with something, but I can't tell what.
Thanks for reading this far :)
If you ever have questions regarding your code vs what it should be, try checking his repository first.
https://github.com/railstutorial/sample_app_rails_4
There he has a full working copy of the sample application.
Assuming that's all of your HTML for the navbar, it doesn't look like the tags were closed properly. Try:
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", '#', id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", '#' %></li>
<li><%= link_to "Help", '#' %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</div>
</div>
</header>
Digging further into RADan's comment
Weirdly, gem list --local gives - bootstrap-sass (3.1.1.0, 2.3.2.0)
I found that I had the same problem. To solve this (it serves the newer version of bootstrap instead of the older version - which causes CSS class issues) I deleted the older version of bootstrap by entering the following into the terminal:
gem uninstall bootstrap-sass -v 3.1.1.0
I'm using rails with Bootstrap. I have a basic page with navbar along the top. What I am struggling to achieve is how to define the targets for the options in the navbar as partials beneath this navbar.
I've trawled threads on here - finding things that don't quite marry up to the above, and followed the following tutorial: http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-partials ... which didn't work. No matter what I try, I end up with a link that simply directs to a whole new page.
My code as it stands (based on the above quoted tutorial)....
snippet from post_login.html.erb
<ul class="nav navbar-nav">
<li class="active"><%= link_to "P4 Sync", "p4syncpartial" %></li>
<li><%= link_to "P4 Output", "p4outpartial" %></li>
</ul>
<<<SNIP!!!>>>
<div class="container">
<%= yield %>
</div>
</body>
routes.rb entry for one of the above tags:
match '/p4syncpartial', to: 'authentication#_perforce', via: 'get'
And just for completeness, my placeholder authentication/_perforce.erb:
<p>This is a dummy P4 partial</p>
Can anyone point out where I am going wrong? Thank you :)
You're missing leading slash / in link_to url parameter:
<ul class="nav navbar-nav">
<li class="active"><%= link_to "P4 Sync", "/p4syncpartial" %></li>
<li><%= link_to "P4 Output", "/p4outpartial" %></li>
</ul>
I try to use current_page? method in my navbar this way:
<ul class="nav pull-right">
<li class="<%="active" if current_page?(controller: :welcome)%>"><%= link_to "Home", "/"%></li>
<li class="<%="active" if current_page?(controller: :products)%>"><%= link_to "Store", "/products"%></li>
<li class="<%="active" if current_page?(controller: :catalog)%>"><%= link_to "Catalog", "/catalog"%></li>
...
Everything works fine when I move between pages.
But when I try to go to /admin which is provided by rails_admin I get 500 internal server error in response. This is probably caused by current_page? method because if I remove them from layout everything works fine.
I'll appreciate any solution.
In case anyone runs into this and wants to fix it, you need to use absolute urls in the way you pass parameters to current_page?
So in your case, this code will work:
<ul class="nav pull-right">
<li class="<%="active" if current_page?(controller: '/welcome')%>"><%= link_to "Home", "/"%></li>
<li class="<%="active" if current_page?(controller: '/products')%>"><%= link_to "Store", "/products"%></li>
<li class="<%="active" if current_page?(controller: '/catalog'')%>"><%= link_to "Catalog", "/catalog"%></li>
I just disabled using default layout by adding layout false in devise session controller and everything works fine now. Thanks!
I saw a few postings related to this topic, but the given solutions did not really clarify things for me ...
So, I am working on a rails (version 3.2.2) application that followed the setup from Michael Hartl's Ruby on Rails tutorial. The application has a signout link, which worked well until recently, when it started giving me the error 'No route matches [GET] "/signout"'.
These are the relevant pieces:
routes.rb
match '/signout' => 'sessions#destroy', :via => :delete
sessions_controller.rb
def destroy
sign_out
redirect_to root_path
end
sessions_helper.rb
def sign_out
current_user = nil
cookies.delete(:remember_token)
end
_header.html.erb
<li>
<%= link_to "Sign out", signout_path, :method => :delete %>
</li>
All it takes for the signout to start working again is the removal of ":via => :delete" from the routes file. Is this the right approach or is there a better one? Also, why did the link stop working without any rails update?
Thank you,
Alexandra
On request, I added the full code for the _header.html.erb:
full _header.html.erb
<!-- ***** Initialized: Listing 5.24 ***** -->
<!-- ***** Updated: Listing 8.24 ***** -->
<!-- ***** Updated: Listing 9.7 ***** -->
<!-- ***** Begin: Listing 9.28 ***** -->
<header>
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<% if signed_in? %>
<%= link_to "project manager", about_path, id: "logo" %>
<% else %>
<%= link_to "project manager", root_path, id: "logo" %>
<% end %>
<nav>
<ul class="nav pull-right">
<!--li><%= link_to "Home", root_path %></li-->
<% if signed_in? %>
<% if Rails.env.development? %>
<li><%= link_to "Overview", overview_path %></li>
<% end %>
<% if Rails.env.development? %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Projects <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Status", projects_path %></li>
<li><%= link_to "Dev View", dev_projects_path %></li>
</ul>
</li>
<% else %>
<li><%= link_to "Projects", projects_path %></li>
<% end %>
<% if Rails.env.development? %>
<li><%= link_to "Teams", teams_path %></li>
<% end %>
<% if Rails.env.development? %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Tasks <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Status", tasks_status_path %></li>
<li><%= link_to "Tree", tasks_tree_path %></li>
<li><%= link_to "Dev View", dev_tasks_path %></li>
</ul>
</li>
<% else %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Tasks <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Status View", tasks_status_path %></li>
<li><%= link_to "Tree View", tasks_tree_path %></li>
</ul>
</li>
<% end %>
<% if Rails.env.development? %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Reports <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Project Progress", analysis_path %></li>
<li><%= link_to "Revision History", history_path %></li>
</ul>
</li>
<% else %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Reports <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Revision History", history_path %></li>
</ul>
</li>
<% end %>
<% if Rails.env.development? %>
<li><%= link_to "Help", help_path %></li>
<% end %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<% if current_user.admin? %>
<li><%= link_to "Admin", users_path %></li>
<% end %>
<% if Rails.env.development? %>
<li><%= link_to "Profile", current_user %></li>
<% end %>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, :method => :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
<!-- ***** End: Listing 9.28 ***** -->
This thread is a bit old, but I thought I'd share a solution anyway as more people might be having the same problem.
Here is some background information on how this works before we get into the actual troubleshooting:
Notice that the Sign Out link looks as follows:
<%= link_to "Sign out", signout_path, :method "delete" %>
If you look at the source code generated by this tag, you will see this:
Sign out
Notice the part that says data-method="delete". With straight HTML, this won't work. If you click the link, your browser will simply ignore the DELETE instruction and instead submit a GET request to your server (this is what you are currently seeing in your log). To get your browser to submit a DELETE request, you will need to use some JavaScript magic. This is where the jquery_ujs.js file comes in (you will most likely be able to see a link to this file by Viewing Source in your browser; it should be in the header, close to, or included within the application.js file). If you look inside the jquery_ujs.js file, towards the middle of the file, you will see the following code:
// Handles "data-method" on links such as:
// Delete
In other words, the code above makes sure that your browser actually does submit a DELETE request (we don't have to get into the details of the actual code here).
Given this background information, there are two possible reasons why you are getting your error.
You are simply missing the jquery_ujs.js file
Other JavaScript code you have written or included is interfering with the desired
behavior of the jquery_ujs.js file
To troubleshoot, do as follows:
For 1) Load the page that contains your Sign Out link and select View Source from your browser. Make sure that the file jquery_ujs.js is in the header (or concatenated with the rest of your JS files into the application.js file, depending on your application settings)
For 2) In application.js, remove the //=require_tree . directive. Reload your page, and click the Sign Out link. If the Sign Out link is hidden under a menu that only works with your JavaScript installed, then just put a duplicate of the Sign Out link somewhere else on your page where it is accessible without JavaScript. Now try clicking on the link - it should hopefully work. If you are no longer getting the routing error, you know that this was the cause for your problem. The easiest way to troubleshoot this is to add back the //=require_tree . directive, but then to remove all your JavaScript files except the application.js file from the folder where they reside, and then to add back each file one by one, while you try the Sign Out link, until it no longer works. This will allow you to identify the troublemaker JavaScript file. Once you have identified this file, try removing all code (upon which the link should work again) and then adding back pieces of the code until it no longer works - voila, you have now identified the root of the problem! Please feel free to report back what it was. My guess is that it could be either a straight up error, a return false; statement, or a stopPropagation(); statement.
Hopefully this will work! Good luck.
Since the answers that I received in September 2012 did not solve my problem, I just ended up removing ":via => :delete" from the routes file, which made the signout link work again.
After reading eriklinde's answer, I went back to my code to see if his answer would help. The jquery_ujs.js file was not missing. So, I started looking into the second possible reason that was suggested. Nevertheless, when I went to the routes.rb file and added "via: :delete" to have a line reading "match '/signout', :to => 'sessions#destroy', via: :delete", the signout functionality continued working without a problem. Since "via: :delete" is different than ":via => :delete", which I previously removed, maybe this was causing the problem?
I think the solution is here:
Rails 3 link_to (:method => :delete) not working
Are you missing <%= javascript_include_tag :all %> in your page? It should go in your layout file.
I had this same problem and fixed it by rearranging the order of the requirements in the application.js file. Mine were ordered as such:
//= require jquery_ujs
//= require jquery
//= require bootstrap
//= require_tree .
and are now ordered like this
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
I literally did nothing else.
try changing the _header.html.erb partial to
<li>
<%= link_to "Sign out", signout_path, :method "delete" %>
</li>
Actually you are probably missing the cross site forgery protection tags. Which would explain the inability to delete via the delete method. Not sure why it was working before.
Add this to your layout file in the head section
for haml
= csrf_meta_tags
for erb
<%= csrf_meta_tags %>
My guess would be that somehow you don't have jQuery UJS installed / active.
One of the key features listed here is "make non-GET requests from hyperlinks." Try adding gem 'jquery-rails' to your gemfile, run bundle install from the command line, restart your server, and see if it works!
This should be in your application.js file (with no lines above it that don't start with //!)
//= require jquery
//= require jquery_ujs
try this :
just add into you application.js
//= require jquery_ujs