Rails route is not being found - ruby-on-rails

My Rails route isn't working, I get this error on load:
undefined method `accept_roomidex_requests_path' for #<#<Class:0x00000103394050>:0x000001033ac920>
Here are my (relevant) file contents:
config.rb
get '/roomidex_requests/:id/accept' => 'roomidex_requests#accept', :as => :accept_roomidex_requests_path
roomidex_requests_controller.rb
def accept
# code to do stuff
end
some_view.rb
<%= link_to "Accept",
accept_roomidex_requests_path(:id),
:method => :post,
:remote => true %>

Try (without _path suffix in as option):
get '/roomidex_requests/:id/accept' => 'roomidex_requests#accept', :as => :accept_roomidex_requests
And probably you should change http verb to post.

Related

No route matches [PUT] error in Rails 4.2.6

I am facing this error while running my application:
No route matches [PUT] "/accounts/1/payment"
This is my form syntax;
<%= semantic_form_for(#account, :url => payment_path, :html => { :method => :put }) do |f| %>
......
<% end %>
routes.rb:
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
controller method:
def billing
#account = Account.find(params[:id])
if request.put?
// some code here
end
end
Replace
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
with
match "/accounts/:id/payment", to: "accounts#payment", :as => "payment", via: [:get, :put]
and let me know if this works.
EDIT
If your controller action is billing, then you may have to do
match "/accounts/:id/payment", to: "accounts#billing", :as => "payment", via: [:get, :put]
your form is trying to find a route matches method put while your route has a get method.
Use put in routes instead of get
put "/accounts/:id/payment" => "accounts#payment", :as => "payment"
In your form you are telling this form to be treated as PUT request but your routes says it is GET request. You should use same request type in both routes.rb and your form definition.
Try this
put "/accounts/:id/payment" => "accounts#billing", :as => "payment"

Routing and form in Rails

I have a controller for a resource, BuddiesController. My routes config file up until now has been
resources :buddies
match ':controller(/:action(/:id))', :via => [:get, :post]
I didn't realize what the ' resources :buddies ' line was doing until I read up on routing in Rails just now, because the behavior has been identical with what I expected until now. The problem was that I wanted to add a non-CRUD action to the controller: 'search'. Every time I used link_to(:action => 'search'), I would get an exception saying that action 'show' could not be found despite the url being ' localhost:3000/buddies/search ' as expected. I have several questions arising from this:
Firstly, the form I used in 'new' stopped working:
%= form_for(#buddy, {:action => :create, :method => :post, :html => {:role => "form"}}) do |f| %>
because buddies_path couldn't be found. How could I manually add a buddies_path to my routes?
Secondly, I revised the form to use:
<%= form_for(#buddy, :url => {:action => :create, :id => #buddy.id}, :html => {:role => "form", :id => #buddy.id}) do |f| %>
but this has caused the form to give me password and email confirmation not matching errors even if they match. What's going on here?
Lastly, what is the best way to add a search action to my resource?
#routes.rb
resources :buddies
collection do
get :search
end
end
now when you run rake routes | grep 'buddies' you will get output something like this :
now you need to define this search action in your buddies controller .
#buddies_controller.rb
Class BuddiesController < ApplicationController
def search
end
end
Have your search form in app/views/buddies/search.html.erb
Now in order to open your search form / to hit your search action you need to use
<%= link_to 'Search XYZ', search_buddies_path %>
against buddies#search you can see search_buddies
In routes.rb:
resources :buddies do
collection do
post :search
end
end
This might make your routing works.

"The page you were looking for doesn't exist." edit form

I have the following route
namespace :dashboard do
get '/courses/:id/edit' => 'courses#edit', :as => :edit_course
put 'courses/:id/update' => 'courses#update'
end
and this form
= form_tag dashboard_edit_course_url( #course), :method => 'post', :multipart => true do
...
the action being:
<form accept-charset="UTF-8" action="http://localhost:3000/dashboard/courses/54633b9fc14ddd104c004de3/edit" enctype="multipart/form-data" method="post">
But when I submit the form I get this error:
The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
I don't understand why? Could somebody explain?
An alternative way to handle this. In your routes write:
namespace :dashboard
resources :courses, only: [:edit, :update]
end
And in your view write:
= form_tag [:dashboard, #course], multipart: true do |f|
Then you will use rails defaults.
Your form states to use post, but you don't have a post route configured.
The rails way to do this is to submit the form to the update path via put, since you are updating a record:
= form_tag dashboard_update_course_path( #course), :method => 'put', :multipart => true do
Also, you probably want to use path instead of url.
Then just name the update route:
namespace :dashboard do
get '/courses/:id/edit' => 'courses#edit', :as => :edit_course
put '/courses/:id/update' => 'courses#update', :as => :update_course
end
The first parameter is where the form submission should go (update).
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

Can I make a route that does not map to a URL in Ruby on Rails?

I have a bunch of routes in my routes.rb that look like this:
post 'events/form1'=> 'events#form1', :as => 'events_form1'
post 'events/form2'=> 'events#form2', :as => 'events_form2'
post 'events/form3'=> 'events#form3', :as => 'events_form3'
These controller actions process data from my forms. The user is never going to need to access these actions by URL so I wanted to do something like this:
post 'events#form1'
and then, in my form, write
<%= simple_form_for :something, :url => url_for{:controller => 'events', :action => 'form1'}, :method => "post" do |f| %>
This doesn't work because rails complains that the route is invalid. Is it possible to have a route without a url? If not, how can I clean up my routes file?
The simple_form_for call still needs a URL, even if your users will never use it directly.
You can kind of see this in the code itself, in that one of the options you're sending to simple_form_for is an option for the URL to use:
:url => url_for{:controller => 'events', :action => 'form1'}

Rails3 Routing with Member Routes

I tried following the advice in this Rails3 Routes - Passing parameter to a member route
I have:
_health.html (partial)
<%= link_to image_tag("icons/pro_gray.png", :border =>'none'), flag_contact_flags_path(#id), :action => "flag", :id => #id, :remote => true, :class => 'flag' %>
routes.rb
resources :contact_flags do
get 'flag/:id', :action => 'flag', :on => :collection
end
When I do this I get this error:
ActionView::Template::Error (undefined method `flag_contact_flags_path' for #<#:0x00000101430358>):
When I rake routes I show:
GET /contact_flags/flag/:id(.:format) {:action=>"flag", :controller=>"contact_flags"}
Why doesn't think work?
I really need to pass the :id parameter.. any ideas?
Thanks
Looks like you need to change up the route you're pointing to. Try contact_flags_path(#id) instead of flags_contact_flags(#id).
Any luck?

Resources