I am getting a:
uninitialized constant ProfilesController::EUserPofile
error when trying to:
class ProfilesController < ApplicationController
def index
##profiles = EUserProfile.all
end
def preview
#profiles = EUserPofile.all
end
end
it works fine for index but for preview it crashes.
here is my route file:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get 'welcome', to: 'pages#home'
get 'profiles', to: 'profiles#index'
get 'login', to: 'login#login'
resources :profiles do
get 'preview', on: :member
end
end
There's a typo: EUserPofile -> EUserProfile
Related
The code is below.
#app/controllers/admin/feeds_controller.rb
class Admin::FeedsController < ApplicationController
def api_index
#routes.rb
Rails.application.routes.draw do
scope '/hoge' do
resource :feeds, only: [] do
collection do
get :api_index
end
end
end
end
My wish is that path is hoge/feeds/api_index and can execute api_index action in admin/feeds_controller.
The routes.rb is currently Routing Error.
Because the path is controller/feeds.
How can I call api_index action in controller/admin/feeds?
Thank you.
Error(Add)
I wrote the below
#routes.rb
Rails.application.routes.draw do
scope '/hoge' do
resource :feeds, controller: 'admin/feeds', only: [] do
collection do
get :api_index
end
end
end
end
Then, I got a error
undefined local variable or method `api_index' for Admin::FeedsController:Class
But I definitely wrote def api_index in app/controllers/admin/feeds_controller.rb
How Can I do that?
You can use namespace.
More info in the Rails guide: https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
#app/controllers/admin/feeds_controller.rb
class Admin::FeedsController < ApplicationController
def api_index
end
end
#routes.rb
Rails.application.routes.draw do
namespace :admin do
scope '/hoge' do
resource :feeds, only: [] do
collection do
get :api_index
end
end
end
end
end
I'm trying to export a docx file using caracal but I'm getting a routing error, but everything seems to be okay.
I did this 3 days ago exactly like now and worked, now I'm getting an error.
Routes.rb
Rails.application.routes.draw do
get 'grayscale/index'
devise_for :users, path: '', path_names: {sign_in: 'login', sign_out: 'logout', sign_up: 'registrar'}
resources :contratos
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'contratos#index'
get 'contratos/page'
end
contratos_controller.rb
class ContratosController < ApplicationController
before_action :authenticate_user!
before_action :set_contrato, only: [:show, :edit, :update, :destroy, :export, :page]
access all: [:show, :index], user: {except: [:destroy, :new, :create, :update, :edit]}, site_admin: :all
require './lib/generate_pdf'
# GET /contratos
# GET /contratos.json
def index
#contratos = Contrato.all
end
# GET /contratos/1
# GET /contratos/1.json
def show
end
# GET /contratos/new
def new
#contrato = Contrato.new
end
# GET /contratos/1/edit
def edit
end
def page
Caracal::Document.save(Rails.root.join("public", "example.docx")) do |docx|
# page 1
docx.h1 'Page 1 Header'
docx.hr
docx.p
docx.h2 'Section 1'
docx.p 'Lorem ipsum dolor....'
docx.p
end
path = File.join(Rails.root, "public")
send_file(File.join(path, "example.docx")
end
show.html.erb
<%= link_to 'Generate Docx', contratos_page_path %>
The full error
ActiveRecord::RecordNotFound in ContratosController#show
Couldn't find Contrato with 'id'=page
def set_contrato
#contrato = Contrato.find(params[:id])
end
This is a very common beginner issue which is due to the fact that routes have precedence in the order they are declared (thus the comment on top of routes.rb).
Since resources :contratos already defines a GET /contratos/:id route it will always match the request for GET /contratos/page to contratos#show. Rails does not assume that your ids are numerical when routing. These paths will all match the GET /contratos/:id route:
GET /contratos/1
GET /contratos/page
GET /contratos/page?foo=bar
GET /contratos/foo-bar-baz
GET /contratos/alksjd-usfiugi%-dfgd
But these will not:
GET /contratos/new # declared before the show route
GET /contratos/1/foo
GET /contratos/foo/bar
You can fix this by moving your custom route to the top:
get 'contratos/page'
resources :contratos
But there is a better Rails way of adding additional restful actions to a resource:
resources :contratos do
get :page, on: :collection
end
I'm getting this error.When I want to run te server on localhost:3000/api/v1/songs.json
Routing Error
uninitialized constant API::V1::SongsController.
Thats my routes.rb file:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :songs, only: [:index, :create, :update, :destroy]
end
end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
Path Match
api_v1_songs_path GET /api/v1/songs(.:format)
api/v1/songs#index
POST /api/v1/songs(.:format)
api/v1/songs#create
api_v1_song_path PATCH /api/v1/songs/:id(.:format)
api/v1/songs#update
PUT /api/v1/songs/:id(.:format)
api/v1/songs#update
DELETE /api/v1/songs/:id(.:format)
api/v1/songs#destroy
and thats my SongsController:
class Api::V1::SongsController < Api::V1::BaseController
def index
respond_with Song.all
end
def create
respond_with :api, :v1, Song.create(song_params)
end
def destroy
respond_with Song.destroy(params[:id])
end
def update
song = Song.find(params["id"])
song.update_attributes(song_params)
respond_with song, json: song
end
private
def song_params
params.require(:song).permit(:id, :name, :singer_name, :genre, :updated_at, :tag)
end
end
I'm going to copy how we have this running, cause I cannot see a direct difference between your code and our code....
routes.rb
constraints subdomain: Settings.subdomains.api do # you can ignore this
namespace :api do
namespace :v1 do
resources :maps, only: [] do
collection do
get :world_map
end
end
end
end
maps_controller.rb
module Api
module V1
class MapsController < BaseController
def world_map; end
end
end
end
routes output
world_map_api_v1_maps GET /v1/maps/world_map(.:format) api/v1/maps#world_map {:subdomain=>"api"}
The spelling is really crucial from what I can see.
The directort structure for us:
app/controllers/api/v1/maps_controller.rb
So check those points and this should work because it's standard Rails magic.
I can't access rails objects created in my controller in my view. This should print "test" in the view, but does not:
test_index.html.erb:
<%= label_tag(#test) %>
test_controller.rb:
class TestController < ApplicationController
def index
#test = "test"
end
end
routes.rb:
Rails.application.routes.draw do
get 'test/test_index'
root :to => 'home#index'
end
In your route file I believe you are missing the to: statement
Rails.application.routes.draw do
get 'test/test_index', to: 'test#index'
root :to => 'home#index'
end
I'm a beginner of RoR and this is my sample
class PagesController < ApplicationController
def welcome
end
end
in my route file
Rails.application.routes.draw do
root :to => “pages#welcome”
...
end
when I open powder
#config/routes.rb
root to: "pages#welcome"