getting No route matches [GET] "/show_house_search.38" in rails - ruby-on-rails

I defined a new method in my controller to show customized show page for my houses after one click on search results.
houses_controller.rb
def show_house_search
#house = House.find(params[:id])
#photos = #house.photos
end
then I declared the routes in routes.rb
get '/show_house_search/:id', to: 'houses#show_house_search', as:'show_house_search'
and when I am using it as
<%= link_to show_house_search_path(house.id),id:'house_search' do %>
<li id="house-<%= house.id %>">
<span class="user" style="font-size: 15px;font-weight: 200;text-decoration: dotted;"><%= house.title %></span>
<span class="content"style="font-size: 15px;color: purple;font-weight: 600;">Price <i class="fa fa-inr"></i><%= house.price %></span>
<span class="badge"><%= house.house_structure%></span> </span>
<span class="badge"><%= house.location%></span> </span>
</li>
<%end%>
clicking on this link is showing me this error:
Routing Error
No route matches [GET] "/show_house_search.38"
while I also searched for a route for my request using rails routes
show_house_search_path GET /show_house_search/:id(.:format) houses#show_house_search

try below code:
<%= link_to show_house_search_path(id: house.id) ,id:'house_search' do %>

Please change menthod
show_house_search_path(house.id)
to
show_house_search_path(id: house.id).
As shown in routes.rb, path is /show_house_search/:id(.:format), it seems that id is considered as :format hence appending to routes as( .id).

Related

ActionController::UrlGenerationError in Users#show No route matches error

I am creating a users controller to show the users profile page but i am getting this error
ActionController::UrlGenerationError in Users#show
No route matches {:action=>"show", :controller=>"users", :id=>nil}
missing required keys: [:id] error
I am trying to add a profile page for each user so when the user for example goes to
www.mysite.com/users/jack
it will show their profile page
Here's my code:
layouts/_navbar.html.erb
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Skillbook</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
</ul>
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<td><%= link_to "Profile", user_path(current_user.username) %></td>
<td><%= gravatar_tag current_user.email, size: 20 %><%= current_user.username %></td>
<li><%= link_to "Edit user", edit_user_registration_path %></li>
<li><%= link_to "Log out", destroy_user_session_path ,method: :delete %></li>
<%else%>
<li><%= link_to "Log in", new_user_session_path %></li>
<li><%= link_to " Sign up", new_user_registration_path%></li>
<%end%>
</ul>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show]
private
def set_user
#user = User.find_by(username: params[:id])
end
end
routes.rb
Rails.application.routes.draw do
get 'pages/home'
devise_for :users
root 'pages#home'
resources :users, only: [:show, :index]
end
Well, this error simply is what it is:
missing required keys: [:id]
Where, according to your setup, is the username of the currently signed-in user.
In a nutshell, what this is telling you is that you are trying to pass in the username of the signed-in user, but for this instance, the user that is signed in has no username.
To fix this, make sure that the username of the signed-in user, (and all other users for that matter) is not nil
In url you should pass an id of the user, and you do not do that.
What you really want to incorporate is friendly_id gem.
With it set up you will be able to actually present users id as name(or whatever other attribute/combination of attributes you want):
www.mysite.com/users/1
will become
www.mysite.com/users/jack
In your routes file add
get '/users/:id', to: 'users#show', as: '/users/:username'
The last part as: is where you are naming your route. This would give the user with username 'jack' the route /users/jack. Likewise if you wanted to get rid of users altogether you could put as: '/:username'
No need to install another gem.
Justin

How to display icon in rails?

Hello I want to use the icon? or is it the alternative span tag? (No idea what its actually called) Anyways, I want to display a icon in my menu here is my current source code.
<li><i class="material-icons left">person</i><%= link_to signup_path %></li>
This is what I currently get
And here is the html I want it to generate:
<li><i class="material-icons">person/i></li>
Is there anyway to accomplish this in rails?
To create an ERB link in Rails that accomplishes the normal HTML code you provided, do this:
<%= link_to signup_path do %>
<i class="material-icons left">person</i>
<% end %>
This will generate:
<i class="material-icons left">person</i>
Only posting this answer because it is possible to do it this way.. definitely not preferred :)
<%= link_to '<i class="material-icons left">person</i>'.html_safe, signup_path %>
<%= link_to signup_path do %>
<i class="material-icons left"> person </i>
<% end %>

Strange error blocks users from editing devise profile

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 %>

Dynamically add active class to bootstrap li in Rails

in the bootstrap navigation bar. You can get the effect of a button being clicked by adding the class active . Naturally, I want to use this on my pages. For example if I'm on the about us page I want the about us button clicked.
What is the best way to go about this? I was going to go to each page and at the bottom have a jQuery function add the class active to it. Is there a better way?
Read about current_page? here
You can add a method for handle logic with current_page?, example a method :
module ApplicationHelper
def active_class(link_path)
current_page?(link_path) ? "active" : ""
end
end
example bootstrap navbar template
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul class="nav">
<li class="active">Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
</div>
So, on view looks like
HTML
<li class="<%= active_class(some_path) %>">
<%= link_to "text of link", some_path %>
</li>
HAML
%li{:class => active_class(some_path)}
= link_to "text of link", some_path
Or you can use request.fullpath to get current full of path if a current path have a parameter
example
<ul>
<% Contry.all.each do |c| %>
<li class="snavitem <%= active_class(contry_path(c)) %>">
<%= link_to "show #{c.name}", contry_path(c) %>
</li>
<% end %>
</ul>
and on your application_helper.rb
def active_class(link_path)
request.fullpath == link_path ? "active" : ""
end
read about request.fullpath here
in my opinion, a cleaner way to achieve that is to write a link_to_in_li method in application_helper.rb:
def link_to_in_li(body, url, html_options = {})
active = "active" if current_page?(url)
content_tag :li, class: active do
link_to body, url, html_options
end
end
then use it this way
<%= link_to_in_li "Home", root_path, id: "home_link" %>
I find the code inside li a little difficult to read.
For anyone having trouble making sense of this, here is an example with my paths and filenames laid out explicitly. As a pretty new person to rails, I was having trouble figuring it out. Thanks to the other people who answered above, as it helped me figure it out!
I placed the Bootstrap navbar in my application.html.erb file:
<div class="navbar-header">
<a class="navbar-brand" href="/">Mapper</a>
<ul class="nav navbar-nav">
<li class="<%= is_active?('/') %>"><%= link_to "Home", '/' %></li>
<li class="<%= is_active?('/main/map') %>"><%= link_to "Map", '/main/map' %></li>
<li class="<%= is_active?('/main/about') %>"><%= link_to "About", '/main/about' %></li>
</ul>
</div>
This goes in the application_helper.rb file:
module ApplicationHelper
def is_active?(link_path)
current_page?(link_path) ? "active" : ""
end
end
That's it! Now your application will dynamically add the 'active' class to whatever page is currently being viewed (i.e. it's corresponding list item in the navbar). This is much simpler (and more DRY) than adding the navbar manually to each page (view) and then updating the 'active' class.
I'll post my answer that I created based on these others because in case of CRUD views the active class wasn't been placed.
module ApplicationHelper
def active_class(name)
controller_name.eql?(name) || current_page?(name) ? 'active' : ''
end
end
My views use something like this:
<ul class="nav navbar-nav">
<li class="nav-item <%= active_class('/') %>">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item <%= active_class('leads') %>">
<a class="nav-link" href="/leads">Leads</a>
</li>
</ul>
<ul class="nav navbar-nav pull-right <%= active_class(edit_user_registration_path) %>">
<li class="nav-item ">
<a class="nav-link" href="/users/edit">Perfil</a>
</li>
<li class="nav-item">
<%= link_to('Sair', destroy_user_session_path, method: :delete) %>
</li>
</ul>
Please try this in each page, check the cotroller or action and add the css
For example:
<li class= <%= (controller.controller_name.eql?('pages') && controller.action_name.eql?('index') )? 'active':''%> ><%= link_to 'my page', pages_path%></li>
You may define a helper method in application_helper.rb
def create_link(text, path)
class_name = current_page?(path) ? 'active' : ''
content_tag(:li, class: class_name) do
link_to text, path
end
end
Now you can use like:
create_link 'xyz', any_path which would render as <li class="active">xyz</li>
Perfect for bootstrap navigation!
Why limit yourself to only li elements? And why not support multiple class names along with active? This solution lets me:
Support not only plain text but HTML inside link_to (e.g. add an icon inside the link)
Add just few lines of code to application_helper.rb
Append active to the whole class name of the link element instead of it being the sole class.
So, add this to application_helper.rb:
def active_class?(class_name = nil, path)
class_name ||= ""
class_name += " active" if current_page?(path)
class_name.strip!
return class_name
end
And on your template you can have something like this:
<div class="col-xs-3">
<%= link_to root_path, :class => active_class?("btn btn-outline-primary", root_path) do %>
<i class="fa fa-list-alt fa-fw"></i>
<% end %>
</div>
You can also specify or not a class_name and use it like this:
<li class="<%= active_class?(root_path) %>">Home</li>
Thanks to previous answers 1, 2 and resources.

Twitter Bootstrap Pills with Rails 3.2.2

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>

Resources