Render page with rails - ruby-on-rails

I'm trying to make "friendship" between model Programme and Student. I do it with has_many :through model Relationship. In Relationship controller I have create action, which should redirect another page after the friendship occurs.
So, in Student show view I have link_to request friendship. I thought I will be able to enter Student show page, but instead it redirects me from Student index when I try to enter Student show view to another page. So I actually can't enter Student show, it just redirects me to other page.
Why is that happening? I want have redirect from Student show view after I click on "request friendship" to other model page.
Student show.html.erb
<%= link_to "Request friendship with #{#student.name}",
{ :controller => "relationship", :action => "create",
:id => #student.name },
:confirm => "Send friend request to #{#student.name}?" %>
route.rb
match "/students/:id" => "relationships#create"
UPDATED
Relationship controller
def create
Relationship.request(#prog, #student)
flash[:notice] = "Friend request sent."
redirect_to prog_url
end
rake routes
relationships_create GET /relationships/create(.:format) relationships#create
relationships_destroy GET /relationships/destroy(.:format) relationships#destroy
/students/:id(.:format) relationships#create
relationships GET /relationships(.:format) relationships#index
POST /relationships(.:format) relationships#create
new_relationship GET /relationships/new(.:format) relationships#new
edit_relationship GET /relationships/:id/edit(.:format) relationships#edit
relationship GET /relationships/:id(.:format) relationships#show
PUT /relationships/:id(.:format) relationships#update
DELETE /relationships/:id(.:format) relationships#destroy
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
progs GET /progs(.:format) progs#index
POST /progs(.:format) progs#create
new_prog GET /progs/new(.:format) progs#new
edit_prog GET /progs/:id/edit(.:format) progs#edit
prog GET /progs/:id(.:format) progs#show
PUT /progs/:id(.:format) progs#update
DELETE /progs/:id(.:format) progs#destroy

Not sure I understand completely what #prog should be, but this is the way to go
def create
#student = Student.find_by_id(params[:id])
#prog = Programme.find_by_id(params[:prog]) #You need to fill this in
Relationship.request(#prog, #student)
flash[:notice] = "Friend request sent."
redirect_to #student # Or #prog, or whatever you'd like
end
You would probably need to pass the prog.id as well to the controller something like:
match "/students/:id/:prog" => "relationships#create"
and add
<%= link_to "Request friendship with #{#student.name}",
{ :controller => "relationship", :action => "create",
:id => #student.name, :prog => #student.current_programme },
# I don't know how you differ programmes, you'd have to work this one out
:confirm => "Send friend request to #{#student.name}?" %>

Related

Pass value from view to Controller without querystring

I am having index method to list all students. Now I am adding search in the index with one text_field_tag. My function is working but the search value is showing as query string in the url. I don't want this.
My view code:
<p>
<%= form_tag students_path, :method => 'get' do %>
Find by name: <%= text_field_tag :search %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</p>
My controller code is
def index
searchString = params[:search]
if searchString
#students = Student.where("LastName like ?", "%#{searchString}%").paginate(:page => params[:page])
else
#students = Student.paginate(:page => params[:page])
end
end
My current url look like below:
http://localhost:3000/students?utf8=%E2%9C%93&search=A
I just need like /students. Please suggest the best practice
You need to use POST request instead of GET. In order to accomplish that, change your form_tag from:
<%= form_tag students_path, :method => 'get' do %>
to
<%= form_tag students_path, :method => 'post' do %>
What could possibly go wrong...
As #carlosramireziii mentioned, answering this question, requires a bit more work (and he is absolutely right!), so - there is update
1. Custom routes
If you are configuring your routes on your own, one of ways proceeding with this is to add a route, that will properly accept your requests. Take a look at config/routes.rb:
Rails.application.routes.draw do
match '/students', controller: :students, action: :index, via: [:get, :post]
# other definitions
end
Definition like this will create following route for you:
students GET|POST /students(.:format) students#index
This will make your new POST request nicely fall into students#index, generating expected result. However, if you are defining your routes via resources it's not that easy change. This leads us to...
2. Modifying standard resources routes
If routes for students are defined as follows:
Rails.application.routes.draw do
resources :students
# other definitions
end
the following routes are registered:
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PATCH /students/:id(.:format) students#update
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
This indicates, the POST request falls into students#create. In order to make it work, you need to change the create action, to something like:
class StudentsController < ApplicationController
# code omitted
def create
if params.include?('search')
#students = Student.where("LastName like ?", "%#{searchString}%").paginate(:page => params[:page])
render :index
else
# your current code from create action
end
end
end
While this will work, it is not recommended to do it this way. The create action does two things now, which is considered a bad pattern (or bad smell at least), and it is error prone.
So, what can we do?
Making search the right way
1. Custom search route
Define custom collection route:
Rails.application.routes.draw do
resources :students do
collection do
post :search
end
end
# other definitions
end
This registers following routes:
search_students POST /students/search(.:format) students#search
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PATCH /students/:id(.:format) students#update
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
You need to change your form_tag as follows:
<%= form_tag search_students_path, :method => 'post' do %>
And add search action to StudentsController:
class StudentsController < ApplicationController
# code omitted
def search
#students = Student.where("LastName like ?", "%#{searchString}%").paginate(:page => params[:page])
# If you don't want to create separate template for `search`,
# you can try to reuse your `index` template with
# render :index
end
end
2. Use students#index with params passed in URL
This approach is more REST-y, and it makes it available to pass the whole URL to someone else, so the another person is able to see exactly the same result of filtering, which is impossible with params "hidden" in POST request.
I hope I've covered all possibilities. If you have any questions - I'm more than happy to answer!
Good luck!

Destroy Action Acting Unusual

The application has: Clients, which has and belongs to many ActionItems, which has and belongs to many Clients. A user, chooses a client (a client they have as a customer), and adds action items (to do's) to that client. -- Like: User creates => "Email client about X topic," for client: Crayola LLC.
I've been instructed to nest resources like so, in routes:
resources :clients do
resources :action_items
end
So that I can get a URL like:
-http://localhost:3000/clients/42/action_items/11
To display the action items for a specific client.
However - deleting action items for that client, doesn't work. It's been trying to redirect me to the destroy action, on which I get:
undefined local variable or method `clients_action_items' for # <ActionItemsController:0x007febd0edf800>
Prior to this, the delete link, which uses the destroy action, was attempting to redirect me to the show page, on which I was getting:
No route matches [POST] "/clients/42/action_items/1"
Then I added: post '/clients/:client_id/action_items/:id' => 'action_items#destroy' to the routes file. (and now I get the undefined local variable or method clients_action_items' error.
Routes:
Rails.application.routes.draw do
get 'users/index'
get 'users/new'
get 'users/edit'
get 'users/delete'
get 'users/create'
patch 'users/create'
patch 'users/update'
get 'clients/index'
get 'clients/new'
get 'clients/edit'
get 'clients/delete' => 'clients#delete'
get 'clients/create'
patch 'clients/create'
patch 'clients/update'
post '/clients/:client_id/action_items/:id' => 'action_items#destroy'
get 'login', :to => "access#index"
resources :action_items
#/clients/13/action_items
resources :clients do
resources :action_items
end
#get 'home/index'
#get 'home/edit'
#
#get 'home/delete'
#get 'home/show'
root 'home#index'
#define, below **, is the URL we named categories/index. It is now localhost:3000/define
#get 'index' => 'questions#index'
#get 'questions/edit'
#get 'new' => 'questions#new'
#get 'questions/delete'
#post 'questions/destroy'
#get 'questions/show'
#post 'create' => 'questions#create'
match ':controller(/:action(/:id))', :via => [:get, :post]
# end
end
Action Items Controller:
class ActionItemsController < ApplicationController
# before_action :get_owner
def index
#action_items = ActionItem.all
#client = Client.find(params[:client_id])
end
def new
#action_items = ActionItem.new
# #action_items_client = #client.action_items.new
#client = Client.find(params[:client_id])
end
def create
# #action_item = ActionItem.new(action_items_params)
# if #action_item.save
# redirect_to(:action => 'show', :id => #action_item.id)
# #renders client individual page
# else
# redirect_to(:action => 'new')
# end
#client = Client.find(params[:client_id])
#action_item_client = #client.action_items.new(action_items_params)
if #action_item_client.save
redirect_to(:action => 'show', :id => #action_item_client.id, :client_id => #client.id)
else
redirect_to(:action => 'new')
end
end
def edit
#action_item = ActionItem.find(params[:id])
end
def update
#action_item = ActionItem.find(params[:id])
if #action_item.update_attributes(action_items_params)
redirect_to(:controller => 'action_items', :action => 'show', :id => #action_item.id)
flash[:notice] = "Updated"
else
render 'new'
end
end
def show
#client = Client.find(params[:id])
#action_item = ActionItem.find(params[:action_item_id])
end
def action_clients
#action_clients = ActionItem.Client.new
end
def delete
#action_item = #client.action_items.find(params[:client_id])
end
def destroy
# #action_items = #client.action_items.find(params[:id]).destroy
# redirect_to(:controller => 'action_items', :action => 'index')
item = clients_action_items.find(params[:client_id])
item.destroy
if params[:client_id]
redirect_to clients_action_items_path(params[:client_id])
else
redirect_to clients_action_items_path
end
end
private
def action_items_params
params.require(:action_item).permit(:purpose, :correspondence_method, :know_person, :contact_name_answer, :additional_notes)
end
# private
# def get_owner
# if params[:client_id].present?
# #owner = user.clients.find(params[:client_id])
# else
# #owner = user
# end
# end
end
Index view from which I am deleting an action item:
<%= link_to('New Action Item', :controller => 'action_items', :action => 'new') %></br>
<ol><% #action_items.each do |list| %>
<li>
Action Item for <%= #client.name %> is: <strong><%= list.correspondence_method %></strong> Client, about:
<strong><%= list.purpose %> </strong></li>
And you created some additional notes: <strong><%= list.additional_notes %></strong></br></br>
-- Crud Actions -- </br>
<%= link_to('New Action Item', :controller => 'action_items', :action => 'new') %></br>
<%= link_to('Edit Action Item', :controller => 'action_items', :action => 'edit', :id => list.id) %></br>
<%= link_to('Show Individual', :controller => 'action_items', :action => 'show', :id => list.id) %></br>
<%= button_to('Delete Action Item', :controller => 'action_items', :action => 'destroy', :id => list.id) %></br>
<h2> new delete </h2>
</br></br>
<% end %></ol>
I have created the foreign key columns in a migration file with a join table called: action_items_clients:
class CreateActionItemsClients < ActiveRecord::Migration
def change
create_table :action_items_clients, :id => false do |t|
t.integer :action_item_id
t.integer :client_id
end
end
end
-New to rails. Please excuse dirty code. What is wrong here? Why the destroy link issues? Why was the destroy link redirecting to show before, and giving me both routing and ID errors?
Thanks for your time.
*** EDIT ****
Rake routes output:
Prefix Verb URI Pattern Controller#Action
users_index GET /users/index(.:format) users#index
users_new GET /users/new(.:format) users#new
users_edit GET /users/edit(.:format) users#edit
users_delete GET /users/delete(.:format) users#delete
users_create GET /users/create(.:format) users#create
PATCH /users/create(.:format) users#create
users_update PATCH /users/update(.:format) users#update
clients_index GET /clients/index(.:format) clients#index
clients_new GET /clients/new(.:format) clients#new
clients_edit GET /clients/edit(.:format) clients#edit
clients_delete GET /clients/delete(.:format) clients#delete
clients_create GET /clients/create(.:format) clients#create
PATCH /clients/create(.:format) clients#create
clients_update PATCH /clients/update(.:format) clients#update
DELETE /clients/:client_id/action_items/:id(.:format) action_items#destroy
login GET /login(.:format) access#index
action_items GET /action_items(.:format) action_items#index
POST /action_items(.:format) action_items#create
new_action_item GET /action_items/new(.:format) action_items#new
edit_action_item GET /action_items/:id/edit(.:format) action_items#edit
action_item GET /action_items/:id(.:format) action_items#show
PATCH /action_items/:id(.:format) action_items#update
PUT /action_items/:id(.:format) action_items#update
DELETE /action_items/:id(.:format) action_items#destroy
client_action_items GET /clients/:client_id/action_items(.:format) action_items#index
POST /clients/:client_id/action_items(.:format) action_items#create
new_client_action_item GET /clients/:client_id/action_items/new(.:format) action_items#new
edit_client_action_item GET /clients/:client_id/action_items/:id/edit(.:format) action_items#edit
client_action_item GET /clients/:client_id/action_items/:id(.:format) action_items#show
PATCH /clients/:client_id/action_items/:id(.:format) action_items#update
PUT /clients/:client_id/action_items/:id(.:format) action_items#update
DELETE /clients/:client_id/action_items/:id(.:format) action_items#destroy
clients GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
edit_client GET /clients/:id/edit(.:format) clients#edit
client GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy
root GET / home#index
GET|POST /:controller(/:action(/:id))(.:format) :controller#:action
It seems you are trying to destroy an object using a POST request.
No route matches [POST] "/clients/42/action_items/1"
Did you try a DELETE request?
delete '/clients/:client_id/action_items/:id' => 'action_items#destroy'
You can read further about CRUD (Create, Read, Update, Delete) operations here
Also, can you run rake:routes and post the output here? That will help figure out what routes are actually being generated.
EDIT
So, as you can see from the output of rake:routes there is a LOT of duplication. You basically have three models, User, Client and ActionItems with basic CRUD and one login.
Rake file:
Rails.application.routes.draw do
get 'login', :to => "access#index"
resources :users
resources :action_items
resources :clients do
member do
get :action_items
end
end
root 'home#index'
end
Client and ActionItems have a many-to-many relation. If it was a one-to-many ie many ActionItems belong to only one Client then it would have been better to nest the resources.
To show all action items of a client you only need one extra method in client controller.
Clients Controller:
def show
#client = Client.find(params[:id])
end
def action_items
#list_action_items = #client.action_items
end
Action Items Controller:
#Will list all action_items irrespective of which clients they belong to
def index
#action_items = ActionItem.all
end
#For creating a new action item,
def new
#action_item = ActionItem.new
#You can render a form with dropdown here with Client.all to assign the new action_item to a client
end
def create
#action_item = ActionItem.new(action_items_params)
if #action_item.save
redirect_to(:action => 'show', :id => #action_item.id)
#renders client individual page
else
redirect_to(:action => 'new')
end
end
def edit
#action_item = ActionItem.find(params[:id])
end
def update
if #action_item.update_attributes(action_items_params)
flash[:notice] = "Updated"
redirect_to(:controller => 'action_items', :action => 'show', :id => #action_item.id)
else
render 'new'
end
end
def show
#action_item = ActionItem.find(params[:id])
end
def destroy
#action_item.destroy
flash[:notice] = "Action Item has been deleted."
redirect_to action_items_path
end
I have tried to simplify the structure here. If you want to perform tasks like deleting all action items of a client from the index/list page of all clients, you can define a method destroy_many in the ActionItems controller which takes client_id as argument, queries all action items and deletes them.
You don't need separate ClientActionItem controller/routes.
Also, if you want to continue with nested routes,
try
<%= button_to('Delete Action Item', client_action_item_path(#client.id, list.id), method: :delete) %></br>
The nested route requires two arguments. The first is client.id and second the action_item id.
This should work, but un-tested. I am not sure of your intentions, but I would use a link_to with a class of button personally.
<%= button_to('Delete Action Item', client_action_item_path(#client, list), method: :delete) %></br>

Route to method always uses first listed despite different name?

I'm new to rails and trying to create up / down vote buttons (implemented as text links for now for clarity). However no matter which link is clicked it calls the action of the first link despite the different names.
I've read over and over the docs and answers on here and wrestled with it all day but still can't understand why rails can't see the difference, any help greatly appreciated.
Routes
Futurebot::Application.routes.draw do
resources :posts do
resources :comments
end
resources :posts do
member do
post 'delete'
post 'upVote'
post 'downVote'
end
end
match ':posts/:id/:upVote', :controller => 'posts', :action => 'upVote'
match ':posts/:id/:downVote', :controller => 'posts', :action => 'downVote'
If I remove the resources :posts block it can't find the route but it seems like the match statements should work (that's what the url looks like)
view
<%= link_to "up: ", :action => 'upVote', :id => post.id %>
<%= link_to "down: ", :action => 'downVote', :id => post.id %>
controller
def upVote
#post = Post.find(params[:id])
if #post.increment!(:score)
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
def downVote
#post = Post.find(params[:id])
if #post.decrement!(:score)
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
rake routes: with the 2 new routes commented out (so just the block is there)
up_vote_post POST /posts/:id/up_vote(.:format) posts#up_vo
te
down_vote_post POST /posts/:id/down_vote(.:format) posts#down_
vote
GET /posts(.:format) posts#index
POST /posts(.:format) posts#creat
e
GET /posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#updat
e
DELETE /posts/:id(.:format) posts#destr
oy
root /
posts#index
Rake routes with two routes in routes.rb
delete_post POST /posts/:id/delete(.:format) posts#delet
e
up_vote_post POST /posts/:id/up_vote(.:format) posts#up_vo
te
down_vote_post POST /posts/:id/down_vote(.:format) posts#down_
vote
GET /posts(.:format) posts#index
POST /posts(.:format) posts#creat
e
GET /posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#updat
e
DELETE /posts/:id(.:format) posts#destr
oy
/:post/up_vote/:id(.:format) post#up_vot
e
/:post/down_vote/:id(.:format) post#down_v
ote
root /
posts#index
The added routes
match ':post/up_vote/:id' => "post#up_vote"
match ':post/down_vote/:id' => "post#down_vote"
UPDATE
strangely if I change the route around to:
match ':post/:id/up_vote/' => "post#up_vote"
match ':post/:id/down_vote/' => "post#down_vote"
..as that looks like the link then the error is
uninitialized constant PostController
I've tried using both post and posts based on the solution to that in another question
It seems to me that
resources :posts do
member do
post 'delete'
post 'up_vote'
post 'down_vote'
end
end
should give you a routes output similar to this
POST /posts/:id/delete(.:format)
/posts/:id/up_vote(.:format)
/posts/:id/down_vote(.:format)
And they should map out to the PostsController and its respective actions delete, up_vote, down_vote.
Is there any reason why you have those two match routes at the end? They pretty much look like wildcards to me and I don't think you need them.
What is really happening is that anything that matches the following
something/another/path
will get mapped to those routes, specifically to the first one and it will be split in the params like
params[:posts] => something
params[:id] => another
params[:upVote]=> path
That happens because you are using the colon character which allows you to specify dynamic routes. For example something like this
match 'hello/world/:name' => "hello#say"
would get mapped out to a HelloController and action say. Inside the action you would then have params[:name] that would be equal to whatever is under name.
So hello/world/leo would have params[:name] equal to leo
For more information take a look at this: Rails Routes: Dynamic Segments
NOTE Also, try not to use camel case for method names in Ruby :D
UPDATE You need a :method => "post" on your link_to in order for it to send a post request

Rails friendship canceling, decline

I am trying to build a friendship system sorta following this link: How to Implement a Friendship Model in Rails 3 for a Social Networking Application?. However lack a bit. I was able to create a relationship however i am not to sure on how to perform the following actions: cancel, decline, accept.
So lets say i try to cancel the relationship i do the following on the pending, to call the actions i do the follow:
<% #customer.pending_friends.each do |pf| %>
<%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :post %><br />
<% end %>
Here the controller of cancel
def cancel
#customer = current_customer
#friend = Customer.find(params[:friend_id])
if #customer.pending_friends.include?(#friend)
Friendship.breakup(#customer, #friend)
flash[:notice] = "Friendship Canceled"
else
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
and here my breakup function
# Delete a friendship or cancel a pending request.
def self.breakup(customer, friend)
transaction do
destroy(find_by_customer_id_and_friend_id(customer, friend))
destroy(find_by_customer_id_and_friend_id(friend, customer))
end
end
I am however getting a no route errors when clicking on the cancel links. What i am doing wrong??
Here on the request
route.rb
resources :friendships do
collection do
get 'cancel'
get 'decline'
end
end
resources :friendships
rake routes
cancel_friendships GET /friendships/cancel(.:format) friendships#cancel
decline_friendships GET /friendships/decline(.:format) friendships#decline
GET /friendships(.:format) friendships#index
POST /friendships(.:format) friendships#create
GET /friendships/new(.:format) friendships#new
GET /friendships/:id/edit(.:format) friendships#edit
GET /friendships/:id(.:format) friendships#show
PUT /friendships/:id(.:format) friendships#update
DELETE /friendships/:id(.:format) friendships#destroy
/********************************************************/
friendships GET /friendships(.:format) friendships#index
POST /friendships(.:format) friendships#create
new_friendship GET /friendships/new(.:format) friendships#new
edit_friendship GET /friendships/:id/edit(.:format) friendships#edit
friendship GET /friendships/:id(.:format) friendships#show
PUT /friendships/:id(.:format) friendships#update
DELETE /friendships/:id(.:format) friendships#destroy
The problem is that in your routes you have:
get 'cancel'
but your cancel-link is doing a post request, not a get:
<%= link_to ..., ..., :method => :post %>
Personally I think it should be a delete request.
In your routes:
delete 'cancel'
In your view:
<%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :delete %>
Your code may have other problems, but this is one thing you have to fix.

why is my remote_form_tag with :action defined not posting the action?

This is what I've used with remote_form_tag:
<% form_remote_tag(:url => {:controller => '/companies', :action => 'update'},
:update => 'tags') do %>
<%= text_field :company, :tag_list %>
<%= submit_tag 'Save' %>
<% end %>
This is in a Company.view, where Company is a model that is acts_as_taggable_on enabled.
My expectation is that, via ajax, a post is made to companies/10/update
But, instead, what is posted is:
http://localhost:3000/companies/10
and the response is:
No action responded to 10. Actions: create, destroy, edit, email_this_week, index, new, show, and update
This is the update method in CompaniesController:
def update
#company = Company.find(params[:id])
if request.xhr?
# add the given tag to the company
#company.tags << params[:company][:taglist]
#company.save
render :partial => 'tags'
else
if #company.update_attributes(params[:company])
flash[:notice] = "Successfully updated company."
redirect_to #company
else
render :action => 'edit'
end
end
end
Help...?
DELETE /companies/:company_id/contacts/:id(.:forma
{:controller=>"contacts", :action=>"destroy"}
companies GET /companies(.:format)
{:controller=>"companies", :action=>"index"}
POST /companies(.:format)
{:controller=>"companies", :action=>"create"}
new_company GET /companies/new(.:format)
{:controller=>"companies", :action=>"new"}
edit_company GET /companies/:id/edit(.:format)
{:controller=>"companies", :action=>"edit"}
company GET /companies/:id(.:format)
{:controller=>"companies", :action=>"show"}
PUT /companies/:id(.:format)
{:controller=>"companies", :action=>"update"}
DELETE /companies/:id(.:format)
{:controller=>"companies", :action=>"destroy"}
When you update a resource like Company with ID 10, Rails will use the RESTful route:
PUT /companies/10
The PUT method is taken into account when routing your request. Taken from your routes:
PUT /companies/:id(.:format)
{:controller=>"companies", :action=>"update"}
This is correct behaviour for Rails. Just implement the update method in your CompaniesController.
If you require more info on RESTful routes in Rails, check up on this document: http://guides.rubyonrails.org/routing.html

Resources