I've searched for an answer to this and nothing has helped.
I have the following in my view:
<div id="user_nav">
<% if user_signed_in? %>
Logged in as <strong><%= current_user.first_name + " " +current_user.last_name%></strong>.
<%= link_to 'Edit profile', edit_user_registration_path %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> |
<%= link_to "Login", new_user_session_path %>
<% end %>
Which is straight from the railscast on devise. Suddenly the paths are not working anymore. (sign up or sign in) for example when I click login i get the error
No route matches {:controller=>"devise/home", :action=>"students"}
Ive tried using <%= link_to "Login", :controller => '/devise/sessions', :action => 'new' %> jsut for kicks and it returns the same error.
Yes I have devise_for :users in my route file.
rake routes return all the right routes including
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
Any help would be awesome! I'm stumped!
Looking at my stack trace, something (a link) in my layout was throwing it off. I created a custom layout for devise and everything works fine now.
Related
I added the devise gem to my rails app. Then I appended the url with user/sign_up as that is self created by the app. Then the following page shows up -
I am unable to locate the file with the following code.
This is what the bottom of the page looks like -
Do let me know what more information I can provide.
Thanks for your time and help.
EDIT 1 Added routes.rb for your reference
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts, only: :create
get 'contact-us', to: 'contacts#new', as: 'new_contact'
end
EDIT 2- Added the following piece of code to my application.html.erb file
<% if user_signed_in? %>
<%= link_to "Log Out", destroy_user_session_path, method: :delete, class: "btn btn-default navbar-btn" %>
<% else %>
<%= link_to "Log In", new_user_session_path, class: "btn btn-default navbar-btn" %>
<%= link_to "Sign Up", new_user_registration_path, class: "btn btn-default navbar-btn" %>
<% end %>
And a page displayed the error: NoMethodError in Pages#home(as in the image added) with the subsequent lines-
Showing /home/ec2-user/environment/saasapp/app/views/layouts/application.html.erb where line #31 raised:
undefined method `user_signed_in?' for #<#Class:0x000000047464c8:0x007f399e3e7ab8>
Did you mean? user_session_url
Did you follow devise docs for installing devise in rails? There is something to do with your routes.rb. Go through this article https://guides.railsgirls.com/devise and follow all the steps carefully. Also post your routes.rb maybe the problem is with your routes. Thanks, This type of question is asked here just try those step as well.
See this link as well.
I am using the Gem 'Devise' in order to set users and admins for my page. I have created a log in page which requires the user to first sign in and then after the succesful log in I want to redirect the user to the home page of the Model Category, which is gonna be displaying just a list of categories of products. Yet, I have not found the proper way to do it. This is my routes file:
Rails.application.routes.draw do
resources :categories
root to: 'pages#home'
devise_for :users
end
And this is my home.html.erb file inside the view pages :
<h1>Login page</h1>
<% if current_user %>
<%= link_to 'Categories', category_path(#category) %>
<%= link_to 'Sign Out', destroy_user_session_path, method:
:delete %>
<% else %>
<%= link_to 'Sign Up', new_user_registration_path %>
<%= link_to 'Sign In', new_user_session_path %>
<% end %>
I get an error saying:
ActionView::Template::Error
(No route matches {:action=>"show",:controller=>"categories", :id=>nil},
missing required keys: [:id]):
How can I connect the home.html.erb file of the view Page with the index.html.erb of the Category view? Thanks in advance for any help provided
How can I connect the home.html.erb file of the view Page with the index.html.erb of the Category view?
If what you want is a link to view all categories, then why are you using a helper to view one singular category, category_path(#category) (and passing a non-existing #category too)? Instead, use this:
<%= link_to 'Categories', categories_path %>
I've never encountered this problem before. I'm getting this error.
No route matches [GET] "/recipes/1/like"
Here is my routes.rb:
Rails.application.routes.draw do
root 'pages#home'
get '/home', to: "pages#home"
resources :recipes do
member do
post 'like'
end
end
end
Here is my recipes_controller:
def like
#recipe = Recipe.create(params[:id])
Like.create(like: params[:like], chef: Chef.first, recipe: #recipe)
#flash message
flash[:success] = "Your selection was sucessful"
redirect_to :back
end
Here is my html.erb file:
<%= render 'shared/page_title', title: #recipe.name.titleize %>
<div class= "row">
<div class="col-md-4 pull-right center">
<%= gravator_for #recipe.chef, size: 200 %>
<p>
<h5>Brought to you by: <%= #recipe.chef.chefname.capitalize %></h5>
</p>
</div>
<div class= "col-xs-8 col-md-8">
<%= link_to "Edit this Recipe", edit_recipe_path(#recipe), class: "btn btn-success pull-right" %>
<h3><%= #recipe.summary.capitalize %></h3>
<div class="show_recipe">
<%= image_tag(#recipe.picture.url, size: "300x200", class: "recipe-image") if #recipe.picture? %>
</div>
<div class ="well recipe-description">
<p>
<strong> Steps:</strong>
</p>
<%= simple_format(#recipe.description) %>
<div class="pull-right">
<%= link_to like_recipe_path(#recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i>
<% end %>     
<%= link_to like_recipe_path(#recipe, like: false), :method => :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i>
<% end %>
</div>
</div>
</div>
</div>
<h5><%= link_to "Return to Recipes Listings", recipes_path, class: "btn btn-warning btn-small" %></h5>
I've explicitly added the HTTP POST request to my html.erb file
%= link_to like_recipe_path(#recipe, like: true), method: :post do %>
but rails is complaining that there is no GET route request, which I never created in my routes because I need a POST request for this particular section of the web app.
Rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
home GET /home(.:format) pages#home
like_recipe POST /recipes/:id/like(.:format) recipes#like
recipes GET /recipes(.:format) recipes#index
POST /recipes(.:format) recipes#create
new_recipe GET /recipes/new(.:format) recipes#new
edit_recipe GET /recipes/:id/edit(.:format) recipes#edit
recipe GET /recipes/:id(.:format) recipes#show
PATCH /recipes/:id(.:format) recipes#update
PUT /recipes/:id(.:format) recipes#update
DELETE /recipes/:id(.:format) recipes#destroy
I am honestly lost. It seems that everything is in its right place.
Rails version:
Rails 4.2.5
I've defined the action, created the like model, nested the route under recipes, and explicitly requested for a post HTTP request in the html.erb page.
Any ideas would be great!
Cheers!
Here's the relevant code from my working project with a similar arrangement.
# /views/events/splits.html.erb:
<%= link_to "Add",
associate_splits_event_path(id: #event.id, split_ids: [split.id]),
:method => :put,
:class => 'btn btn-xs btn-success' %>
# routes.rb
resources :events do
member { put :associate_splits }
end
If it's helpful to see it in context, feel free to poke around the repo. Here's a link to the view: https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb
While rails link_to does allow links to make requests with a method other than GET, it is not the normal behavior of links and relies on Javascript to make a form which gets submitted behind the scenes. Rails uses jquery-ujs for this, so make sure you haven't disabled it.
That said, making POST requests is normal behavior for forms, so you could also try using button_to instead, and simply styling the button to look like a link if necessary.
<%= button_to "Like", like_recipe_path(#recipe), method: :delete, like: true %>
Additional Info:
https://github.com/rails/jquery-ujs
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
Try with
<%= link_to "LIKE", like_recipe_path(#recipe) , method: :post %>
Other option:
<%= link_to '<i class="glyphicon glyphicon-thumbs-up"></i>'.html_safe , like_recipe_path(#recipe , like: 'value' ) , method: :post %>
You need to add a GET route:
resources :recipes do
member do
get 'like'
post 'like'
end
end
I'm using devise for user authentication. In my views I've set:
<% if user_signed_in? %>
<li><%= link_to "Log Out", destroy_user_session_path %></li>
<% else %>
<li><%= link_to "Sign In", new_user_session_path %></li>
<% end %>
However when I click Log_Out I'm getting an error:
No route matches [GET] "/users/sign_out"
However when I check my rake routes I'm getting:
devise/sessions#destroy destroy_user_session DELETE /users/sign_out(.:format)
What apneadiving said.
<%= link_to "Log Out", destroy_user_session_path, method: :delete %>
Default sign out is use "delete" method. Your route also said the method is “DELETE"
If you want use "get" method
Modify devise.rb to
config.sign_out_via = :get
Baloo is right, make sure to use the :delete method. You can see this clearly if you invoke
rake routes
You'll see the path as well as the method.
I have a rails 3 app, and when I click the link to my terms page, it routes to a completely different controller, than what the routes should use. Stranger still, the route works when I'm not logged in, and I'm using devise.
I get this error when clicking the link when I'm logged in.
No route matches {:action=>"edit", :controller=>"users", :id=>nil}
<%= link_to "Terms", terms_path %>
Routes (in the order they appear in routes.rb):
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
member do
get :following, :followers
post :accept
end
end
match '/terms', to: 'static_pages#user_agreement'
Static Pages Controller
def user_agreement
end
Rake Routes
terms /terms(.:format) static_pages#user_agreement
This also happens for every other action that I've routed this way to the staticpages controller, but not for any other actions that route to different controller.
Update: Terms Page
Header:
<%= link_to "Follow", users_path %>
<%= link_to current_user.name, current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
Footer:
<%= link_to "Welcome", welcome_path %>
<%= link_to "Settings", edit_user_path(#user) %>
<%= link_to "Terms", terms_path %>
All the content is pure html.
Thanks in advance
You have a link to edit_user_path with no #user as hinted in the comments.
You should almost certainly be using current_user anyway.