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.
Related
I am trying to create a scaffold called 'Pages'.
So far everything is fine but the page structure now needs to change where it currently is:
http://0.0.0.0:3000/pages/the-page-name
What I need to do now is have this instead:
http://0.0.0.0:3000/the-page-name
In my routes.rb I have this:
resources :pages
This obviously maps all routes within the model to this base but I want to hide this.
Is it entirely possible?
Thanks,
Taken from the Rails Routing Guide, you could do this:
get '*pages', to: 'pages#show', format: false
I would recommend you make it the very last route you have, since the Rails router matches the request with the first route, and having a wildcard early in your routes file will end up clobbering all your other routes/resources.
You can specify a path
resources :pages, path: ''
I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)
"Routing Error
No route matches [GET] "/suggestion-boxes"
In my routes.rb file I have:
SuggestionBoxApp::Application.routes.draw do
resources :suggestion_boxes
end
This is what I get when I run rake routes:
suggestion-box-app$ rake routes
suggestion_boxes GET /suggestion_boxes(.:format) suggestion_boxes#index
POST /suggestion_boxes(.:format) suggestion_boxes#create
new_suggestion_box GET /suggestion_boxes/new(.:format) suggestion_boxes#new
edit_suggestion_box GET /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
suggestion_box GET /suggestion_boxes/:id(.:format) suggestion_boxes#show
PUT /suggestion_boxes/:id(.:format) suggestion_boxes#update
DELETE /suggestion_boxes/:id(.:format) suggestion_boxes#destroy
However, if I modify my routes file to
SuggestionBoxApp::Application.routes.draw do
get "suggestion-boxes" => "suggestion_boxes#index"
end
Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.
I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.
Any insights would be very much appreciated.
The error is that you are not renaming the REST route, instead the controller one.
Try declaring
resources :suggestion_boxes, :as => "suggestion-boxes"
in your config/routes.rb file.
Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding
I have a resource in a Rails 3.2 app called "news". I know it is not the best name to use for a resource but I'd prefer not to change it.
I have an issue with the routing since new_news_path doesn't work.
How can I define a custom route for the new action? something like unused_news_path for news#new?
thanks for your help.
Try this in your routes.rb:
match "unused" => "news#new"
When you do a rake routes | grep unused, you will get the following output:
unused /unused(.:format) news#new
So you now can use unused_path on your views and controller to get to the correspondent action.
Here is a app contorller directory from Rails project
doing a self study for rails, but from what I understand if I create a directory in the app folder then I have to do the complete the routes files with a match that route like:
match "/editor/usynkdataeditor/saveusynkeditor",
Question to the community is there a better way that I can define different directory structure for a specific workflow or is it safe to define all the controllers in parent controllers directory.
If you create additional directory in controllers directory, you are effectively namespacing your controllers.
So this controller would be:
class Editor::UsynkdataeditorController < ApplicationController
def saveusynkeditor
end
end
As far as routes are defined, you can do something like:
MyApplication::Application.routes.draw do
namespace :editor do
get "usynkdataeditor/saveusynkeditor"
end
end
Whish will give you route:
$ rake routes
editor_usynkdataeditor_saveusynkeditor GET /editor/usynkdataeditor/saveusynkeditor(.:format) editor/usynkdataeditor#saveusynkeditor
Or, preferably just use restful routes instead of saveusynkeditor like this:
MyApplication::Application.routes.draw do
namespace :editor do
resources :usynkdataeditor do
collection do
get :saveusynkeditor
end
end
end
end
when you will get:
$ rake routes
saveusynkeditor_editor_usynkdataeditor_index GET /editor/usynkdataeditor/saveusynkeditor(.:format) editor/usynkdataeditor#saveusynkeditor
editor_usynkdataeditor_index GET /editor/usynkdataeditor(.:format) editor/usynkdataeditor#index
POST /editor/usynkdataeditor(.:format) editor/usynkdataeditor#create
new_editor_usynkdataeditor GET /editor/usynkdataeditor/new(.:format) editor/usynkdataeditor#new
edit_editor_usynkdataeditor GET /editor/usynkdataeditor/:id/edit(.:format) editor/usynkdataeditor#edit
editor_usynkdataeditor GET /editor/usynkdataeditor/:id(.:format) editor/usynkdataeditor#show
PUT /editor/usynkdataeditor/:id(.:format) editor/usynkdataeditor#update
DELETE /editor/usynkdataeditor/:id(.:format) editor/usynkdataeditor#destroy
There is a really good explanation http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing of what you are trying to achieve in rails guides.
Finally, to answer your question:
Better way? Well it's up to your preferences. How do you like your code organized? You can use namespacing but you don't have to. However,
at the same there is nothing wrong with having all controllers in parent controller directory.
This falls under Namespacing and it's generally considered the best approach to do what you're trying to do. Check it out.
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