I have the following routes:
create_admin_path GET /app/views/createAdmin/create_admin.html.erb(.:format) application#createAdmin
I have this in routes.rb
get "/app/views/createAdmin/create_admin.html.erb", to: "application#createAdmin", as: "create_admin"
I have this in the application_controller.rb
def createAdmin
end
and in the view I have a folder called createAdmin which has a file called create_admin.html.erb
in the create_admin.html.erb I have something like this:
<h1> testing is here </h1>
Yet I am receiving this error message:
No route matches [GET] "/app/views/create_admin/create_admin.html.erb"
What am I doing wrong ?
Thanks for your time
The views must match in controller name and view file name, that's if you're registering the route to application#createAdmin, then the folder must be application/ and the file createAdmin, in camel case as you've named the action and the route, so your file must be called createAdmin and must be located in a folder called application/.
/app/views/application/createAdmin.html.erb.
But, there are some things to add:
There's no need to use the ApplicationController to handle such operations, you can easily create a new controller and use it with that purpose instead polluting the ApplicationController.
You don't need to specify the full route to the view as the URI when registering a route, unless it's that specifically what want to do.
When working with Ruby use snake case.
Related
I am learning how to login from Rails and I wanted to know one thing:
I have several files which I want to show when a certain condition is met, in this case logging in will redirect me to another file called starter.html.erb
I am trying to redirect it through both the controller and the routes files and I get the following error:
No route matches [GET] "/app/views/usuarios/starter.html.erb"
Can you please tell me what I'm doing wrong? Thanks!
Controller portion:
redirect_to search_starter_path
routes.rb portion:
get "/search/starter" => redirect("/app/views/usuarios/starter.html.erb")
You have to use the controller#action syntax to redirect. Example: if your controller is usuarios_controller.rb you should have inside an action named starter. Then in your routes.rb put this entry:
get "search/starter" => 'usuarios#starter'
I am working on a Ruby on Rails project. In the project I generated a controller with a name controller1 which automatically gives me my view files (show, index, and others). This also gives me my url (http://www,website.com/controller1/).
But now I want to change the URL path to another one (http://www.website.com/controls). Is there a way I can do this without having to write route command for each file in the controller1 view folder.
Oh! And I will appreciate options better than the "Moved Permanently" redirect route command code.
Thanks in advance.
Add this to your routes.rb
resource :controls, controller: 'controller1' # add this line
resource :controller1 # possibly this is in your routes.rb already
Reference: Specifying a Controller to Use
I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.
I have an engine (developed by me / the company I work for) that we use on several different projects. I just converted it to work with rails 3.1 w/ assets pipeline and everything seems to be working... for the most part.
My problem is that I need to extend the functionality of the UsersController with a little bit of app-specific spice, but I'm not sure about the best way to do it. The engine doesn't define a Users#show action, but this app does need it, so I added to the routes file:
JobEngine::Application.routes.draw do
root :to => 'home#index'
resource :users, :only => [:show]
resources :jobs, :only => [:show]
end
Then in my application I created the UsersController:
class UsersController < MyEngine::UsersController
def show
end
end
Then I made a users/show.html.haml view, I've stripped it down to only show one of the problem lines:
= link_to "Somewhere", job_path(3)
This gives me an error that reads undefined method 'job_path' for #<#<Class:0x00000102d69900>:0x00000102d4ed30>
Which is bizarre because before I made my app's UsersController inherit from MyEngine::UsersController it worked just fine.
When I do rake routes in the console, there are these lines:
users GET /users(.:format) {:action=>"show", :controller=>"users"}
job GET /jobs/:id(.:format) {:action=>"show", :controller=>"jobs"}
I can alter the class definition to be:
class UsersController < ApplicationController
and then the link works just fine. However, the engine's controller MyEngine::UsersController already inherits from ApplicationController. I can put code into my app's ApplicationController (like a before_filter) and it will run as expected, so I know my class definition does ultimately hit my app's ApplicationController, why is the job_path helper not working?
When I change the show action to read:
def show
job_path(3)
end
I get the error:
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"jobs", :id=>3}):
app/controllers/users_controller.rb:9:in `show'
Which further confuses me because now it actually does recognize job_path as a method, but somehow the router isn't picking up where to go with all the correct parameters.
What am I doing wrong? What is the correct way to extend engine controller functionality? I saw the extending engine functionality question here.
And followed that code example, changing my class definition to instead re-open MyEngine::UsersController but I still get the exact same results concerning job_path(NUMBER)
UPDATE:
Ok I sort of figured out what's going on. Let's say your engine has a job_path route, and your application has a job_path route. If you're on a page that was accessed via an engine's controller, you can call the engine's helper with just job_path, but you can also call the main application's helper with main_app.job_path.
Likewise, if you're on a page accessed via one of your application's controllers, you access the engine's helper with my_engine.job_path and your own application's helper with job_path. This is assuming that you have something like mount MyEngine::Engine => "/my_engine", :as => 'my_engine'.
When you inherit an engine controller from your application, it then completely changes your route helpers to think you're in the context of the engine through the controller/view lifecycle. So to fix my problem all I really have to do is change it to be main_app.job_path(3) and it works.
I'm not completely satisfied with this solution because it feels a little...weird. Maybe I have a partial on this page that will be used on a separate non-inheriting page. Now the link helper will only work for one of the two pages, but never both =\ Am I missing something here...?
Try changing your Mount path in main app's routes with below,
mount MyEngine::Engine => "/"
This would solve your problem.
Though you can make this approach work, the semantics do not create a clean architecture. You can surmise this from the duplication of the Users controller - which implies that some User functionality is handled in the AppEngine, and some is handled in the parent app itself.
Instead, think about what functionality exists uniquely within the app, and which is packaged into the AppEngine gem. Perhaps with JobEngine, as you call it, your Users controller there is actually a UsersStatisticsController and, in the app, the controller there is the 'true generic' UsersController that handles CRUD, profiles, message queue, etc.
If you feel you must unify them into a single controller (rather than giving them distinct nomenclature), then you are best to create a Namespaced controller, where the various functionality can be conjoined thereby.
Even though this adds complexity, it's generally arguable that this is the most sound solution. Here's another post on SO about it
I'm tired of creating a new line in my routes.rb every time I add a new method in my controller. Is there a way in routes.rb to tell rails to accept any defined action in a given controller? I'm pretty sure I've done this before but can't remember how. I still need to explicitly specify the controller, however, because many other people use this routes file.
Thanks!
This is from the default generated config/routes.rb file
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'