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'}
Related
I’m using Rails 4.2.3. I have this in my controller file, “./app/controllers/users_controller.rb” …
def edit
#user = User.find(session["user_id"])
render 'edit'
end
def update
#user = User.find(session["user_id"])
if #user.update_attributes(user_params)
flash[:success] = "Profile updated"
end
render 'edit'
end
And I have this in my “./app/views/users/edit.html.erb” file
<%= form_for(#user) do |f| %>
…
<%= button_to "Save", { :action => "update" }, :method => :post, :class => 'button' %>
But when I visit my url “http://localhost:3000/users/edit”, I get this error
No route matches {:action=>"update", :controller=>"users"}
This is what I have in routes/config.rb so I’m not sure why its falling apart …
get "users/edit" => "users#edit"
resources :users
I believe you need to use PUT/PATCH method for the update action, while POST is used for the create action as per the Rails documentation:
<%= button_to "Save", { :controller => "users", :id => #user.id }, :method => :put, :class => 'button' %>
Also, rake routes is a really useful command for debugging routing issues that dumps all defined routes.
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.
I am trying to follow the tutorial of making a twitter clone in ruby on rails (http://www.youtube.com/watch?v=oXr1jAsBlPI&feature=relmfu). Unfortunately for me he is doing it in Rails 2.x.x and I am having some trouble keeping up. I am currently 41:34 when he is defining the paths in routes.rb.
I am getting this problem when I enter my 'show' page:
"Routing Error No route matches {:action=>"show",
:controller=>"toggle_follow"} Try running rake routes for more
information on available routes."
This is what I have in the relevant files:
routes.rb
match '/:username', :controller => 'home', :action => 'show'
match '/:username/toggle_follow', :controller => 'home', :action => 'toggle_follow'
show.html.rb
<% if current_user.is_friend? #user %>
<%= submit_tag "Following", :class => "button" %>
<% else %>
<%= submit_tag "Stop following", :class => "button" %>
<% end %>
home_controller.rb
def show
#user = User.find_by_username(params[:username])
#flits = #user.all_flits
end
def toggle_follow
#user = User.find_by_username(params[:username])
if current_user.is_friend? #user
flash[:notice] = "You are no longer following ##{#user.username}"
current_user.remove_friend(#user)
else
current_user.add_friend(#user)
flash[:notice] = "You are following ##{#user.username}"
end
redirect_to user_flits_path(#user.username)
end
...
Thanks in advance
Ok I solved it...
in routes you have to enter:
routes.db
match '/:username', :to => 'home#show', :as => 'user_flits'
match '/:username/toggle_follow', :to => 'home#toggle_follow', :as => 'toggle_follow'
My setup: Rails 3.0.9, Ruby 1.9.2
I added a custom action to a nested resource task.
routes.rb
resources :tasks
resources :projects do
resources :tasks, :constraints => { :protocol => "http" } do
put :cancel, :on => :member
end
end
rake routes shows
cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}
In my controller,
tasks_controller.rb
def cancel
#task = Task.find(params[:id])
respond_to do |format|
if #task.cancel
format.html { redirect_to(#task, :notice => 'Task was successfully canceled.') }
else
format.html { render :action => "edit" }
end
end
end
I need to define a form to perform the action, here's what I have currently
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel' } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
It throws an error
No route matches {:action=>"cancel", :controller=>"tasks"}
I also tried adding :method => "put" with the same error
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel', :method => "put" } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
Anyone knows the correct form_format syntax to accomplish this?
In case anyone wants the answer, here's what I had to do
<%= form_for [#project, #task], :url => cancel_project_task_path(#project, #task) do |f| %>
It took me way too long to figure this out, hopefully this helps the next unsuspecting developer.
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 %>