No route matches error in Rails - ruby-on-rails

I am getting the following error
No route matches {:action=>"index",
:controller=>"module/admin/orders"}
when using following:
<%= form_tag({ :action => :index}, { :method => "get" }) do %>
My routes have this path:
admin_orders GET /new/admin/orders(.:format) module/admin/orders#index
I tried to run this code in rails console and it works without errors:
r.url_for :controller => 'module/admin/orders', :action=> :index, :id=>42
=> "http://domain.com/new/admin/orders?id=42"
r.url_for :controller => 'module/admin/orders', :action=> :index
=> "http://domain.com/new/admin/orders"
My application is using Spree and this controller inherits Spree::Admin::ResourceController class

User proper url helpers ass follows
<%= form_tag(admin_orders_path, method: :get) %>

The answer is to put routes configuration in the same engine controller is using (For example Spree in this case):
Spree::Core::Engine.add_routes do
scope 'module/' do
namespace :admin, as: 'module_admin' do
resources :orders, controller: '/module/admin/orders',
only: [:edit, :index, :update]
end
end
and use:
<%= form_tag(module_admin_orders_path, method: :get) %>
or use url helpers with engine prefix:
<%= form_tag(main_app.admin_orders_path, method: :get) %>

Related

Custom update function routing for rails

I'm implementing a custom update function called update_status for my users controller. I need some help with the routing. what I want to do is update a status that only admins can access. I'm calling the update function via a form helper through the edit function in the users controller. This is my code for the form helper:
<%= form_for #user, :url => url_for(:controller => "users", :action => "update_status"), method: :put do |f| %>
<%= render "shared/error_messages", object: f.object %>
<%= f.check_box :admin %>
<%= f.label :admin %>
<%= f.check_box :editor %>
<%= f.label :editor %>
<%= f.submit "Save Changes", class: "btn btn-primary" %>
<% end %>
But when I click save changes all I get is this error
I want to route the action so that the user id can be resolved.
Controller action code:
def update_status
if #user.update_attributes(status_params)
flash[:success] = "User updated"
redirect_to #user
else
render 'edit'
end
end
Routes:
Transpub::Application.routes.draw do
resources :users do
member do
put 'update_status'
end
end
resources :papers
resources :comments
resources :reviews
resources :sessions, only: [:new, :create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
resources :subject_field, only: [:create, :destroy]
#get "users/new"
root "static_pages#home"
match "/signup", to: "users#new", via: "get"
match "/signin", to: "sessions#new", via: "get"
match "/signout", to: "sessions#destroy", via: "delete"
match "/help", to: "static_pages#help", via: "get"
match "/about", to: "static_pages#about", via: "get"
match "/contact", to: "static_pages#contact", via: "get"
match "/search_papers", to: "papers#index", via: "get"
match "/browse_papers", to: "papers#browse", via: "get"
In your routes files, look for the part that corresponds to the users controller and make sure that you have the following code
resources :users do
put :update_status, on: :member
end
That will declare the route. Another thing you have to update is the url of the form. Change the url to
form_for #user, :url => [:update_status, #user], html: { method: :put } do |f|

Rails: form for different controller

I'm developing a rails app with a landingpage. On the landingpage, the user can sign up for the app. For login, there is an extra view with an extra controller.
It looks like this:
views/landinpage/index.html --> sign up form
views/login/index.html --> login form
but I only want to have one controller
controllers/login_controller --> create new user from sign up form & check login data
so I have to get a connection between the landingpage view and the login_controller.
This is my attempt:
<%= form_for #login, :url => { :controller => "login_controller", :action => "create" }, :html => {:method => :post} do |f| %>
but it throws a route error:
No route matches {:controller=>"login_controller", :action=>"create"}
I already defined login resources in routes.rb, but it seems that the problem is elsewhere?
resources :logins
any ideas?
try this
class LoginsController < ApplicationController
def new
...
end
def create
...
end
...
end
in your route.rb file write
match '/login/create' => 'logins#create', :as => :create_login
or
resources :logins
in your console - write - rake routes and check your routes
then
<%= form_for #login, :url => create_login_path(#login) do |f| %>
I think your code should look like this:
<%= form_for #login, :url => { :controller => "login", :action => "create" }, :html => {:method => :post} do |f| %>
can't test this right now, but I believe the _controller part is not required.
Update:
Another thing that I'm using a lot and that works:
<%= form_for #login, :url => create_login_path(#login), :html => {:method => :post} do |f| %>
You may have to fix the create_login_path part to match your application's routes but that's how I usually define these views.
Try this
class LoginsController < ApplicationController
def new
...
end
def create
...
end
...
end
in your routes.rb file
resources :logins do
collection do
post :create
end
end
and in your views
<%= form_for #login, :url => create_login_path(#login) do |f| %>>
you can see the html form action part, you can see!
your config/routes has
resources :posts
namespace :admin do
resources :posts
end

Rails: Routing error

I have a button_to tag in my show.html.erb file.
<%= link_to 'Click HERE to open file', #user.image.url %><br/><br/><br/>
<%= label_tag(:q, "Parse CSV File:") %><br/>
<%= button_to 'Parse CSV', {:controller => "users_controller", :action => "process" } %>
<% end %>
Then I have this added to my users_controller.rb file
# GET /users/1/process
def process
puts 'To be Implemented'
end
Im getting an error in the routing file
No route matches [POST] "/assets"
This is how my routing file looks:
resources :users
resources :listings
What should I change. Im a bit confused, woould really appreciate some help.
Please correct your route and define like this
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
Then in route file
resources :users do
collection do
get: process
end
end
It will sure work
1) In view, use the controller name like 'users', not the 'users_controller' .
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
2) rails define few routes by default , but for other you need to define yourself .
Declare the routes like :
resources :users do
:member => {
:process => :get
}
end
Hope that help .

rails3 routes.rb

I have been working with rails3, here the view.html.erb form have one login button, so when i click on that button, gives no routes matches :controller => 'home', :action => 'login'. But i have put that in routes.rb. Why this happening?
view.html.erb
<%= form_tag( { :controller => 'home', :action => 'login' }, { :method
=> 'post'}) do %>
<%= text_field(:name, :name, :class => "span2",:placeholder =>
"Username") %>
<%= password_field_tag(:password, :password, :class =>"span2") %>
<%= submit_tag "Login", :class => "btn btn-primary" %>
<% end %>
**routes.rb**
resources :home
resources :home do
post :login, :on => :member
end
**homecontroller.rb**
class HomeController < ApplicationController
def login
end
end
You have defined "resources :home" twice, first declaration is useless and overrides second.
Since you used resources to define your routes (which is recomended) you should use the helper method generated, in this case its login_home_path instead of the old syntax { :controller => 'home', :action => 'login' }
First,
You declare resources :home two times.
try this way in your routes.rb
resources :home
match '/login', to: 'home#login'
and use login_path in submit tag.
I would prefere for login, logout, create Sessions controller
rails generate controller Sessions --no-test-framework
and for login create new method and for logout(signout) create destroy method

Rails 3 : Can't get form_for to work as a 'delete' following the RESTful achitecture => always giving a ROUTING ERROR

I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

Resources