Routing error with confirmed (seemingly) existing route - ruby-on-rails

I'm copying what seems to be identical logic, but it doesn't work with one of my models.
In surveys, I have
View
<% #surveys.each do |survey| %>
...
<%= link_to 'Delete', survey, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
Controller
def destroy
#survey = Survey.find(params[:id])
#survey.destroy
respond_to do |format|
format.html { redirect to '/' }
format.json { head :no_content }
end
end
The delete function works fine.
Yet in question, I have
View
<% #questions.each do |question| %>
...
<%= link_to 'Delete', question, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
Controller
def destroy
#survey = Survey.find(params[:survey_id])
#question = Question.find(params[:id])
#question.destroy
respond_to do |format|
format.html { redirect to #survey }
format.json { head :no_content }
end
end
This gives me the error:
undefined method `question path' for #<#<Class:0x008ff2534....
When I remove the link_to, it works just fine retrieving question and its properties.
Changing the logic in my view to something more specific,
<%= link_to "Delete", :controller => "questions", :action => "destroy", :id => question.id %>
I get a more specific error.
No route matches {:controller=>"questions", :action=>"destroy", :id=>1}
Running rake routes, this confirms the path exists.
DELETE /surveys/:survey_id/questions/:id(.:format) questions#destroy
And here's my routes.rb entry:
devise_for :users do
resources :surveys do
resources :questions do
resources :responses
end
end
end
Computers don't make mistakes, so what did I do wrong?

questions are nested resources, so you should also pass survey to the path:
<%= link_to 'Delete', [#survey, question], :confirm => 'Are you sure?', :method => :delete %>
Assuming that you have set #survey variable.

Question is a nested resource under Survey so your route needs to reflect that. Note that in the rake routes output there's a :survey_id parameter as part of the route. It is required. Therefore your link needs to look like this:
<%= link_to "Delete", :controller => "questions", :action => "destroy", :survey_id => #survey.id, :id => question.id %>
Alternatively you can use Marek's path, namespacing the question resource:
<%= link_to 'Delete', [#survey, question], :confirm => 'Are you sure?', :method => :delete %>

Related

Removing ActiveStorage attachments in Rails 5

I have an app with products - each product has things like notes/FAQs/attachments.
I can delete the notes & FAQs successfully, but not the Active Storage attachments.
Could somebody please assist? I've tried using a separate method in the Products controller but that didn't work, so my current line is using an Uploads controller.
The current error I am getting is:
NameError in UploadsController#destroy
uninitialized constant Upload
Uploads controller:
class UploadsController < ApplicationController
load_and_authorize_resource :nested => :product
def destroy
#product = Product.find(params[:id])
#upload = #product.ActiveStorage::Attachment.find(params[:id])
#upload.purge
redirect_back(fallback_location: products_path)
end
end
Products view:
<% #product.uploads.each do |upload| %>
<% if can? :destroy, upload %>
<td><%= link_to t('X', :default => t("X")),
product_upload_path(#product, upload),
:method => :delete,
:data => { :confirm => t('.confirm', :default => 'Are you sure you want to delete this attachment?') },
:id =>'delete-faq' %></td>
<% end %>
<% if upload.variable? %>
<span><%= image_tag upload.variant(resize: "100x100"), class: "other-image" %></span>
<% elsif upload.previewable? %>
<span><%= link_to image_tag(upload.preview(resize: "100x100"), class: "other-image"), rails_blob_path(upload), target: "_blank" %></span>
<% else %>
<span><%= link_to image_tag("paper.jpg", size: "100x100", class: "other-image"), rails_blob_path(upload), target: "_blank" %></span>
<% end %>
<% end %>
routes:
resources :products do
resources :notes
resources :faqs
resources :uploads
end
I know this is not the most RESTful solution but I got it working, so am adding this as an answer in case it helps anybody else tearing their hair out.
I have checked the logs and it does remove both the attached image and the blob.
In my case, a product has_many uploads.
routes.rb:
resources :products do
resources :uploads do
match '/remove', to: 'products#remove', via: 'delete'
end
end
products controller (yes, I know, putting it here is not ideal)
def remove
#product = Product.find(params[:product_id])
#upload = #product.uploads.find(params[:upload_id])
#upload.purge
redirect_to product_path(#product), notice: "Upload was successfully removed."
end
products show view:
<% #product.uploads.each do |upload| %>
<%= link_to "X", product_upload_remove_path(#product, upload),
:method => :delete,
:data => { :confirm => t('.confirm', :default => 'Are you sure you want to delete this upload?') },
:id =>'delete-faq', :class => 'delete' %></td>
<% end %>
Hopefully this will help others or give a foundation for a more RESTful solution.

Delete method in ROR route error when using remote true

I'm trying to delete post with the remote: true command in rails. Everything is working fine when i'm not using ajax. But when i use the remote true command i get an routing error.
view:
<% #posts.each do |post| %>
<%= post.title %>
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
<% end %>
controller:
def index
#posts = Post.all
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
routes:
resources :posts
This is the error i get in the log.
Started DELETE "/posts" for 127.0.0.1 at 2014-10-11 12:46:33 +0200
ActionController::RoutingError (No route matches [DELETE] "/posts"):
Thanks in advance.
Update. I get this when i write rake routes.
DELETE /posts/:id(.:format) posts#destroy
To get this to working.
Change:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
redirect_to :back
end
To this:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js { head :no_content }
end
end
and:
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
to:
<%= link_to 'delete', post, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
Incase anyone is having any problems with this, I managed to solve it by adding :data => { :type => :json } to the link.
For example:
<%= link_to "Delete this post", #post, :method => :delete, :remote => true, :data => { :type => :json } %>
I imagine UJS is expecting HTML, however JSON is returned so it fails and posts again (I noticed two requests).
Hope this helps!

What is causing this error when I try to edit a RoR object that was created via scaffold?

What is causing this error when I click on the edit button for a note? The delete button works fine. I created the note object with a scaffold.
index.html.erb
<% #notes.each do |note| %>
<%= note.detail %>
<%= button_to 'Delete', note, :confirm => 'Are you sure?', :method => :delete %>
<%= button_to 'Edit', edit_note_path(note) %>
<% end %>
notes_controller.rb
before_filter :check_ownership, :except => [:new, :create, :index, :edit]
def edit
#note = Note.find(params[:id])
end
Error
ActiveRecord::RecordNotFound in NotesController#192
Couldn't find Note with ID=edit
../app/controllers/notes_controller.rb:248:in `check_ownership'
Parameters:
{"id"=>"edit"}
EDIT
config/routes.rb
map.resources :notes
All of the other routes for notes work fine.
Thanks for reading
Scaffold controller's 'edit' action, and defult routing support 'edit' as GET rather than POST request.
If you use link_to rather than button_to, things should work.
Alternatively, change the line containing button_to -
<%= button_to 'Edit', edit_note_path(note), :method => :get %>

button_to :action => 'destroy' looks for 'show'

This seems incredibly similar to a question I had answered just a few days ago, but the solution then isn't working now.
I'm building a rails app, and I am trying to have a button_to trigger a destroy in a different controller.
the code I have for the button is
<%= button_to "delete", :controller => :meals,
:action => 'destroy',
:recipe_id => recipe.id,
:method => :post >
when I click the delete button, i get a
'no matches for meals/3' which is the current meal_id.
the destroy in the meals controller looks like this
def destroy
#meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first
#meal.destroy
respond_to do |format|
format.html { redirect_to :controller => "user" , :action => "show" }
format.xml { head :ok }
end
end
it appears as though the button_to is completely ignoring the :action and requesting show which does not exist and shouldn't exist.
And how you part of routes.rb for that one looks like?
Because if you use map.resources then destroy has same path as show but :method => :delete(which is virtual verb implemented by form and _method=delete param).
Try this:
<%= button_to "delete", {:controller => :meals,
:action => 'destroy', :id => recipe.id }, :method => :delete %>
or if recipe is instance of Meal class then
<%= button_to "delete", #recipe, :method => :delete %>
Mind the curly brackets.
I know it is way too late for an answer but hope it may help somebody(using Rails 4).
<%= button_to "delete", meal_path(:id => recipe.id), :method => :delete %>

Why I got a unknown action?

I have a "manageUser" page, the route is like that:
map.manageUsers "manageUsers", :controller => "users", :action => "manageUsers"
and, it like a index of user, but provide a ban button for admin to ban the user, so, I have something like this:
<% #users.each do |user| %>
<td><%=h user.username %></td>
<td><%= link_to 'Ban !', user, :confirm => 'Are you sure?', :method => :ban %></td>
<%end%>
And the users controller have the method like this:
def ban
#user = User.find(params[:id])
#user.isBan = true
if #user.save
flash[:notice] = #user.username ' is successful banned.'
else
flash[:error] = #user.username ' may have greater power than you.'
end
redirect_to manageUsers_url
end
But when I click the link, it show me this address:
http://localhost:3000/users/46
With this error:
Unknown action
No action responded to 46. Actions:
What happen? thank you.
Because the :method in link_to helper is to define the HTTP method to request. But not the action in your controller.
You need use url_for system
<%= link_to 'Ban !', {:controller => 'users', :action => 'ban', :user_id => user.id}, {:confirm => 'Are you sure?'} %>

Resources