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.
Related
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>
When submitting my form (_reply_form.html.erb) I get this error:
Routing Error
No route matches [POST] "/responses/replies/4"
Try running rake routes for more information on available routes.
This is how my architecture is set up: Offering has many Responses. Response has many Replies.
I know I need the form to submit to this route, but I can't get it to do that:
response_replies POST /responses/:response_id/replies(.:format) replies#create
_reply_form.html.erb:
<%= form_for(#reply, :url => response_reply_path([#response, #reply])) do |f| %>
<%= render 'common/form_errors', object: #reply %>
<%= f.label :body, "Your Reply" %>
<%= f.text_area(:body, :rows => 10, :class => "field span6") %>
<%= f.submit "Post Reply", :class => "block" %>
<% end %>
_response.html.erb:
<%= render 'replies/reply_form', {:response => #response, :reply => #reply} %>
offerings/show.html.erb:
<% if #offering.responses.any? %>
<%= render #offering.responses %>
<% else %>
<p>This offering has no responses yet.</p>
<% end %>
responses_controller.rb:
class ResponsesController < ApplicationController
before_filter :auth, only: [:create]
def show
#offering = Offering.new
#response = Response.new
#reply = Reply.new
end
def create
#offering = Offering.find(params[:offering_id])
# now that we have our offering we use it to
# build a response with it
#response = #offering.responses.build(params[:response])
# now we get the user who posted the response
#response.user = current_user
if #response.save
flash[:success] = 'your response has been posted!'
redirect_to #offering
else
#offering = Offering.find(params[:offering_id])
render 'offerings/show'
end
end
end
routes.rb:
resources :offerings, except: [:new] do
# makes it easier for us to display
# forms for responses on the offering show page
# allows us to have access to the
# offering that the response is associated to
resources :responses, only: [:create]
end
resources :responses, except: [:new] do
resources :replies, only: [:create]
end
rake routes produces this:
root / dashboard#index
users POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
need_applicants GET /needs/:need_id/applicants(.:format) applicants#index
POST /needs/:need_id/applicants(.:format) applicants#create
new_need_applicant GET /needs/:need_id/applicants/new(.:format) applicants#new
edit_need_applicant GET /needs/:need_id/applicants/:id/edit(.:format) applicants#edit
need_applicant GET /needs/:need_id/applicants/:id(.:format) applicants#show
PUT /needs/:need_id/applicants/:id(.:format) applicants#update
DELETE /needs/:need_id/applicants/:id(.:format) applicants#destroy
needs GET /needs(.:format) needs#index
POST /needs(.:format) needs#create
edit_need GET /needs/:id/edit(.:format) needs#edit
need GET /needs/:id(.:format) needs#show
PUT /needs/:id(.:format) needs#update
DELETE /needs/:id(.:format) needs#destroy
offering_responses POST /offerings/:offering_id/responses(.:format) responses#create
offerings GET /offerings(.:format) offerings#index
POST /offerings(.:format) offerings#create
edit_offering GET /offerings/:id/edit(.:format) offerings#edit
offering GET /offerings/:id(.:format) offerings#show
PUT /offerings/:id(.:format) offerings#update
DELETE /offerings/:id(.:format) offerings#destroy
response_replies POST /responses/:response_id/replies(.:format) replies#create
responses GET /responses(.:format) responses#index
POST /responses(.:format) responses#create
edit_response GET /responses/:id/edit(.:format) responses#edit
response GET /responses/:id(.:format) responses#show
PUT /responses/:id(.:format) responses#update
DELETE /responses/:id(.:format) responses#destroy
register /register(.:format) users#new
login /login(.:format) sessions#new
/offerings(.:format) offerings#index
/needs(.:format) needs#index
dashboard /dashboard(.:format) dashboard#index
contact /contact(.:format) contact#index
your_offerings /your_offerings(.:format) offerings#your_offerings
your_needs /your_needs(.:format) needs#your_needs
search /search(.:format) offerings#search
logout DELETE /logout(.:format) sessions#destroy
-- Original question
This should work for your form_for
<%= form_for([#response, #reply], :url => response_reply_path do |f| %>
-- Second part of your question route matches {:action=>"show", :controller=>"replies"}
I don't see anything wrong in your code, it's maybe in a part of your code you did not paste? Try to search for a link_to that would be corresponding
-- Bonus
Also you should not need to write your render with local variables, the controller instance variables #response and #reply will automatically be present in your partial
<%= render 'replies/reply_form' %>
# #response and #reply are automatically forwarded to all your views and partials
Off course if you are using response and reply in your partial you can rename them to #response and #reply respectively
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}?" %>
I think I've tried every single "Undefined Method" question solutions on StackOverflow but still haven't found one that works for my situation...
I have a model named "Has" and I'm thinking that my problem may be related to the "s" on the end of that. I keep getting this error anytime I try to load the has/new url. /has works just fine. Just not new...
undefined method `has_index_path' for #<#:0x007ff3bbaa8d48>
Has.rb:
class Has < ActiveRecord::Base
attr_accessible :bathroom_count, :bedroom_count, :neighborhood, :price, :property_type
belongs_to :trader
end
has_controller.rb:
class HasController < ApplicationController
def index
#has = Has.all
end
def show
#has = Has.find(params[:id])
end
def new
#has = Has.new
end
def create
#has = Has.new(params[:has])
if #has.save
flash[:success] = "New Listing Created!"
redirect_to (:back)
else
redirect_to (:back)
end
end
def destroy
#has.destroy
end
end
view (new.html.erb) (using simple_form, obviously and that's working just fine on other "new" views")
<div class="center hero-unit">
<%= simple_form_for (#has) do |f| %>
<%= f.association :trader%>
<%= f.input :price %>
<%= f.input :bathroom_count %>
<%= f.input :bedroom_count %>
<%= f.input :property_type %>
<%= f.input :neighborhood %>
<%= f.button :submit %>
<% end %>
routes.rb:
Algotest::Application.routes.draw do
resources :traders
resources :wants
resources :has
root to: 'static_pages#index'
match '/add_traders', to: 'traders#new'
match '/traders', to: 'traders#index'
match '/add_has', to: 'has#new'
match '/has', to: 'has#index'
match '/add_wants', to: 'wants#new'
match '/wants', to: 'wants#index'
end
EDIT: Here's the rake routes output:
traders GET /traders(.:format) traders#index
POST /traders(.:format) traders#create
new_trader GET /traders/new(.:format) traders#new
edit_trader GET /traders/:id/edit(.:format) traders#edit
trader GET /traders/:id(.:format) traders#show
PUT /traders/:id(.:format) traders#update
DELETE /traders/:id(.:format) traders#destroy
wants GET /wants(.:format) wants#index
POST /wants(.:format) wants#create
new_want GET /wants/new(.:format) wants#new
edit_want GET /wants/:id/edit(.:format) wants#edit
want GET /wants/:id(.:format) wants#show
PUT /wants/:id(.:format) wants#update
DELETE /wants/:id(.:format) wants#destroy
has GET /has(.:format) has#index
POST /has(.:format) has#create
new_ha GET /has/new(.:format) has#new
edit_ha GET /has/:id/edit(.:format) has#edit
ha GET /has/:id(.:format) has#show
PUT /has/:id(.:format) has#update
DELETE /has/:id(.:format) has#destroy
root / static_pages#index
add_traders /add_traders(.:format) traders#new
/traders(.:format) traders#index
add_has /add_has(.:format) has#new
/has(.:format) has#index
add_wants /add_wants(.:format) wants#new
/wants(.:format) wants#index
EDIT 3
New Route after adding the Inflections code snippet. "Has" routes are looking better but now I'm getting this error on any localhost:3000 page
No route matches {:action=>"show", :controller=>"has"}
Here are the new routes:
traders GET /traders(.:format) traders#index
POST /traders(.:format) traders#create
new_trader GET /traders/new(.:format) traders#new
edit_trader GET /traders/:id/edit(.:format) traders#edit
trader GET /traders/:id(.:format) traders#show
PUT /traders/:id(.:format) traders#update
DELETE /traders/:id(.:format) traders#destroy
wants GET /wants(.:format) wants#index
POST /wants(.:format) wants#create
new_want GET /wants/new(.:format) wants#new
edit_want GET /wants/:id/edit(.:format) wants#edit
want GET /wants/:id(.:format) wants#show
PUT /wants/:id(.:format) wants#update
DELETE /wants/:id(.:format) wants#destroy
has_index GET /has(.:format) has#index
POST /has(.:format) has#create
new_has GET /has/new(.:format) has#new
edit_has GET /has/:id/edit(.:format) has#edit
has GET /has/:id(.:format) has#show
PUT /has/:id(.:format) has#update
DELETE /has/:id(.:format) has#destroy
root / static_pages#index
add_traders /add_traders(.:format) traders#new
/traders(.:format) traders#index
add_has /add_has(.:format) has#new
/has(.:format) has#index
add_wants /add_wants(.:format) wants#new
/wants(.:format) wants#index
I would suggest using a noun for the name of your resource, but if for some reason you really want to keep the current name, you could use the following hack: trick rails into thinking that the "singular" and "plural" of "has" are the same using the inflector module:
In configs/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'has', 'has'
end
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