I created an edit form for my model "Things", but when I submit the form, it returns the error "No route matches [PATCH] "/things"". The weird thing is, the url of the error is http://localhost:3000/things, but I was expecting it to be http://localhost:3000/things/:id, because I wrote redirect_to #thing under the create function. I just defined all my Thing routes with resources :things, so it should have the update path defined. What am I doing wrong?
things_controller:
class ThingsController < ApplicationController
def show
#thing = Thing.find(params[:id])
#category_things = CategoryThing.all
#thing.categories.build
#thing.things.build
#related_things = RelatedThing.all
end
def index
end
def new
#thing = Thing.new
#things = Thing.all
end
def create
#thing = Thing.new(thing_params)
if #thing.save
redirect_to #thing
else
render 'new'
end
end
def edit
#thing = Thing.find(params[:id])
end
def update
#thing = Thing.find(params[:id])
if #thing.update_attributes(thing_params)
flash[:success] = "Thing updated"
redirect_to #thing
else
render 'edit'
end
end
private
def thing_params
params.require(:thing).permit(:name, :image_path, :avatar)
end
end
things/edit.html.erb:
<h1>Edit <%= #thing.name %></h1>
<p>
<%= form_for #thing, :url => things_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :name, :placeholder => "Name of the thing" %>
<br>
<%= f.label :categories_the_thing_belongs_to %>
<%= f.collection_select :categories, Category.all, :id, :name %>
<br>
<%= f.label :related_things %>
<%= f.collection_select :related_things, Thing.all, :id, :name %>
<br>
<%= f.label :display_picture %>
<%= f.file_field :avatar %>
<br>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</p>
Output of rake routes
Prefix Verb URI Pattern Controller#Action
ratings_new GET /ratings/new(.:format) ratings#new
down_votes_new GET /down_votes/new(.:format) down_votes#new
thing_new GET /thing/new(.:format) thing#new
good_comments_new GET /good_comments/new(.:format) good_comments#new
good_comments_show GET /good_comments/show(.:format) good_comments#show
bad_comments_new GET /bad_comments/new(.:format) bad_comments#new
related_things_new GET /related_things/new(.:format) related_things#new
things_new GET /things/new(.:format) things#new
category_things_new GET /category_things/new(.:format) category_things#new
thing_ratings_new GET /thing_ratings/new(.:format) thing_ratings#new
category_ratings_new GET /category_ratings/new(.:format) category_ratings#new
subjects GET /subjects(.:format) subjects#index
POST /subjects(.:format) subjects#create
new_subject GET /subjects/new(.:format) subjects#new
edit_subject GET /subjects/:id/edit(.:format) subjects#edit
subject GET /subjects/:id(.:format) subjects#show
PATCH /subjects/:id(.:format) subjects#update
PUT /subjects/:id(.:format) subjects#update
DELETE /subjects/:id(.:format) subjects#destroy
subjects_show GET /subjects/show(.:format) subjects#show
subject_things_new GET /subject_things/new(.:format) subject_things#new
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
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
categories_results GET /categories/results(.:format) categories#results
subjects_new GET /subjects/new(.:format) subjects#new
root GET / home_page#home
all_things_new GET /all/things/new(.:format) things#new
all_allthings GET /all/allthings(.:format) all#allthings
thing_good_comments GET /things/:thing_id/good_comments(.:format) good_comments#index
POST /things/:thing_id/good_comments(.:format) good_comments#create
new_thing_good_comment GET /things/:thing_id/good_comments/new(.:format) good_comments#new
edit_thing_good_comment GET /things/:thing_id/good_comments/:id/edit(.:format) good_comments#edit
thing_good_comment GET /things/:thing_id/good_comments/:id(.:format) good_comments#show
PATCH /things/:thing_id/good_comments/:id(.:format) good_comments#update
PUT /things/:thing_id/good_comments/:id(.:format) good_comments#update
DELETE /things/:thing_id/good_comments/:id(.:format) good_comments#destroy
thing_bad_comments GET /things/:thing_id/bad_comments(.:format) bad_comments#index
POST /things/:thing_id/bad_comments(.:format) bad_comments#create
new_thing_bad_comment GET /things/:thing_id/bad_comments/new(.:format) bad_comments#new
edit_thing_bad_comment GET /things/:thing_id/bad_comments/:id/edit(.:format) bad_comments#edit
thing_bad_comment GET /things/:thing_id/bad_comments/:id(.:format) bad_comments#show
PATCH /things/:thing_id/bad_comments/:id(.:format) bad_comments#update
PUT /things/:thing_id/bad_comments/:id(.:format) bad_comments#update
DELETE /things/:thing_id/bad_comments/:id(.:format) bad_comments#destroy
thing_ratings GET /things/:thing_id/ratings(.:format) ratings#index
POST /things/:thing_id/ratings(.:format) ratings#create
new_thing_rating GET /things/:thing_id/ratings/new(.:format) ratings#new
edit_thing_rating GET /things/:thing_id/ratings/:id/edit(.:format) ratings#edit
thing_rating GET /things/:thing_id/ratings/:id(.:format) ratings#show
PATCH /things/:thing_id/ratings/:id(.:format) ratings#update
PUT /things/:thing_id/ratings/:id(.:format) ratings#update
DELETE /things/:thing_id/ratings/:id(.:format) ratings#destroy
thing_up_votes GET /things/:thing_id/up_votes(.:format) up_votes#index
POST /things/:thing_id/up_votes(.:format) up_votes#create
new_thing_up_vote GET /things/:thing_id/up_votes/new(.:format) up_votes#new
edit_thing_up_vote GET /things/:thing_id/up_votes/:id/edit(.:format) up_votes#edit
thing_up_vote GET /things/:thing_id/up_votes/:id(.:format) up_votes#show
PATCH /things/:thing_id/up_votes/:id(.:format) up_votes#update
PUT /things/:thing_id/up_votes/:id(.:format) up_votes#update
DELETE /things/:thing_id/up_votes/:id(.:format) up_votes#destroy
things GET /things(.:format) things#index
POST /things(.:format) things#create
new_thing GET /things/new(.:format) things#new
edit_thing GET /things/:id/edit(.:format) things#edit
thing GET /things/:id(.:format) things#show
PATCH /things/:id(.:format) things#update
PUT /things/:id(.:format) things#update
DELETE /things/:id(.:format) things#destroy
things_show GET /things/show(.:format) things#show
things_results GET /things/results(.:format) things#results
things_random GET /things/random(.:format) things#random
web_console /console WebConsole::Engine
Routes for WebConsole::Engine:
root GET / web_console/console_sessions#index
input_console_session PUT /console_sessions/:id/input(.:format) web_console/console_sessions#input
pending_output_console_session GET /console_sessions/:id/pending_output(.:format) web_console/console_sessions#pending_output
configuration_console_session PUT /console_sessions/:id/configuration(.:format) web_console/console_sessions#configuration
console_sessions GET /console_sessions(.:format) web_console/console_sessions#index
POST /console_sessions(.:format) web_console/console_sessions#create
new_console_session GET /console_sessions/new(.:format) web_console/console_sessions#new
edit_console_session GET /console_sessions/:id/edit(.:format) web_console/console_sessions#edit
console_session GET /console_sessions/:id(.:format) web_console/console_sessions#show
PATCH /console_sessions/:id(.:format) web_console/console_sessions#update
PUT /console_sessions/:id(.:format) web_console/console_sessions#update
DELETE /console_sessions/:id(.:format) web_console/console_sessions#destroy
just remove the :url => things_path and use:
<%= form_for #thing, :html => { :multipart => true } do |f| %>
The form_for helper automatically generates the proper resfull endpoint if using ressourcefull routes.
Depending on #thing being persisted? or a new_record? it the endpoint will be :create or :update
Related
**ARTICLE CONTROLLER** //controller
class ArticlesController < ApplicationController
def index
#article=Article.all
end
def show
#article=Article.find(params[:id])
#comment = Comment.new
#comment.article_id = #article.id
end
def new
#article=Article.new
end
def create
#article = Article.new(article_params)
#article.save
redirect_to article_path(#article)
end
def destroy
article=Article.find(params[:id])
article.destroy
redirect_to articles_path
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
#article.update(article_params)
flash.notice = "Article '#{#article.title}' Updated!"
redirect_to article_path(#article)
end
def article_params
params.require(:article).permit(:title, :body)
end
end
**SHOW.HTML.ERB** /view file
<h1><%=#article.title%></h1>
<p><%=#article.body%></p>
<br><hr>
<%= link_to "edit", edit_article_path(#article) %>|
<%= link_to "delete",article_path(#article), method: :delete,data: {confirm: "Really delete the article?"} %>|
<%= link_to "<< Back to Articles List", articles_path %>
<h3>Comments</h3>
<%= render partial: 'articles/comment', collection: #article.comments %>
<%= render partial: 'comments/form' %>
**_FORM.HTML.ERB** //_form view
<h3>Post a Comment</h3>
<%= form_for [ #article, #comment ] do |f| %>
<p>
<%= f.label :author_name %><br/>
<%= f.text_field :author_name %>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
rake routes:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / articles#index
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
i get no articles_comments_path!
NoMethodError in Articles#show
Showing /home/manoj/ror/blogger/app/views/comments/_form.html.erb where line #3 raised:
undefined method article_comments_path' for #<#<Class:0x00000005be3d40>:0x000000054cacc0>
app/views/comments/_form.html.erb:3:in_app_views_comments__form_html_erb__3077497298558231225_47236640'
app/views/articles/show.html.erb:11:in `_app_views_articles_show_html_erb__4529829459036724249_48053660'
You are getting this error because you don't have article_comments_path helper method. Change your routes to this:
resources :articles do
resources :comments
end
OR
If you don't want to nest routes then change your form to this:
<%= form_for #comment do |f| %>
// form fields
<% end %>
Also in show action you can dry up your code by using association methods
def show
#article= Article.find(params[:id])
#comment = #article.comments.build
end
Mamboo!
Am new to RoR and using Lynda Ruby on Rails 4 Essential Training.
I keep getting " Unknown Action, The action '1' could not be found for SubjectsController" After pressing "Update Subject", on my edit page. I think it can not find update function! What is missing here?
Route.rb
Rails.application.routes.draw do
resources :subjects do get 'index', on: :collection end
match ':controller(/:action(/id))', :via => [:get, :post]
end
subjects_controller.rb
class SubjectsController < ApplicationController
layout false
def index
#subjects = Subject.sorted
end
def show
#subject = Subject.find(params[:id])
end
def new
#subject = Subject.new({:name => "Default"})
end
def create
#subject = Subject.new(subject_params)
if #subject.save
flash[:notice] = "Subject Created Successfully"
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#subject = Subject.find(params[:id])
end
def update
#subject = Subject.find(params[:id])
if #subject.update_attributes(subject_params)
flash[:notice] = "Subject Updated Successfully"
redirect_to(:action => 'show')
else
render('edit')
end
end
def delete
#subject = Subject.find(params[:id])
end
def destroy
#subject = Subject.find(params[:id])
#subject.destroy
flash[:notice] = "Subject Destroyed Successfully"
redirect_to(:action => "index")
end
private
def subject_params
params.require(:subject).permit(:name, :position, :visible)
end
end
edit.html.erb
<%= link_to("<< Back to List", {:action => 'show'}, :class => 'back-link') %>
<div>
<h2>Update Subjects</h2>
<%= form_for(:subject, :url => {:action => "update", :id => #subject.id}) do |f| %>
<table summary="Subject form field">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visbile</th>
<td><%= f.text_field(:visible) %></td>
</tr>
</table>
<div class="form-buttons">
<%= f.submit("Update Subject") %>
</div>
<% end %>
</div>
rake routes
$ rake routes
Prefix Verb URI Pattern Controller#Action
sections GET /sections(.:format) sections#index
POST /sections(.:format) sections#create
new_section GET /sections/new(.:format) sections#new
edit_section GET /sections/:id/edit(.:format) sections#edit
section GET /sections/:id(.:format) sections#show
PATCH /sections/:id(.:format) sections#update
PUT /sections/:id(.:format) sections#update
DELETE /sections/:id(.:format) sections#destroy
pages GET /pages(.:format) pages#index
POST /pages(.:format) pages#create
new_page GET /pages/new(.:format) pages#new
edit_page GET /pages/:id/edit(.:format) pages#edit
page GET /pages/:id(.:format) pages#show
PATCH /pages/:id(.:format) pages#update
PUT /pages/:id(.:format) pages#update
DELETE /pages/:id(.:format) pages#destroy
subjects GET /subjects(.:format) subjects#index
POST /subjects(.:format) subjects#create
new_subject GET /subjects/new(.:format) subjects#new
edit_subject GET /subjects/:id/edit(.:format) subjects#edit
subject GET /subjects/:id(.:format) subjects#show
PATCH /subjects/:id(.:format) subjects#update
PUT /subjects/:id(.:format) subjects#update
DELETE /subjects/:id(.:format) subjects#destroy
No need to specify match ':controller(/:action(/id))', :via => [:get, :post] in the routes. resources :subjects will give u route for all new, create, update, show & delete actions. Try this
Rails.application.routes.draw do
resources :subjects
end
As pulkit21 already stated, you don't need that match in your routes. That works for some specific cases, but in general this is usually hacky code that can be avoided with the correct usage of resource routing.
Removing that line in your routes file will then give you the error "No route matches [POST] "/subjects/1". While I don't fully understand why this happens, I do know that the form_for helper don't need to have the :url parameter set. As a matter of fact, when you use the rails scaffold to generate a controller and views, it uses a single form on a partial file for both the new and edit pages. It can do that because it can detect if a object is a new record (don't have an ID yet, so call CREATE) or is an existing record (already has a database ID, so call PUT to update).
In your case, Rails seems to be misunderstanding the :action => "update" part and instead of routing this to the PUT method (which will call the update method on your controller), is routing it to POST.
Try changing your form_for to this:
<%= form_for(#subject) do |f| %>
It's more elegant, readable and simple, and has the advantage of being able to work for both new and edit forms.
I'm having trouble replying to conversations with the Mailboxer gem in Rails 4. There isn't a whole lot of documentation on this gem, or maybe I'm just not experienced enough, but I've been stuck on this for a while now.
view/conversations/index:
(shows list of all current user's conversations)
<% #conversations.each do |conversation| %>
<%= link_to conversation.subject, reply_conversation_path(conversation.id) %>
<%= conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %>
<%= link_to "Move to Trash", {:controller => "conversations", :action =>
"trash", :id => conversation.id}, :title=> "Move to Trash", :method=>'post' %>
<% end %>
When I click the first link_to in the above view, I receive the routing error: No route matches [GET] "/conversations/68/reply".
I was hoping to have it render the following view, and have the correct conversation passed to it:
view/messages/_form/
(used to reply to existing conversations)
Reply:
<%= form_for :message, url: [:reply, conversation] do |f| %>
<%= f.text_area :body %>
<%= f.submit "Send Message", class: 'btn btn-primary' %>
<%= submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger' %>
<% end %>
Routes:
resources :users
root to: 'profiles#index'
resources :messages do
member do
post :new
end
end
resources :conversations do
member do
post :reply
post :trash
post :untrash
end
collection do
get :trashbin
post :empty_trash
end
end
Conversations Controller:
class ConversationsController < ApplicationController
before_filter :authenticate_user!
helper_method :mailbox, :conversation
def index
#conversations ||= current_user.mailbox.inbox.all
end
def reply
current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
redirect_to conversation
end
def trashbin
#trash ||= current_user.mailbox.trash.all
end
def trash
conversation.move_to_trash(current_user)
redirect_to :conversations
end
def untrash
conversation.untrash(current_user)
redirect_to :back
end
def empty_trash
current_user.mailbox.trash.each do |conversation|
conversation.receipts_for(current_user).update_all(:deleted => true)
end
redirect_to :conversations
end
private
def mailbox
#mailbox ||= current_user.mailbox
end
def conversation
#conversation ||= mailbox.conversations.find(params[:id])
end
def conversation_params(*keys)
fetch_params(:conversation, *keys)
end
def message_params(*keys)
fetch_params(:message, *keys)
end
def fetch_params(key, *subkeys)
params[key].instance_eval do
case subkeys.size
when 0 then self
when 1 then self[subkeys.first]
else subkeys.map{|k| self[k] }
end
end
end
end
Messages Controller:
class MessagesController < ApplicationController
# GET /message/new
def new
#request = Request.find(params[:request])
#message = current_user.messages.new
#user = #request.user
end
# POST /message/create
def create
#user = User.find(params[:user])
#body = params[:body]
#subject = params[:subject]
current_user.send_message(#user, params[:body], params[:subject])
flash[:notice] = "Message has been sent!"
redirect_to :conversations
end
end
relevant rake routes:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / profiles#index
message POST /messages/:id(.:format) messages#new
messages GET /messages(.:format) messages#index
POST /messages(.:format) messages#create
new_message GET /messages/new(.:format) messages#new
edit_message GET /messages/:id/edit(.:format) messages#edit
GET /messages/:id(.:format) messages#show
PATCH /messages/:id(.:format) messages#update
PUT /messages/:id(.:format) messages#update
DELETE /messages/:id(.:format) messages#destroy
reply_conversation POST /conversations/:id/reply(.:format) conversations#reply
trash_conversation POST /conversations/:id/trash(.:format) conversations#trash
untrash_conversation POST /conversations/:id/untrash(.:format) conversations#untrash
trashbin_conversations GET /conversations/trashbin(.:format) conversations#trashbin
empty_trash_conversations POST /conversations/empty_trash(.:format) conversations#empty_trash
conversations GET /conversations(.:format) conversations#index
POST /conversations(.:format) conversations#create
new_conversation GET /conversations/new(.:format) conversations#new
edit_conversation GET /conversations/:id/edit(.:format) conversations#edit
conversation GET /conversations/:id(.:format) conversations#show
PATCH /conversations/:id(.:format) conversations#update
PUT /conversations/:id(.:format) conversations#update
DELETE /conversations/:id(.:format) conversations#destroy
I've been following this tutorial: http://jamestansley.com/2014/02/22/customizing-the-mailboxer-ruby-gem-2/
In case I've left anything out, here's my github repo:
https://github.com/portOdin/GoFavorIt-Heroku/tree/stackflow/app.
The route is a post route:
resources :conversations do
member do
post :reply
Your form needs to use an HTTP POST request, and because you haven't specified a method, it's defaulting to a GET request.
Replace this...
<%= form_for :message, url: [:reply, conversation] do |f| %>
with this:
<%= form_for :message, url: [:reply, conversation], method: :post do |f| %>
Your rake routes shows reply_conversation POST /conversations/:id/reply(.:format) conversations#reply but your link_to will send a get request. You need to change your route to make it a get request.
I am getting the following error on my app when I try to access /messages/new:
No route matches [GET] "/messages/new"
If I try adding a /messages/new route to config.rb, I get the following error:
No route matches {:controller=>"messages"}
The app itself is like Gumtree.com, where Users come and create Posts (e.g. selling my car), and people respond my sending them Messages (through the Simple Pvt Messages plugin).
Any insights on this would be greatly appreciated.
Thanks!
Faisal
ROUTES.RB
Mysalary::Application.routes.draw do
resources :users do
resources :messages do
collection do
post :delete_selected
end
end
end
resources :users
resources :profiles
resources :pages
resources :posts
resources :messages
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
MESSAGE>NEW VIEW
<% form_for #message, :url => user_messages_path(#user) do |f| %>
<p>
To:<br />
<%= f.text_field :to %>
<%= error_message_on #message, :to %>
</p>
<p>
Subject:<br />
<%= f.text_field :subject %>
<%= error_message_on #message, :subject %>
</p>
<p>
Message<br />
<%= f.text_area :body %>
<%= error_message_on #message, :body %>
</p>
<p>
<%= submit_tag "Send" %>
</p>
<% end %>
MESSAGES CONTROLLER
class MessagesController < ApplicationController
before_filter :set_user
def index
if params[:mailbox] == "sent"
#messages = #user.sent_messages
else
#messages = #user.received_messages
end
end
def show
#message = Message.read_message(params[:id], current_user)
end
def new
#message = Message.new
if params[:reply_to]
#reply_to = #user.received_messages.find(params[:reply_to])
unless #reply_to.nil?
#message.to = #reply_to.sender.login
#message.subject = "Re: #{#reply_to.subject}"
#message.body = "\n\n*Original message*\n\n #{#reply_to.body}"
end
end
end
def create
#message = Message.new(params[:message])
#message.sender = #user
#message.recipient = User.find_by_login(params[:message][:to])
if #message.save
flash[:notice] = "Message sent"
redirect_to user_messages_path(#user)
else
render :action => :new
end
end
def delete_selected
if request.post?
if params[:delete]
params[:delete].each { |id|
#message = Message.find(:first, :conditions => ["messages.id = ? AND (sender_id = ? OR recipient_id = ?)", id, #user, #user])
#message.mark_deleted(#user) unless #message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to :back
end
end
private
def set_user
#user = User.first
end
end
MESSAGE MODEL
class Message < ActiveRecord::Base
is_private_message
attr_accessor :to
end
RAKE ROUTES OUTPUT
delete_selected_user_messages POST /users/:user_id/messages/delete_selected(.:format) {:action=>"delete_selected", :controller=>"messages"}
user_messages GET /users/:user_id/messages(.:format) {:action=>"index", :controller=>"messages"}
POST /users/:user_id/messages(.:format) {:action=>"create", :controller=>"messages"}
new_user_message GET /users/:user_id/messages/new(.:format) {:action=>"new", :controller=>"messages"}
edit_user_message GET /users/:user_id/messages/:id/edit(.:format) {:action=>"edit", :controller=>"messages"}
user_message GET /users/:user_id/messages/:id(.:format) {:action=>"show", :controller=>"messages"}
PUT /users/:user_id/messages/:id(.:format) {:action=>"update", :controller=>"messages"}
DELETE /users/:user_id/messages/:id(.:format) {:action=>"destroy", :controller=>"messages"}
messages GET /messages(.:format) {:action=>"index", :controller=>"messages"}
POST /messages(.:format) {:action=>"create", :controller=>"messages"}
new_message GET /messages/new(.:format) {:action=>"new", :controller=>"messages"}
edit_message GET /messages/:id/edit(.:format) {:action=>"edit", :controller=>"messages"}
message GET /messages/:id(.:format) {:action=>"show", :controller=>"messages"}
PUT /messages/:id(.:format) {:action=>"update", :controller=>"messages"}
DELETE /messages/:id(.:format) {:action=>"destroy", :controller=>"messages"}
pages_home GET /pages/home(.:format) {:action=>"home", :controller=>"pages"}
pages_about GET /pages/about(.:format) {:action=>"about", :controller=>"pages"}
pages_legal GET /pages/legal(.:format) {:action=>"legal", :controller=>"pages"}
pages_feedback GET /pages/feedback(.:format) {:action=>"feedback", :controller=>"pages"}
messages_new GET /messages/new(.:format) {:action=>"new", :controller=>"messages"}
root
Replace your route file with this you are defining resource message inside users and also twice.
Mysalary::Application.routes.draw do
resources :messages do
collection do
post :delete_selected
end
end
resources :users
resources :profiles
resources :pages
resources :posts
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
Try it...
I've added a method to my controller and routed it correctly but when I try to call it from a form_tag it give me a router error. What's going on?
<% form_tag search_item_path, :method => 'get' do %>
<%= text_field_tag :name , '' %>
<%= submit_tag "Submit" %>
<% end %>
routes:
resources :items do
collection do
get :search, :as => :search
end
end
rake routes also ok:
search_item GET /items/:id/search(.:format) {:action=>"search", :controller=>"items"}
items GET /items(.:format) {:action=>"index", :controller=>"items"}
POST /items(.:format) {:action=>"create", :controller=>"items"}
new_item GET /items/new(.:format) {:action=>"new", :controller=>"items"}
edit_item GET /items/:id/edit(.:format) {:action=>"edit", :controller=>"items"}
item GET /items/:id(.:format) {:action=>"show", :controller=>"items"}
PUT /items/:id(.:format) {:action=>"update", :controller=>"items"}
DELETE /items/:id(.:format) {:action=>"destroy", :controller=>"items
However, if I write something like this works:
<% form_tag url_for(:controller => "items" , :action => "search"), :method => "get" do %>
What am I missing here?
I believe it should be pluralized search_items_path
And routes could be little cleaner
resources :items do
collection do
get :search
end
end
or
resources :items do
get :search, :on => :collection
end
Your route is looking for an id, and must be called with search_item_path(#item)?
Something is not right there. With the routes.rb you gave, it should look like:
search_items GET /items/search(.:format)
Are we seeing everything here? Your sample defines a collection route but the output of your routes.rb shows it as a member route.