I'm trying to write my first rails 3 gem - everything works well, except for routes - I can't seem to get them working. It's possible this is a very simple error - as mentioned, it's my first experience with engines. The gem itself is very, very basic - literally just one scaffold
My gem's config/routes file:
class ActionController::Routing::RouteSet
resources :frogs
end
...And when I try to start the server, I get the following error:
/home/john/.rvm/gems/ruby-1.9.2-p0/gems/cancandevise-0.1.0/config/routes.rb:3:in
<class:RouteSet>': undefined method
resources' for
ActionDispatch::Routing::RouteSet:Class
(NoMethodError)
Any suggestions much appreciated. At the present moment, the gem is nothing more than a very basic rails-generated 'frog' scaffold
Cheers,
- JB
#marcgg, I believe that's the syntax for a regular rails app, but I think he's talking about an engine.
#unclaimedbaggage, your engine/gem routes file should look like this:
Rails.application.routes.draw do |map|
resources :frogs
end
I made an example engine that touches on all the common setup issues I encountered when creating my first gem, you might find it helpful to reference:
http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/
I'm not sure if I get why you're using a routeset. What file did you show? Did you try this:
YourApp::Application.routes.draw do |map|
resources :frogs
end
More info here: http://asciicasts.com/episodes/203-routing-in-rails-3
Just wanted to add an alternative here, as I'm not sure #Keith Schact is doing it the conventional way, this worked for me:
MyEngine::Engine.routes.draw do
resources :frogs
end
then in the application that requires the gem:
mount MyEngine::Engine => '/my_engine', :as => :some_namespace
The url you will get is then:
http://myserver.com/some_namespace/frogs
Related
I am using this gem to translate my routes, I am working with two languages in my project, spanish and english
I translate my routes this way:
localized do
namespace :admin do
resources :events
end
end
that generates me:
/es/admin/events
/en/admin/events
I need to add translations in config/locales/*.yml
for example in es.yml
routes:
admin: administrador
events: eventos
That works fine, but the problem is that I have a .js that does post, put or get to specific routes, and that causes me error
$http.get('/admin/events/' + id)
when translating my route in .yml the previous route does not exist
How could I solve this?
What are RouteTranslator.config.generate_unlocalized_routes or RouteTranslator.config.force_locale set to in your application? If I look at the documentation for RouteTranslator, I see those are relevant to your use case.
When I run the rails server it returns with an error in index_html.erb. the code ran great for a few minutes then I came across this error.
routes.rb file
[
[
Rails uses the routes.rb file to locate the various resources of your application, so any resource you expect your users to access will have to be defined in routes.rb. You don't have a route defined for home_about_path.
From your code, I take it you have an about method in your home_controller.rb controller file. You have to have the following line in your routes.rb (add it under 'get home#index'):
get 'home#about' => 'home/about'
Adding this line should create a home_about_path and your link_to method should work.
Understanding routing is incredibly important to Rails development, so you should do some reading and make sure you have a good grasp of the fundamentals before pushing too far ahead.
We are updating our large web app and getting rid of our URL synonym system. For example, though we only use one model for 'advertiser', it is aliased to 'merchants' and 'sellers', using synonymous paths. Thus, /merchant/1 and /seller/1 use the same exact controller and action as advertiser/1.
We no longer need this functionality, but we do need for old URLs to correctly redirect users to the proper place (i.e. proper controller actions).
So far, I've tried:
1) reading http://guides.rubyonrails.org/routing.html - This lead me to try the following suggestions
2) match '/merchants' => redirect('/advertisers') - this didn't seem to work at all, though the syntax seems correct.
3) iterating over resources (this produces a TON of routes and may cause insecurities):
ADVERTISER_SYNONOYMS = ['affiliates', 'sellers']
ADVERTISER_SYNONYMS.each do |a|
resources :advertisers, :path => a, :as => :advertiser
resources :other_controller do
member do
get :test
match :generate_test
end
end
end
end
We use nested resources in our routes.rb, so I'm struggling with getting these synonymns to be recognized all throughout more complex URLs. If anyone dealt with this problem or has a more elegant suggestion, I'd be most grateful.
There are many ways to handle this, including additional routes in routes.rb like you've tried, or rewrites at the webserver level such as Apache or Nginx.
One solution I like is the rack-rewrite gem; it keeps routes clean and the rules are plain ruby. It provides a set of methods for configuring rewrites and redirects. For your situation, I would want to use permanent redirects. Though I haven't tested it out myself, something like the following may work for you:
# Gemfile
gem 'rack-rewrite'
# application.rb
config.middleware.use(Rack::Rewrite) do
r301 %r{/merchant|seller/(\w+)}, '/advertiser/$1'
end
First i created refinerycms application,
in side my application i can't call my own controller route,actions and view i got error like
undefined local variable or method `contacts_save_contact_path' for #<#<Class:0xafc9338>:0xb5467fc>
I have found myself having to put refinery.route_path with refinery url helpers, so in your case, refinery.contacts_save_contact_path might do the trick
You have to gain access to url_helpers of your actual app and it depends on the app's namespace. For example:
In config/routes.rb:
MyApp::Application.routes.draw do
resources :foos
mount Refinery::Core::Engine, at => '/'
end
And rake routes shows:
foos GET /foos(.:format) foos#index
refinery / Refinery::Core::Engine
You should be able to use:
MyApp::Application.routes.url_helpers.foos_path
the path have to be someone like this: Refinery::Core::Engine.routes.url_helpers.your_object_admin_your_object_path
main_app.contacts_save_contact_path should work.
I am following this routing tutorial for Ruby on Rails:
http://guides.rubyonrails.org/routing.html
It says that when I need to create a new url, I should make a route for it. So I did that.
I would like to have a url like this www.domain.com/fomats/formats.html.rb so I did something like this in the routes.rb file:
resources :formats
get "formats/index" #display all formats
Is that correct? For my index route, I also have something like this in my route.rb file: root :to => "home#index" - should I have something like this in the formats route?
Also, how do I create the actual controller? Do I make it by hand, or does rails somehow create the stub of it for me?
Right now I get this error:
missing :action
Does that mean I am missing the controller or something else?
Thanks,
Alex
As others have said, you should probably continue studying with other books or resources. These fundamental questions you are asking may become more clear the more you read.
Here are some quickfire hints that hopefully help you.
---
When you declare this in the routes.rb file:
resources :formats
You automatically get the following declaration for free, so you don't have to re-declare it:
get "formats/index" # Don't add this to routes.rb
---
URL's in rails look like this:
www.domain.com/formats
That URL would map to "formats#index"
---
To see what explicit routes have been generated, run this in your rails root directory:
rake routes
---
To create a controller:
rails g controller formats