I'm having trouble routing a button so that it calls an action in a controller. To be more specific, I have this line
delete 'destroy/:id', to: 'users#admin_destroy'
in my route file, and this line
%= button_to 'Destroy', destroy_path(user), data: { confirm: 'Are you sure?' } %>
in a view.
The problem is that the page keeps treating destroy_path as a method, and I'm at a loss for what the proper syntax should be. What am I misunderstanding?
Additionally, I originally tried to get this work as link_to, but I learned that that should be reserved for GET requests. However, I don't like how button_to looks, and I'm wondering if there's a way to do this as a GET so that link_to can be used instead.
I'm not sure if this will completely solve your problem, but... You can custom define a path like so:
in config/routes.rb
delete 'destroy/:id', to: 'users#admin_destroy', as: :destroy
This will allow you to use destroy_path in your application, and have it route to your users controller with the admin_destroy function.
You can read more about it here
Related
I have a form_for tag that I want to use the index action in the search_results controller.
My tag looks like the following.
<%=form_for(:search_results, url:{controller: :search_results, action: :index}, html:{id:"quickSearch"}) do | f| %>
...form fields...
<% end %>
However when I try to enter something into my form and submit it, it is going to the create action instead of the index action? Normally this would make sense if I didn't specify the action, but I did. I also tried removing the action and making the tag use the get method but it never hits the controller and I need it to do that.
I'm simply trying to construct a link_to with method: :delete to call the destroy method in my users controller:
<%= link_to 'disable token', user_path(user), method: :delete, data: { confirm: 'Are you sure?'} %>
Which generates HTML that looks like:
<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/users/6">disable token</a>
My application.js file has:
//= require jquery
//= require jquery_ujs
And, in fact, i know the javascript is loaded and doing what it is supposed to, because it generates the 'Are you sure?` alert dialog.
However, when following the link I get the following:
No route matches [POST] "/users/9"
And indeed there is no such route, because my only routes are:
users_path GET /users(.:format) users#index
user_path DELETE /users/:id(.:format) users#destroy
The mystery (to me) is WHY is Rails doing a POST to this route in the first place? You'll note that the URL is correct ("/users/9") but that the HTTP verb is not: POST.
The parameters of the request are getting set:
{"_method"=>"delete",
"authenticity_token"=>"VcAVJF1/f9mwjNI4GPteRtDiyjKobnioF0hIQvF+3BVMzUnIoHymM2Z3w2sqSLJqJ11sZ/tIHt78aA9
}
Here you can see the _method key being set to delete as it should be, so why the routing error?!?
I'm stumped.
Edit: If i change my link_to call to include remote: true Rails routes it to the proper route! So, as a "fix" for this i've changed my controller to use UJS, but i don't like this because, well, as far as I can tell what i had before should work!
Here is the link_to call which sends a proper DELETE request as JS:
<%= link_to 'disable token', u, method: :delete, data: { confirm: 'Are you sure?'}, remote: true %>
Just try explicitly stating the controller action and method also (note #user.id is an example, you may be referencing it differently) with url_for:
<%= link_to 'disable token', url_for(action: :destroy, id: #user.id), method: :delete, data: { confirm: 'Are you sure?'} %>
My suspicion in this case is that there is some other javascript that is overriding the rails behavior.
I had the same issue (but with it getting a GET request, rather than POST). It turned out to a completely unrelated bit of JS that was too general in the selector, and was calling jquery's stopPropagation on all anchor tags surrounding a button, which was how my particular HTML was set up.
undefined local variable or method `nov_tisk_revij_index' for #<#<Class:0x007f361551b858>:0x007f3615519c60>
Hey guys, I'm setiing up a rails app and can't figure out what's wrong. I used rails g controller nov_tisk_revij index show to create my views and controller and now I can't link to it. If you can spot what I'm doing wrong, I'd be very grateful, don't want to remake this as is took a fair amount of work.
rake routes returns:
Prefix Verb URI Pattern Controller#Action
nov_tisk_revij_index GET /nov_tisk_revij/index(.:format) nov_tisk_revij#index
nov_tisk_revij_show GET /nov_tisk_revij/show(.:format) nov_tisk_revij#show
zic_index GET /zic/index(.:format) zic#index
root GET / zic#index
my html:
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index %>
routes:
get 'nov_tisk_revij/index'
get 'nov_tisk_revij/show'
get 'zic/index'
I tried adding resources :nov_tisk_revij into routes, and it still didn't work, just created additional routes for views I do not have.
controller:
class NovTiskRevijController < ApplicationController
def index
end
def show
end
end
I'm using rails4 with ruby 2.
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index_path %>
works
In a rails project, I use devise to manage my user. Now I want remove sign up from login-page and just user that allready exist, can register new user.
What the best way to do this?
To remove the sign up link from the login page, install the Devise Views and remove that link from the "shared/links" partial.
In your terminal, generate the Devise views:
$ cd path/to/app
$ rails generate devise:views users
In the folder views/devise that is created, edit the document shared/_links.html.erb to contain only the links you want to show.
To allow only existing users to create new users, you will want to at least place restrictions on the Devise::RegistrationsController controller actions new and create. This is a bit complex, and requires creating a custom Devise registrations controller.
To do that, create a file in your controller folder called registrations_controller.rb, and update your routes.rb file to point devise_for :users to that new controller. To restrict actions to signed in users only consider using the Devise helper "authenticate_user!". All of those suggestions together might look like this:
#controllers/registration_controllers.rb
class RegistrationsController < Devise::RegistrationsController
before_action :authenticate_user!, only: [:new, :create]
def new
super
end
def new
super
end
end
And your routes
#config/routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }
Ideally, in the views, Users who are not signed in should not even be given the option to try to create new users, so there should be some additional view logic, such as this:
<% if user_signed_in? %>
<%= link_to "New user", new_user_registration_path %>
<% end %>
To clean up your view, you might consider extracting conditional view logic like this into helpers.
With this setup, existing users will be able to create new users through the new_user_registration_path.
For more information, the Devise documentation is pretty useful (https://github.com/plataformatec/devise) as well as perusing other StackOverflow answers.
I have built a mailer that notifies users of a change to a page they are tracking that contains product info. I cannot seem to get the link to the page correct. In this case,
<%= link_to #page_update.page.product.name, pages_url(#page_update.page) %>
I end up with a link to http://www.mydomain.com/pages.123
When I am hoping for http://www.mydomain.com/pages/123
I can provide more info if necessary, but I imagine since I'm so new to this that there's something simple going on here (I hope).
You need to use page_url instead of pages_url
<%= link_to #page_update.page.product.name, page_url(#page_update.page) %>