I'm trying to use a bit of code from a Ruby application in Rails.
The application in question is not in Rails and has two views. I copied a part of the code and pasted it in a controller in Rails:
get '/' do
erb :index, :locals => {:item_id => item_id, :access_token => access_token}
end
And I'm getting the error:
ActionController::RoutingError (undefined method `get' for XXXcontroller)
I'm not sure how I'm supposed to interpret this bit of code in a controller in Rails.
You need to start with an actual tutorial/book and learn Rails. You can't assemble a rails app out of random snippets that you have no understanding of.
That code is from Sinatra which is designed with simple applications in mind and where your routes and controllers are mushed together into a single file. Rails and Sinatra code is not interchangeable*.
If you want to define a route in Rails for / (the root path). You define it like so:
# config/routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
end
And then declare the corresponding controller:
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
end
end
And a view:
<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>
See:
Getting Started with Rails
Routes in a Rails app are located in the routes.rb file in the config directory. Your route might look something like this:
get '/index', to: 'controller#index'
This says execute the index action of the controller controller. That method (index) is associated with a ‘get’ request.
Related
When I hit this link:
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
I get this error:
uninitialized constant InvitationsController
The method in InvitationsController looks like this:
def sumbit_invitation
#invi = #invitations.find(params[:id])
#invi.update_attributes(accepted: true)
end
and routes is:
resources :invitations do
put :sumbit_invitation
end
I propose you can follow the below steps to resolve your problem:
1/ run command rake routes to make sure your routes are correct?
In my opinion, it should be:
resources :invitations do
member do
put :sumbit_invitation
end
end
2/ Make sure you URL is correct
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
should be changed to
link_to("Accept", invitations_sumbit_invitation_path(invitations), method: :put)
Using rake routes to get correct path
4/ Make sure that your controller InvitationsController worked
P/S: It is better if you share your routes.rb file or logs rake routes
You controller is in the module ControlPanel and lives in the control_panel subfolder of your app's controller's folder, but your route doesn't have a corresponding controll_panel namespace defined.
Add a controll_panel namespace to your routes:
namespace :controll_panel do
resources :invitations do
put :sumbit_invitation
end
end
This will make the invitation_sumbit_invitation_url invalid and you will have to change it to something like admin_invitation_sumbit_invitation_url or invitation_sumbit_admin_invitation_url. From the top of my head I am not sure how Rails handles the naming. Use rake routes in your console to get a list of all available route names in your app.
I this context I advise to read: The Rails Guide: Routing
I'm trying to learn Ruby on Rails, from here: http://guides.rubyonrails.org/getting_started.html , But when I try to set the root for the route up in Section 4.3, although the route is recognized, when I run the server and localhost:3000, it still thinks there's no root route. What could the problem be? I'm using Ruby 1.9.3.
Routes
Without your routes file, or even the error which is being presented, I'll just give you what you need to get the "root" route working:
#config/routes.rb
root "application#index" #-> syntax - "controller#action"
This has to be backed up with the appropriate controller action:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def index
end
end
It also needs the appropriate views:
#app/views/application/index.html.erb
This is the root page!
I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
namespace namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin. That's what admin/admin#index means. :)
According to rubyonrails.org guide on routing you should be able to do something like this.
it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
on rails 4 in routes two roots not permited now
use
get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index and other methods to be in straightforward AdminController controller in usual directory, views to go in views/admin etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end
So I'm just beginning with RoR and figured I do a basic blog with API endpoints aswell. The problem is that my api requests seem to be routed to the wrong controller,
I have the following as my routes.rb
Blog::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :articles
end
end
end
I also have controllers/api/v1/articles_controller.rb, which has the following content:
module API
module V1
class ArticlesController < ApplicationController
respond_to :json
def index
respond_with Article.all
end
end
end
end
My logic says that when I hit http://localhost:3000/api/v1/articles, this should be the Controller to respond, however the actual Controller that responds is the one in the root of controllers (controllers/articles_controller.rb) and not the one in the /api/v1 path. When I remove the Controller that actually responds, I'll get uninitialized constant Api::V1::ArticlesController instead.
Even rake routes gives me the expected routes, however actually hitting those endpoints fails. Output of rake routes is the following:
api_v1_articles GET /api/v1/articles(.:format) api/v1/articles#index
POST /api/v1/articles(.:format) api/v1/articles#create
new_api_v1_article GET /api/v1/articles/new(.:format) api/v1/articles#new
edit_api_v1_article GET /api/v1/articles/:id/edit(.:format) api/v1/articles#edit
api_v1_article GET /api/v1/articles/:id(.:format) api/v1/articles#show
PUT /api/v1/articles/:id(.:format) api/v1/articles#update
DELETE /api/v1/articles/:id(.:format) api/v1/articles#destroy
The only similar question I found on SO is nested namespace route going to wrong controller however, there's no accepted answer there and it's been a year. Maybe another attempt will help resolve this issue
Your module is API, but Rails is looking for Api. Ruby's modules are case-sensitive.
i have a simple cms on ROR 3.2.
with this folder scheme:
app |controllers |my controllers
but i wanted to have an "admin" section where i could have some controllers too.
so i created
rails generate controller admin/Users
app | controllers |admin & my admin controllers
so my file is:
users_controller.rb
class Admin::UsersController < ApplicationController
def index
render(:text => "sou o index!")
end
def list
render(:text => "sou o list")
end
end
On my routes i have:
namespace :admin do
resources :users
end
match ':controller(/:action(/:id))(.:format)'
Im new to rails and i cant figure out the solution. Cant find it anywhere.
The PROBLEM is
i try do acess:
http://localhost:3000/admin/users/list
and i get this error:
Unknown action The action 'show' could not be found for
Admin::UsersController
You seem to not have an understanding of how Rails's RESTful routing works by default. I recommend reading the Resource Routing section of the Rails Guides. By default, when using resources in your routes, the show action is what is used to display a particular model record. You can customize this behavior to an extent in that you can change the URL that for the show action, but not the method name in the model:
resources :users, :path_names => { :new => 'list' }
If you are going to use RESTful routing (which you should), you should remove the default route (match ':controller(/:action(/:id))(.:format)'). Also, you can run rake routes at any time from the terminal to see details about your current routing configuration.
Your on the right track, however, there are a few more steps involved to complete your solution for a backend admin CRUD section. Check out the following example of how to create it yourself:
https://stackoverflow.com/a/15615003/2207480