I have an application under development in Rails 3.2.13 with devise, omniauth and cancan. It all worked perfectly until I started to implement authorisation with cancan. It is even more interesting that cancan itself works like charm but generates an error in editing the user profile provided by devise. If cancan does that at all, I'm really not sure.
The error message is:
No route matches {:controller=>"devise/posts"}
I have a posts controller, but that's not linked to devise by any means. This is the strangest part in the story.
I successfully localised the spot that generates it but I can't figure out what the cause of the problem is and how to fix it. So I have a menu shown only to admins in my application.html.erb , this is the source:
<% if (user_signed_in? && (current_user.role?("sysadmin") || current_user.role?("postadmin") || current_user.role?("testadmin"))) %>
<ul class="nav navbar-nav nav-pills">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" style="color: crimson;">
<span class="glyphicon glyphicon-cog"></span> Administration <b class="caret"></b></a>
<ul class="dropdown-menu">
<% if (current_user.role?("sysadmin") || current_user.role?("postadmin")) then %>
<li><%= link_to 'Posts', :controller => :posts, :action => :index %></li>
<% end %>
<% if (current_user.role?("sysadmin") || current_user.role?("testadmin")) then %>
<li><%= link_to 'Itests', :controller => :itests, :action => :index %></li>
<% end %>
</ul>
</li>
</ul>
<% end %>
What's really interesting is that if I delete the <ul>...</ul> block so to leave nothing but a naked if ... end block, it works. Also, it works for users not having any of the three admin roles.
But in the <ul>...</ul> block there's nothing else but HTML, Bootstrap styling and some inline ruby links to some other controllers.
How does this breaks the "edit profile" feature of devise?
Check out this question: Rails No route matches {:controller=>"devise/products"}
Basically you're in device namespace and you have to use path helpers:
<%= link_to 'Posts', posts_path %>
or:
<%= link_to 'Posts', :controller => '/posts', :action => :index %>
Related
I am trying to log out a user from a (test) site in production on heroku.
The problem is heroku transforms this <%= link_to 'Logout', destroy_user_session_path, method: :delete %> in a GET /users/sign_out
I've tried this <%= link_to 'Logout', destroy_user_session_path, method: :delete, :data => { :no_turbolink => true } %> as suggested here Devise destroy_user_session_path does'nt work
I've found some solutions in reply to destroy_user_session_path is triggering GET instead of DELETE in Rails but I don't feel comfortable with the answers as I'd like to avoid a GET.
Logout was ok in development, so I checked on https://devcenter.heroku.com/ and made some modifications, but I am still stuck.
I worked on the asset pipeline and added these gems:
gem 'rails_serve_static_assets'
gem 'rails_stdout_logging'
These gems are probably useful in other cases, but not mine (I also have a problem with links to images that don't persist).
EDIT
Here is my full template
<div class="" style="background-color: #1a252f;">
<ul class="nav justify-content-center">
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to 'Edit account|', edit_user_registration_path %></li>
<li class="nav-item">
<%= link_to 'Logout', destroy_user_session_path, method: :delete, :data => { :no_turbolink => true } %></li>
<% else %>
<li class="nav-item">
<%= link_to 'Login', new_user_session_path %></li>
<% end %>
</ul>
</div>
EDIT 2
I've also tried this in routes.rb:
devise_for :users do
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
and I still get a status 404 in the heroku logs.
The solution provided here: destroy_user_session_path is triggering GET instead of DELETE in Rails was the one for me,
i.e. with config.sign_out_via = :get in devise.rb
I feel like I'm missing something really simple and I keep spinning my wheels on this problem.
I currently have internationalization working throughout my app. The translations work and the routes work perfectly. At least, most of the site works with the exception of the routes to my two static pages, my "About" and "FAQ" pages.
Every other link throughout the app points to the proper localized route. For example if I select "french" as my language, links point to the appropriate "(/:locale)/controller(.:format)." However, despite the changes I make throughout the app my links for the "About" and "FAQ" refuse to point to "../fr/static/about" and always point to "/static/about."
To make matters stranger, when I run rake routes I see:
"GET (/:locale)/static/:permalink(.:format) pages#show {:locale=>/en|fr/}"
and when I manually type in "../fr/static/about" the page translates perfectly.
My Routes file:
devise_for :users
scope "(:locale)", :locale => /en|fr/ do
get 'static/:permalink', :controller => 'pages', :action => 'show'
resources :places, only: [:index, :show, :destroy]
resources :homes, only: [:index, :show]
match '/:locale' => 'places#index'
get '/'=>'places#index',:as=>"root"
end
My ApplicationController:
before_filter :set_locale
def set_locale
I18n.locale=params[:locale]||I18n.default_locale
end
def default_url_options(options={})
logger.debug "default_url_options is passed options: #{options.inspect}\n"
{ :locale => I18n.locale }
end
and My Pages Controller:
class PagesController < ApplicationController
before_filter :validate_page
PAGES = ['about_us', 'faq']
def show
render params[:permalink]
end
def validate_page
redirect_to :status => 404 unless PAGES.include?(params[:permalink])
end
end
I'd be very grateful for any help ... it's just been one of those days.
Edit: Thanks to Terry for jogging me to include views.
<div class="container-fluid nav-collapse">
<ul class="nav">
<li class="dropdown">
<%= t(:'navbar.about') %><b class="caret"></b>
<ul class="dropdown-menu">
<li><%=link_to t(:'navbar.about_us'), "/static/about_us"%></li>
<li><%=link_to t(:'navbar.faq'), "/static/faq"%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
</ul>
</li>
You should give a name to your route by "as":
scope "(:locale)", :locale => /en|fr/ do
get 'static/:permalink', to: 'pages#show', as: :static
And then in view build link with routing helpers (route_name_path):
<li><%=link_to t('navbar.about_us'), static_path(permalink: "about_us") %></li>
This helper automatically add current locale to path.
To get list of all routes with names use rake routes console command.
Good luck!
Interesting. Ended up solving this problem by using url_for in the view:
<div class="container-fluid nav-collapse">
<ul class="nav">
<li class="dropdown">
<%= t(:'navbar.about') %><b class="caret"></b>
<ul class="dropdown-menu">
<li><%=link_to t(:'navbar.about_us'), url_for('static/about_us')%></li>
<li><%=link_to t(:'navbar.faq'), url_for('static/faq')%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
</ul>
</li>
I've personally never used url_for, so I'm not sure if this is the most graceful solution, however I'm happy that it works!
Edit:
Actually going back to code, this produced a really bad result. It worked from root_url and most other paths, however if the current path was /fr/static/about_us or /fr/static/faq the above lines actually called "/fr/static/static/about_us" or "/fr/static/static/faq" - rectified with:
<ul class="dropdown-menu">
<% if request.fullpath == '/fr/static/about_us' or request.fullpath == '/fr/static/faq' %>
<li><%=link_to t(:'navbar.about_us'), url_for('/fr/static/about_us')%></li>
<li><%=link_to t(:'navbar.faq'), url_for('/fr/static/faq')%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
<% elsif request.fullpath == '/en/static/about_us' or request.fullpath == '/en/static/faq' %>
<li><%=link_to t(:'navbar.about_us'), url_for('/en/static/about_us')%></li>
<li><%=link_to t(:'navbar.faq'), url_for('/en/static/faq')%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
<% elsif request.fullpath == '/static/about_us' or request.fullpath == '/static/faq' %>
<li><%=link_to t(:'navbar.about_us'), url_for('/en/static/about_us')%></li>
<li><%=link_to t(:'navbar.faq'), url_for('/en/static/faq')%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
<% else %>
<li><%=link_to t(:'navbar.about_us'), url_for('static/about_us')%></li>
<li><%=link_to t(:'navbar.faq'), url_for('static/faq')%></li>
<li><%=link_to t(:'navbar.blog'), '#' %></li>
<%end%>
</ul>
Moving on to another problem now, but if anyone has a more graceful way of doing this feel free to contribute. Will probably re-examine this problem early next week.
I want to display the little drop down menu symbol in my rails app that uses twitter bootstrap via the link_to method in my rails app.
I can't figure out how to do it. When I add the <b class="caret"></b> (that twitter bootstrap uses) to the 'Setup ' argument below, it gets escaped and shows up as text not html.
<%= link_to 'Setup ', root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' %>
Of all the rails examples that I have seen so far, none use link_to when using this feature of TBS (and include the caret).
Try this,
<%= link_to 'Setup <b class="caret"></b>'.html_safe, root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' %>
I remember having an issue with this very thing. If I remember correctly it had to do with the tag order - it had to be inside a <li> tag. Here's my code that works:
<ul class="nav" role="navigation">
<li class="explore">
<%= link_to 'explore <b class="caret"></b>'.html_safe, root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' %>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop1">
<li class="nav-header">Majors</li>
<li class="dropdown-link"><%= link_to "View All", majors_path %>
<li class="dropdown-link">By School</a></li>
<li class="dropdown-link">Recently Added</a></li>
<li class="dropdown-link">Most Liked</a></li>
</ul>
</li>
</ul>
Thanks to Amar I didn't have to play around with converting it into erb as he did it. Here's what my code now looks like with Amar's help.
link_to takes a block so you can do
link_to root_path, :id => 'setupdrop', :'data-toggle' => 'dropdown', :class => 'dropdown-toggle' do
Setup
<b class="caret"></b>
end
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
I followed the basic railscast for using Twitter Bootstrap in Rails application. Everything was great, until I had to push to heroku, then I had some problems with pg gem, and I had to rake assets:precompile to push it ok. Finally I solved.
Now, I'm trying to use pills in my application, I have copy/paste from documentation and changed the url in href :)
<div class="container">
<div class="row">
<div class="span3">
<p> Lorem Ipsum</p>
</div>
<div class="span9">
<ul class="nav nav-pills">
<li class="active">Home</li>
<li>Products</li>
<li>Categories</li>
</ul>
</div>
</div>
</div>
When I push one of the links, I'm redirected to the right url but the selected option doesn't change to class="active". I don't know why... I thought it was the javascript but hover property works ok... I mean, when the mouse is over an option (diferent from active) its style changes ok.
I tried rake assets:clean, but no change is made
Thanks
You actually have to handle this by yourself!
Your list should look something like
<li class="<%= 'active' if params[:controller] == 'yourdefaultcontroller' %>">Home</li>
<li class="<%= 'active' if params[:controller] == 'products' %>">Products</li>
<li class="<%= 'active' if params[:controller] == 'categories' %>">Categories</li>
You need to specify in each request which tab is the active one. You can do this by relying on the name of the controller (and action if need be) that is passed in the params hash.
You can use something like this:
<li class="<%= 'active' if current_page?(root_path) %>"><%= link_to "Home", root_path %></li>
<li class="<%= 'active' if current_page?(about_path) %>"><%= link_to "About", about_path %></li>
<li class="<%= 'active' if current_page?(contact_path) %>"><%= link_to "Contact", contact_path %></li>
I used a helper to implement this in the style of Rails' form helpers.
In a helper (e.g. app/helpers/ApplicationHelper.rb):
def nav_bar
content_tag(:ul, class: "nav navbar-nav") do
yield
end
end
def nav_link(text, path)
options = current_page?(path) ? { class: "active" } : {}
content_tag(:li, options) do
link_to text, path
end
end
Then, in a view (e.g. app/views/layouts/application.html.erb):
<%= nav_bar do %>
<%= nav_link 'Home', root_path %>
<%= nav_link 'Posts', posts_path %>
<%= nav_link 'Users', users_path %>
<% end %>
This example produces (when on the 'users' page):
<ul class="nav navbar-nav">
<li>Home</li>
<li>Posts</li>
<li class="active">Users</li>
</ul>