I'm adding a nav menu in application.html.erb which navigate users in authlogic. All routers like: :logout, :register and :login seems working with custom paths.
match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
match 'register' => 'users#new', :as => :register
Here is nav menu:
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(current_user.id)%> <%=h current_user.firstname %>
<%= link_to "Logout", :logout %>
<% else %>
<%= link_to "Register", :register %> |
<%= link_to "Login", :login %>
<% end %>
But edit_user_path is transferring me to /users/:id/edit. How do I make the nice URL for this path. I would like to make it /account/edit. It also need to disable this path /users/:id/edit to prevent user from requesting another user ID which doesnt belong to him/her. It should throw a 404 page is perfect.
Current paths in the nav menu:
Logout: /account/logout
Login: /account/login
Register: /register
I would love to have another path for edit_user_path:
Edit profile: /account/edit
Is there any way I can simply use edit_user_path(current_user.id) and the path automatically transfers me to /account/edit and disable id request.
Any help would be much appreciated! Thanks!
[Updated] This is my /config/router.rb
Appcatous::Application.routes.draw do
resources :users, :user_sessions
match "account" => "users#show", :as => :account
match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
end
users.rb model is very simple:
class User < ActiveRecord::Base
acts_as_authentic
validates :firstname, :presence => true
validates :lastname, :presence => true
end
It would be helpful if you included all the routes in question - which means also the routes for the users. Did you create those with:
resources :users
In case you did, you'll have to disable that command and recreate just the routes you need manually with (example!):
match 'account/edit' => 'users#edit', :as => :edit_user
or whatever your controller is named. That way you'll get your "/account/edit" - however, you'll also have no way to transfer the user id that way. That one you'll have to transfer by other means, like through a variable stored in a cookie or a session.
The latter method is the usual way of remembering user authentification anyway.
Related
I have a settings tab which links to the user to edit_user_registrations_path corresponding to the devise/registrations/edit.html.erb page.
I created another page under devise/registrations called edit_account.html.erb and I'd like this to allow the user to edit additional settings like Twitter and any other social networks that allow it.
I keep getting a routing error. This is the route I tried using with no luck:
devise_scope :user do get "/edit/edit_account" => "devise/registrations#edit_account" end
Thanks in advance!
The way I do it in my routes file is like this:
devise_scope :user do
put "edit/edit_account", :to => "devise/registrations#edit_account",
:as => "edit_account"
end
and then like this:
<%= simple_form_for(resource, :as => resource_name, :url => edit_account_path(resource_name), :html => { :method => :put }) do |f| %>
My users_controller has these methods
def follow_code
#user = current_user
end
def followsubmit
redirect_to root_path
end
My route file has
match "follow_code" => "users#follow_code", :as => "follow_code"
match "follow_code" => 'users#followsubmit', :as => "follow_code", :via => 'post'
My follow_code.html.erb view has
<%= form_tag(follow_code_path, :method => 'post') do %>
<%= submit_tag("Submit") %>
<% end %>
Yet for some reason when I click submit on my view I am never redirected to my root_path and instead the follow_code view is re-rendered.
What am I doing wrong? Thanks.
I'm also curious about this. I ran into it today, using match as well, and my solution was to rename the post action:
match "follow_code" => "users#follow_code", :as => "follow_code"
match "save_follow_code" => 'users#followsubmit', :as => "save_follow_code", :via => 'post'
However, I was using the condition attribute to specify the method. In your case, you may just need to specify the first one as a get.
match "follow_code" => "users#follow_code", :as => "follow_code", :via => 'get'
match "follow_code" => 'users#followsubmit', :as => "follow_code", :via => 'post'
I'm trying to incorporate Devise and Cancan into a web app. I want users with :role => "admin" to be able to delete users, and Devise's destroy action only allows users to delete themselves, so I've created a custom action for this purpose. (To override the plugin's controller file, I've copied the registrations controller over to app/controllers/registrations_controller.rb.)
Here is my custom action in my registrations_controller.rb:
def destroy_user_account
#user = User.find_by_id(params[:user])
#user.destroy
redirect_to profiles_path, :flash => { :success => "User deleted!" }
authorize! :destroy, User, :message => "You don't have authorisation to delete this user."
end
Here is how I'm trying to use it, in a link on the page where you view a user's profile. (I have things set up so that each user has_one profile; profiles are what you see at the front end. A profile is automatically created in the profiles table on user registration.)
<% if can? :update, #profile %>
| <%= link_to 'Edit Profile', edit_profile_path(#profile) %>
| <%= link_to 'Edit Settings', edit_settings_path %>
<% end %>
<% if can? :destroy, #profile.user %>
| <%= link_to "Delete User", destroy_user_account(#profile.user),
:class => "delete",
:confirm => "Are you sure?",
:title => "Delete #{#profile.user.name}"
%>
<% end %>
My tests are showing 2 failures that I can't resolve:
1) ProfilesController GET show when signed in as an admin should
have a link to edit the profile
Failure/Error: get :show, :id => #profile
ActionView::Template::Error:
undefined method destroy_user_account' for #<#<Class:0x105b474a8>:0x1057f32e8>
# ./app/views/profiles/show.html.erb:41:in_app_views_profiles_show_html_erb___917863454_2195331000_0'
# ./spec/controllers/profiles_controller_spec.rb:143
2) ProfilesController GET show when signed in as an admin should
have a link to delete the user's account (using the
destroy_user_account action in the registrations controller)
Failure/Error: get :show, :id => #profile
ActionView::Template::Error:
undefined method destroy_user_account' for #<#<Class:0x105b474a8>:0x105806d20>
# ./app/views/profiles/show.html.erb:41:in_app_views_profiles_show_html_erb___917863454_2195331000_0'
# ./spec/controllers/profiles_controller_spec.rb:148
Also, when I try it out in my browser, clicking on the "Delete user" link gets me the following error:
Routing Error
No route matches "/destroy-user-account/2"
Here are the routes that should cover this:
devise_for :users, #:path => '',
:skip => [ :confirmations, :passwords, :registrations ],
:controllers => { :registrations => "registrations" } do
# Routes for ACCOUNT REGISTRATIONS
get "join", :to => "registrations#new", :as => :new_user_registration
post "join", :to => "registrations#create", :as => :user_registration
get "settings/account", :to => "registrations#show", :as => :settings
get "settings/account/edit", :to => "registrations#edit", :as => :edit_settings
put "settings/account", :to => "registrations#update", :as => :update_settings
delete "close-my-account/:id", :to => "registrations#destroy", :as => :close_my_account
delete "destroy-user-account/:id", :to => "registrations#destroy_user_account", :as => :destroy_user_account
Can anyone help with what I'm doing wrong?
In the browser it isn't matching the route because you're sending a GET request but the route only matches a DELETE request. The test fails because the route gives the name destroy_user_account_path instead of destroy_user_account.
Try:
<%= link_to "Delete User", destroy_user_account_path(#profile.user),
:class => "delete",
:confirm => "Are you sure?",
:title => "Delete #{#profile.user.name}"
:method => :delete
%>
I have been working with rails3, here the view.html.erb form have one login button, so when i click on that button, gives no routes matches :controller => 'home', :action => 'login'. But i have put that in routes.rb. Why this happening?
view.html.erb
<%= form_tag( { :controller => 'home', :action => 'login' }, { :method
=> 'post'}) do %>
<%= text_field(:name, :name, :class => "span2",:placeholder =>
"Username") %>
<%= password_field_tag(:password, :password, :class =>"span2") %>
<%= submit_tag "Login", :class => "btn btn-primary" %>
<% end %>
**routes.rb**
resources :home
resources :home do
post :login, :on => :member
end
**homecontroller.rb**
class HomeController < ApplicationController
def login
end
end
You have defined "resources :home" twice, first declaration is useless and overrides second.
Since you used resources to define your routes (which is recomended) you should use the helper method generated, in this case its login_home_path instead of the old syntax { :controller => 'home', :action => 'login' }
First,
You declare resources :home two times.
try this way in your routes.rb
resources :home
match '/login', to: 'home#login'
and use login_path in submit tag.
I would prefere for login, logout, create Sessions controller
rails generate controller Sessions --no-test-framework
and for login create new method and for logout(signout) create destroy method
I'm trying to pass some values through a link and I want them to be invisible. These are the options I've tried:
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
<%= link_to 'Add comment',{ :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id}, :method => :post %>
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id, %>
<%= link_to 'Add comment', new_comment_path, :idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
First option - treats method as a parameter:
http://localhost:2000/comments/new?idea_id=1&method=post&user_id=1
Second option - goes like this: http://localhost:2000/comments/new?idea_id=1&user_id=1
and also causes routing error: "Routing Error No route matches "/comments/new"
Third option - loads the form, but of course is like: http://localhost:2000/comments/new?idea_id=1&user_id=1
Fourth option - looks good (http://localhost:2000/comments/new) but the same routing error like the second one.
What am I doing wrong?
Thanks in advance.
PS
I was asked to give my routes, so here they are:
resources :rights
resources :comments
resources :ideas
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'main#home'
#match '/comments/new' => "comments#new" # this doesn't help
match '/home', :to => 'main#home'
match '/contact', :to => 'main#contact'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/signup', :to => 'users#new'
If you have RESTful routes
<%= link_to 'Add comment', new_comment_path,
:idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
should be
<%= link_to 'Add comment', comments_path,
:idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
As others have said, it sounds like you have a problem in your routes file. Either check if you have the resource :comments defined or post your routes file here and we'll help you out. It's possible that it's not working because you are trying to POST...
If you want 'invisible' variables (I assume you mean that you do not want the variables to appear in the URL), you'll have to POST to the page rather than just link to it. In this case, your second example is the best bet. It goes against convention to POST to /new so that could be what is causing the 'no routes' error if you're using resource :comments
Give this a try in your routes.rb:
match '/comments/new' => "comments#new"
Give that a try, it should load the correct page and give you access to the variables you passed through to it through params.
Please note, that this goes wildly against convention. Is there a reason why you don't want those variables to appear in the URL? It's likely there's a better way of doing what you're thinking and if you explain we can advise you as best as we can.
Have you properly defined the routes?
Can you show how they are?
You should have something like this for that to work: resource :comments
Also, in general /new works with a GET, and a POST is sent when creating...
The method is part of html_options, you need to separate the two hashes like so:
<%= link_to 'Add comment', {:controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id}, :method => :post %>