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.
Related
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')
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 %>
So I'm trying to post to a 'toggle_live' action in my PollsController but am getting a 'No routes matching" error. Advice here would be really awesome! Thanks!
It's configured in routes like so:
post "polls/toggle_live"
Here's the action in the PollsController:
def toggle_live
#poll = Poll.find(params[:id])
respond_to do |format|
format.js {#poll.toggle_live}
end
end
Here's the form that initiates the request:
= link_to "Start",:method => :post, :action => :toggle_live, :remote=>true, :class=> 'btn btn-success btn-small start-poll'
You should use a named route, it would be easier:
routes.rb:
post 'polls/toggle_live' => 'polls#toggle_live', :as => 'toggle_live'
In the view:
= link_to "Start", toggle_live_path, {:method => :post, :remote=>true}, {:class=> 'btn btn-success btn-small start-poll'}
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.
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?'} %>