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
Related
I am getting the following error
No route matches {:action=>"index",
:controller=>"module/admin/orders"}
when using following:
<%= form_tag({ :action => :index}, { :method => "get" }) do %>
My routes have this path:
admin_orders GET /new/admin/orders(.:format) module/admin/orders#index
I tried to run this code in rails console and it works without errors:
r.url_for :controller => 'module/admin/orders', :action=> :index, :id=>42
=> "http://domain.com/new/admin/orders?id=42"
r.url_for :controller => 'module/admin/orders', :action=> :index
=> "http://domain.com/new/admin/orders"
My application is using Spree and this controller inherits Spree::Admin::ResourceController class
User proper url helpers ass follows
<%= form_tag(admin_orders_path, method: :get) %>
The answer is to put routes configuration in the same engine controller is using (For example Spree in this case):
Spree::Core::Engine.add_routes do
scope 'module/' do
namespace :admin, as: 'module_admin' do
resources :orders, controller: '/module/admin/orders',
only: [:edit, :index, :update]
end
end
and use:
<%= form_tag(module_admin_orders_path, method: :get) %>
or use url helpers with engine prefix:
<%= form_tag(main_app.admin_orders_path, method: :get) %>
I'm developing a rails app with a landingpage. On the landingpage, the user can sign up for the app. For login, there is an extra view with an extra controller.
It looks like this:
views/landinpage/index.html --> sign up form
views/login/index.html --> login form
but I only want to have one controller
controllers/login_controller --> create new user from sign up form & check login data
so I have to get a connection between the landingpage view and the login_controller.
This is my attempt:
<%= form_for #login, :url => { :controller => "login_controller", :action => "create" }, :html => {:method => :post} do |f| %>
but it throws a route error:
No route matches {:controller=>"login_controller", :action=>"create"}
I already defined login resources in routes.rb, but it seems that the problem is elsewhere?
resources :logins
any ideas?
try this
class LoginsController < ApplicationController
def new
...
end
def create
...
end
...
end
in your route.rb file write
match '/login/create' => 'logins#create', :as => :create_login
or
resources :logins
in your console - write - rake routes and check your routes
then
<%= form_for #login, :url => create_login_path(#login) do |f| %>
I think your code should look like this:
<%= form_for #login, :url => { :controller => "login", :action => "create" }, :html => {:method => :post} do |f| %>
can't test this right now, but I believe the _controller part is not required.
Update:
Another thing that I'm using a lot and that works:
<%= form_for #login, :url => create_login_path(#login), :html => {:method => :post} do |f| %>
You may have to fix the create_login_path part to match your application's routes but that's how I usually define these views.
Try this
class LoginsController < ApplicationController
def new
...
end
def create
...
end
...
end
in your routes.rb file
resources :logins do
collection do
post :create
end
end
and in your views
<%= form_for #login, :url => create_login_path(#login) do |f| %>>
you can see the html form action part, you can see!
your config/routes has
resources :posts
namespace :admin do
resources :posts
end
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 a button_to tag in my show.html.erb file.
<%= link_to 'Click HERE to open file', #user.image.url %><br/><br/><br/>
<%= label_tag(:q, "Parse CSV File:") %><br/>
<%= button_to 'Parse CSV', {:controller => "users_controller", :action => "process" } %>
<% end %>
Then I have this added to my users_controller.rb file
# GET /users/1/process
def process
puts 'To be Implemented'
end
Im getting an error in the routing file
No route matches [POST] "/assets"
This is how my routing file looks:
resources :users
resources :listings
What should I change. Im a bit confused, woould really appreciate some help.
Please correct your route and define like this
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
Then in route file
resources :users do
collection do
get: process
end
end
It will sure work
1) In view, use the controller name like 'users', not the 'users_controller' .
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
2) rails define few routes by default , but for other you need to define yourself .
Declare the routes like :
resources :users do
:member => {
:process => :get
}
end
Hope that help .
I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'