Why I got a unknown action? - ruby-on-rails

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?'} %>

Related

recaptcha is not verifying -rails

Previously I was able to delete the user using an admin account, after I've added the recaptcha gem I was unable to do so.
I've tried the same method using recaptcha in adding a post and it works, but it doesn't work on delete.
users_controller.rb
def destroy
#user = User.find(params[:id])
if verify_recaptcha(model: #user) && #user.destroy
redirect_to #user
else
redirect_to posts_path
end
end
index.html.erb
<% #users.each do |user| %>
<td> <%= link_to "Delete", admin_destroy_user_path(user),
method: :delete, data: { confirm: "You sure?" },
:class => 'btn btn-danger' %>.
</td>
<% end %>
routes.rb
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
Can you guys please help me to identify what seems to be the problem? Based on my understanding i think there's something wrong with the
if verify_recaptcha
because when i did something like !if verify_recaptcha && #user.destroy the user will be deleted and will show error message: reCAPTCHA verification failed, please try again. So i assume something is wrong with verifying the captcha

ActiveRecord::RecordNotFound Couldn't find User without an ID

I made a simple voting system for users
votes_controller:
def create
#user = User.find(params[:id])
#vote = Vote.create(author_uid: current_user.uid)
#user.votes << #vote
redirect_to root_url
end
view file:
- #users.each do |user|
%tr
%td= user.id
%td= link_to user.username, user.url
%td
= link_to 'Like', {:controller => "votes", :action => "create" }, :method => "post"
But if I vote for user there is an error "Couldn't find User without an ID"
Hope you will help to solve this problem. If you need more information, please comment.
Thanks!
You are not passing in an id param in your link_to method.
That is why the params[:id] doesnt exist when you try to look it up on the server
Change
link_to 'Like', {:controller => "votes", :action => "create" }, :method => "post"
to
link_to 'Like', {:controller => "votes", :action => "create", :id => user.id }, :method => "post"
Also, you're not saving your user object after creating the votes object.

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

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