I have a object called "category", in my view/store/manage.html.erb, I want to do this :
<%=link_to_remote category.name, :url => delete_category_path(category),
:confirm => 'Are you sure?', :method => :delete%>
But it show me the NoMethodError, how can I do that?
This is the error from RoR:
undefined method `delete_category_path' for #<ActionView::Base:0x103490da0>
This is my manage method in the store_controller.rb:
def manage
#categories = Category.all
#products = Product.all
#category = Category.new(params[:category])
end
You should use just category_path(#category). Both URL's are the same and only the HTTP method changes. In your case it would be:
<%=link_to_remote category.name, :url => category_path(category),
:confirm => 'Are you sure?', :method => :delete%>
As you can see with rake routes:
categories GET /categories(.:format) {:controller=>"categories", :action=>"index"}
POST /categories(.:format) {:controller=>"categories", :action=>"create"}
new_category GET /categories/new(.:format) {:controller=>"categories", :action=>"new"}
edit_category GET /categories/:id/edit(.:format) {:controller=>"categories", :action=>"edit"}
category GET /categories/:id(.:format) {:controller=>"categories", :action=>"show"}
PUT /categories/:id(.:format) {:controller=>"categories", :action=>"update"}
DELETE /categories/:id(.:format) {:controller=>"categories", :action=>"destroy"}
The actions show, update and destroy share the same category_path.
You have to add to the config/routes.rb the next following code:
map.resources :categories
(Resources - create paths for the next commands, create, update, delete, new, edit).
For more information read in the Rails Way book or in the tutorials rubyonrails.com.
Related
I'm getting this error, ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.), when I try to submit a form. My route looks like this,
admin.resources :email_launcher
and this is my form_for,
form_for :email_launcher, :url => new_admin_email_launcher_path, :method => :get do |f|
This is not in a form but in the new view, and I'm using HAML. I've google searched all day on the error but never found anything of value. Any help would be great, thanks.
Here's my rake routes for admin_email_launcher
admin_email_launcher_index GET /admin/email_launcher(.:format) {:controller=>"admin/email_launcher", :action=>"index"}
POST /admin/email_launcher(.:format) {:controller=>"admin/email_launcher", :action=>"create"}
new_admin_email_launcher GET /admin/email_launcher/new(.:format) {:controller=>"admin/email_launcher", :action=>"new"}
edit_admin_email_launcher GET /admin/email_launcher/:id/edit(.:format) {:controller=>"admin/email_launcher", :action=>"edit"}
GET /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"show"}
PUT /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"update"}
DELETE /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"destroy"}
routes.rb
namespace :admin do
resources :email_launcher
end
admin/email_launchers_controller.rb
def new
#email_launcher = EmailLauncher.new
end
admin/email_launcher/new
<%= form_for([:admin,#email_launcher]) do |f| %>
...
<%= f.submit %>
<% end -%>
It will POST new email_launcher.
Why do you need to GET the new action? You're already on the new page.
I have a list of names, that contain this:
<td><%= product.date %></td>
and it is in my index page. (It is the list of my applications)
I want to assign a custom link to that, and pass THAT value to the controller:
I tried this:
<td><%= link_to product.date, {:controller => "product", :action => "sort_by_date", <how do I pass the 'product.date' string?> }%></td>
I read the rails route guide but I couldn't find help
Thanks
what rakes routes returns:
products GET /products(.:format) products#index
POST /products(.:format) products#create
new_product GET /products/new(.:format) products#new
edit_product GET /products/:id/edit(.:format) products#edit
product GET /products/:id(.:format) products#show
PUT /products/:id(.:format) products#update
DELETE /products/:id(.:format) products#destroy
root /
products#home.html
use like below
<%= link_to product.date, {:controller => "product", :action => "sort_by_date", :product_date => product.date %>
and in your controller you should get the date in params[:product_date]
You can also use resource helper function to pass custom parameters like below.
<%= link_to product.date, products_path(:product_date => product.date)
This method will route to the ProductsController#index with a params[:product_date]
Edit:
I just tried with following
resources :products do
collection do
get 'sort_by_date' => 'products#sort_by_date'
end
end
The rake routes are as following
sort_by_date_products GET /products/sort_by_date(.:format) products#sort_by_date
To generate the URL it will be better to use the resource helper function like below
sort_by_date_products_path(:product_date => '1/1/2012')
This will generate a url like /product/sort_by_date?product_date=1/1/2012 which will call the sort_by_date method of ProductsController and also have params[:product_date] available.
Lets check if its work...
I am using Ruby on Rails v3.2.2 and I would like to use plural names for nested resources. That is, in my config/routes.rb I have (note: "category" and "article" are sample resources):
resources :categories do
resources :articles do
collection do
get 'one'
post 'two'
put 'three'
end
member do
get 'four'
post 'five'
put 'six'
end
end
end
The above statements generates the following:
$ rake routes
one_category_articles GET /categories/:category_id/articles/one(.:format) articles#one
two_category_articles POST /categories/:category_id/articles/two(.:format) articles#two
three_category_articles PUT /categories/:category_id/articles/three(.:format) articles#three
four_category_article GET /categories/:category_id/articles/:id/four(.:format) articles#four
five_category_article POST /categories/:category_id/articles/:id/five(.:format) articles#five
six_category_article PUT /categories/:category_id/articles/:id/six(.:format) articles#six
category_articles GET /categories/:category_id/articles(.:format) articles#index
POST /categories/:category_id/articles(.:format) articles#create
new_category_article GET /categories/:category_id/articles/new(.:format) articles#new
edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) articles#edit
category_article GET /categories/:category_id/articles/:id(.:format) articles#show
PUT /categories/:category_id/articles/:id(.:format) articles#update
DELETE /categories/:category_id/articles/:id(.:format) articles#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
I would like to change statements in my config/routes.rb so to generate following routers with plural names only for the category_article "part" (that is, I would like to use categories_article/categories_articles respectively instead of category_article/category_articles):
$ rake routes
# Note: I marked changes from the previous outputting with '=>'.
=> one_categories_articles GET /categories/:category_id/articles/one(.:format) articles#one
=> two_categories_articles POST /categories/:category_id/articles/two(.:format) articles#two
=> three_categories_articles PUT /categories/:category_id/articles/three(.:format) articles#three
=> four_categories_article GET /categories/:category_id/articles/:id/four(.:format) articles#four
=> five_categories_article POST /categories/:category_id/articles/:id/five(.:format) articles#five
=> six_categories_article PUT /categories/:category_id/articles/:id/six(.:format) articles#six
=> categories_articles GET /categories/:category_id/articles(.:format) articles#index
POST /categories/:category_id/articles(.:format) articles#create
new_category_article GET /categories/:category_id/articles/new(.:format) articles#new
edit_category_article GET /categories/:category_id/articles/:id/edit(.:format) articles#edit
category_article GET /categories/:category_id/articles/:id(.:format) articles#show
PUT /categories/:category_id/articles/:id(.:format) articles#update
DELETE /categories/:category_id/articles/:id(.:format) articles#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format)
If you aren't satisfied with the provided helpers then you can use the magic:
<%= link_to 'One', [:one, #category, Article] %>
# /categories/123/articles/one
<%= link_to 'Four', [:four, #category, #article] %>
# /categories/123/articles/456/four
<%= link_to 'Edit the article', [:edit, #category, #article] %>
# /categories/123/articles/456/edit
<%= link_to 'All articles for the category', [#category, Article] %>
# /categories/123/articles
The very same approach can be used to specify the :url option for the form_for helper.
I hope you got the idea!
P.S. Note the using of a class (like Article) to specify that you want to see all records and using of a model instance (like #article) to say that you are about to see one article.
root :to => "index#home"
#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get
match "/tattoo" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get
members section and its nested images
resources :members, :except => [:new, :create] do
resources :tattoos
end
Thats whats in my routes.rb file. They produce:
root /(.:format) {:controller=>"index", :action=>"home"}
submit GET /submit(.:format) {:controller=>"index", :action=>"new"}
tattoo POST /tattoo(.:format) {:controller=>"index", :action=>"create"}
GET /tattoo/:id(.:format) {:controller=>"index", :action=>"show"}
tagged GET /tagged(.:format) {:controller=>"index", :action=>"tagged"}
DELETE /tattoo/:id(.:format) {:controller=>"index", :action=>"destroy"}
tattoos GET /tattoos(.:format) {:controller=>"index", :action=>"index"}
members GET /members(.:format) {:action=>"index", :controller=>"members"}
edit_member GET /members/:id/edit(.:format) {:action=>"edit", :controller=>"members"}
member GET /members/:id(.:format) {:action=>"show", :controller=>"members"}
PUT /members/:id(.:format) {:action=>"update", :controller=>"members"}
DELETE /members/:id(.:format) {:action=>"destroy", :controller=>"members"}
But i have a problem. For some reason, when I try to go to mysite.com/submit
I used to get this error
No route matches {:controller=>"images"}
on
<%= form_for #tattoo, :html =>{:multipart => true} do |f| %>
but that has magically changed to:
undefined method `images_path'
on the same line.
when my controller has this:
indexcontroller
def new
#tattoo = Image.new
end
def create
#tattoo = Image.new(params[:image])
if #tattoo.save
flash[:success] = "Tattoo sent in for approval!"
redirect_to(images_path)
else
render :action => "new"
end
end
And then this link_to:
<%= link_to "Manage tattoos", member_tattoos_path() %>
give me this error:
No route matches {:controller=>"tattoos"}
I thought I was beginning to understand routes and had a decent grasp but I dont get whats going on!
You need to pass in a member object to edit_member_path.
<%= link_to "Edit profile", edit_member_path(#member) %>
edit_member_path should know the id of the member you want to edit. Please try
<%= link_to "Edit profile", edit_member_path(#member) %>
For No route matches {:controller=>"images"}; since the action image is not defined in your route, please try to stop and restart the server and check if there is any plugin like Paperclip in place.
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