A Destroy Button in the Edit and View for Rails - ruby-on-rails

I have an app in Rails with a form to edit a register, and I would like to have a destroy button here.
I currently have the button with code that I think should work.
this is how it looks in the view and this is the code
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
register_path(register),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-mini btn-danger' %>
But, when I try to delete it says "ActiveRecord::RecordNotFound in RegistersController#edit"

try this, write in your RegistersController#destroy actin
def destroy
#register = Register.find(params[:id])
if #register.present?
#register.destroy
redirect_to .....
else
redirect_to .... or render 'xyz'
end
end
i thing, error 'ActiveRecord::RecordNotFound' comes because record is not present in the database.

Related

Error with a link action to delete a record

I am creating a link to delete a record from the database, the link calls a destroy method that is responsible for doing the deletion.
Link:
<%= link_to "Eliminar el articulo", options = {:action => destroy, :id => #article.id}, html_options = {:method => :delete, :data => { :confirm => '¿Estas seguro?' }, :class => 'btn btn-danger'} %>
Routes.rb:
delete 'articles/:id' => 'articles#destroy'
Controller:
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to root_path
end
To be more concise, I would like to say that I can locate the problem in the link since what fails is :action => destroy but if I remove the link, what it does is go to the same page instead of delete the record.
The error: undefined local variable or method `destroy' for #ActionView::Base:0x000000000395d0
The text is translated using Google Translate. To see the original question click here
The error: undefined local variable or method `destroy' for #ActionView::Base:0x000000000395d0
destroy variable is not defined in the view... you should use a symbol instead (:action => :destroy)
<%= link_to "Eliminar el articulo", options = {:action => :destroy, :id => #article.id}, html_options = {:method => :delete, :data => { :confirm => '¿Estas seguro?' }, :class => 'btn btn-danger'} %>
However, I suggest to use the route helpers:
delete 'articles/:id' => 'articles#destroy', as: :article
# or user rails resources
# resources :articles, only: [:destroy]
<%= link_to "Eliminar el articulo", article_path(#article), method: :delete, data: { confirm: 'Are you sure?' } %>
Try this
= form_tag(article_path(#article), method: :delete, remote: true) do
= button_tag(type: 'submit', class: 'btn btn-danger') do
= content_tag(:strong, 'Delete')

Routing error with confirmed (seemingly) existing route

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 %>

wrong number of arguments (4 for 1..3)

i'm struggling to understand why this is happen with destroy method since everything on controller and routes is ok!
if someone passed through this way please could give me a hint?
Routes
resources :users, :as => "" do
resources :sections, :only => [:new, :create, :destroy, :index]
end
Controller
def destroy
#section = Section.find(params[:id])
#section.destroy
redirect_to sections_url
flash[:notice] = "Section deleted"
end
View
<%= render :partial => "section", :collection => #sections %>
Partial
<%= link_to section.name, section_path(current_user, section) %>
<%= button_to 'Remove', current_user, section, :data => { :confirm => 'Confirm?' }, :class=> "buttom", method: :delete %>
That error means that some function takes 1 to 3 arguments, but you gave to it 4 arguments.
Please see the row number in the error and look up the function, then open documentation and look up how to use that function. Often functions works differently as instance methods and class methods.
The problem seems to be this method call:
button_to 'Remove', current_user, section, :data => { :confirm => 'Confirm?' }, :class=> "buttom", method: :delete
The pair current_user and section has to been passed as an array:
button_to 'Remove', [current_user, section], confirm: 'Confirm?', class: "buttom", method: :delete
Your button_to helper arguments are wrong.
Try this:
<%= button_to 'Remove', {:action => :destroy, :user => current_user, :id => section}, {:data => { :confirm => 'Confirm?' }, :class=> "buttom", method: :delete} %>
codeit, Stefan did what you guys said but did not work, so i tried the path instead and worked!
<%= button_to 'Remove', section_path(current_user, section), :data => { :confirm => 'Confirm?' }, :class=> "button", method: :delete %>

how to call action from link_to

i have index page users_controller:
def index
#companies = Company.where(:is_confirmed => "f")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #companies }
end
end
and I want at the touch of a button, the company changed the status to confirmed
def confirm
company = Company.find(params[:id])
company.is_confirmed = "t"
company.save
redirect_to users_path
end
button which should call confirmation
= link_to '<i class="icon-ok icon-white"></i> '.html_safe + t('Confirm'), users_path, confirm: t('Are you sure'), :controller => "users", :action => "confirm", :class => 'btn btn-small btn-success'
please tell me how to fix or tell me where you can see a working version
= link_to confirm_company_path(company), confirm: 'Are you sure', method: :post do
%i{class: "icon-ok icon-white"}
= t('Confirm')
In routes.rb
post '/company/:id/confirm' => "users#confirm", as: :confirm_company
1) Don't use GET request when you change object, use POST instead.
2) Move confirm logic to company model and confirm action to companies controller
You have to choose between the controller/action/id argument and the RESTful route, check the rails api. You probably want this :
= link_to '<i class="icon-ok icon-white"></i> '.html_safe + t('Confirm'), :controller => "users", :action => "confirm", :id => #companies, method: :post, confirm: t('Are you sure'), :class => 'btn btn-small btn-success'
or
= link_to '<i class="icon-ok icon-white"></i> '.html_safe + t('Confirm'), confirm_users_path(#companies), method: :post, confirm: t('Are you sure'), :class => 'btn btn-small btn-success'
implying your route look like this (RESTful):
resources :users do
post 'confirm'
end
Yuri Barbashov is right, a post make a lot more sense here.

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 %>

Resources