I have an admin namespace and a scaffold of companies placed out of the admin namespace.
I wanted to put the companies into admin namespace
Then I put the companies_controller into admin directory and changed the definition to
class Admin::CompaniesController < Admin::AdminController
and put the companies views into the admin directory in /app/views/
and put the companies_helper into admin directory and now it looks as follows:
module Admin::CompaniesHelper
end
The namespace in routes.rb:
namespace :admin do
root :to => "companies#index"
resources :companies
end
When I go to localhost:3000/admin I get this error:
undefined method `company_path' for #:0xb696b408>
Now please tell me how to edit the links to make the links work properly?
When you moved the controller in to the admin namespace you changed routes to the links created in the scaffolded templates. For example if your templates use company_path the links would change to admin_company_path.
To view the routes within your application at any given point in time, run "rake routes" from the command line within the root of your rails application. This will show you all the routes within you application
Since company is under the namespace admin you have to prefix the path with admin.
Like so:
admin_company_path(#company)
See this Rails guide for more info on Rails routing and namespaces.
I got the kind of ugly solution but it works. I generated a new scaffold but differently:
rails generate scaffold Admin::Companies
instead of
rails generate scaffold Companies
but I still don't understand how the helpers make url for the resources :(
Related
For some reason default url for
sort_link is always pointing to /orders/populate?controller=&action=
I want it as whatever default route i have configured on routes.rb
if sort_link is a route you defined yourself then try using this
"main_app.sort_link" =)
Take a look here from more info
Adding Routes to Rails' Spree E-Commerce
I'm using devise in my rails3 project. I need to redirect user to different page once logged-in. Without overriding sessions controller how can I do this?
Question looks like a duplicate of https://stackoverflow.com/a/12854498/790737
Try putting this in you ApplicationController:
def after_sign_in_path_for(resource)
different_page_path # this should be a path helper
end
I assume you know how to use path and url helpers, and that you can use
rake routes
to list them. Good luck.
I did
rails generate controller home index
But it adds this line to my routes.rb
get "home/index"
I thought Rails could deduce controller/method from the URL automatically? Why do I need to specify every get/post page?
Here's my complete routes.rb file:
Callisto2::Application.routes.draw do
root :to => "home#index"
resources :assets
end
root "/" works fine. so does /assets/*.
What's the problem with /home/index? I get the error:
Routing Error
No route matches [GET] "/home/index"
Try running rake routes for more information on available routes.
rake routes (run as apache user) gives me the following output:
root / home#index
Thanks for any clarifications. Not sure what I'm missing.
Edit: I didn't make this clear: I manually removed get /home/index from routes.rb to keep that file clean.
Rails used to add the so called catch all route at the bottom of your routes file:
match ':controller(/:action(/:id(.:format)))'
There was nothing 'automatic' or magical about these urls, just that every rails app started out with this route in their routes.rb
This has fallen out of favour, at least partially because it makes everything accessible over get, whereas
resources :books
Adds each route with the appropriate http verb. Listing routes explicitly is also a lot less verbose than when rails started out.
If your controller is home and the action is index your path is just /home.
You can find more information here.
I followed the basic steps to add authentication to Rails using Devise from their page, but every time I try to visit a default page (such as the Sign In or Sign Up pages), I get:
Routing Error
No route matches {:controller=>"devise/Home"}
This happens whether I link to the page in a view using
link_to('Register', new_user_registration_path)
or just visit "/users/sign_up".
This is a different error from when I visit a page with no route defined (No route matches [GET] "/users/bad_example"), and
devise_for :users
is already present in my routes.rb. I have even tried generating views (rails g devise:views) to no avail. It looks like Devise isn't generating/using a controller or some such. How do I go about fixing this?
Here are some files that may help:
routes.rb
rake routes output
It turns out the problem was not with Devise, but with my routes file. I have fixed my routes file as seen at http://guides.rubyonrails.org/routing.html and now this works properly.
I am planning to create an app in rails but first I want to make a launch page. Having never made a launch page I am curious as to how others are doing it?
Do you creae a small rails application with controller and model that just collects email addresses? and then deploy the rails app? I'd prefer this way but it seems like an overkill to deploy a rails app just for a launch page...?
Also, how do you modify the routes file so that if users type anything after the url then only page that shows up is the laungh page.
Meaning, if my launch page is at http://mycoollaunchpage.com then if users mess around and type http://mycoollaunchpage.com/lkjlkjljk then it should redirect back to http://mycoollaunchpage.com
Your idea sounds good. Just a page with an email signup form would work well.
To redirect back to your home page, make a route glob in your routes.rb file, and have an action in your controller that just redirects back to your root.
# in routes.rb
match "*whatever", :controller => 'pages', :action => 'redirect_to_root'
# in your pages_controller.rb file
def redirect_to_root
redirect_to "/"
end
There is an awesome rails plugin available for this very requirement of yours ;)
https://github.com/vinsol/Launching-Soon/