Apologies for the basic question, but I'm trying to create an endpoint so I can a TranslationApi in my backend via my VueJs frontend via Fetch, so I need to make an endpoint I can insert. I'm attempting to create a route to make that happen, however when I run bin/rails routes | grep CcApiController I receive the following error:
ArgumentError: 'CcApiController' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
I've read the documentation linked, but I'm not managing to fix this, can someone explain where I'm going wrong here? I'll link the files I've changed below:
cc_apis_controller.rb
module Panel
class CcApisController < CcenterBaseController
def index
run Ccenter::Adapters::Zendesk::TranslationApi.call(2116449)
end
end
end
panel_routes.rb
def draw_api_routes
resources: CcApiController
end
routes.rb
Rails.application.routes.draw do
resources :CcApiController, only: [:index]
end
API method I need to create Route for:
def make_request
response = Faraday.post('https://api.deepl.com/v2/translate', auth_key: '', text: #final_ticket, target_lang: 'DE', source_lang: 'EN')
if response.status == 200
body = response.body
message_element = body.split('"')[-2]
return message_element
else
raise InvalidResponseError unless response.success?
end
end
The answer to that is pretty simple.
Route names are snake_case, and match the controller's file name just omit the _controller suffix. In your case it is
Rails.application.routes.draw do
namespace :panel do
resources :cc_apis, only: %i[index]
end
end
For more info check this documentation and this article.
Related
I'm trying to set up dynamic routing in a Rails 5.1.4 api. Originally, I had all my dynamic routes pointing to a single action, which works just fine. The problem came when I attempted to use the swagger-docs gem, which requires a separate action for each entry in order to generate the necessary json for swagger ui. So, I figured I could simply dynamically create the methods and point the routes at them. As far as I can tell, the methods are not being created and I don't understand why. Even if I drop my debugger into the loop that cycles through my sections, it never gets triggered. I deeply appreciate any help on this.
routes.rb
Section.active.all.each do |section|
get "/#{section.url}", :to => "sections##{section.url}", defaults: { id: section.id }
end
sections_controller.rb
class SectionsController < ApplicationController
Section.active.all do |section|
define_method :"#{section.url}" do
#some things
end
end
end
development.rb
config.eager_load = true
If I call:
SectionsController.action_methods
=> #<Set: {}>
I ended up just making the section_url a param, pointing everything to the show action and adding the possible section urls in the docs description.
routes.rb
get "/:section_url", to: "sections#show"
#must be at the end of the routes definitions
sections_controller.rb
swagger_api :show do
summary "Returns Section Items"
param :path, :section_url, :string, :required, Section.active.pluck(:url).join(", ")
response :ok
response :not_found
end
could one advise me how to get a url like this in rails
http://www.example.com/users/5/ian
i tried the below but unsure:
route file:
devise_for :users
resources :users do
resources :socials
end
get '/users/:id/:firstname', controller: 'users', action: 'show'
users_controller.rb
def show
#user = User.find(params[:id], params[:firstname])
end
If you are trying to achieve 'friendly urls' then I suggest using this:
You don't have to create a special route:
get '/users/:id', controller: 'users', action: 'show'
Instead you have your model overwrite the to_param method:
class User
...
def to_param
"#{id}-#{firstname.try(:parameterize)}"
end
...
end
The url helper calls to_param to build the urls. If you overwrite it this way, you will receive a url like this:
http://localhost:3000/users/1-artloe
The rails find method calls .to_i on the params[:id] which, thankfully, interprets strings as number until it arrives at a character that can't become a number.
Examples:
'123abcde'.to_i # 123
'123-asdf'.to_i # 123
'asdf-123'.to_i # 0
So except for overwriting to_param, you don't have to do anything.
Try replacing this
def show
#user = User.find_by_id_and_firstname(params[:id], params[:firstname])
end
If what you are trying accomplish is "friendly urls" you would do it by:
# GET /users/1
# GET /users/joe
def show
#user = User.find_by!('id = :x OR firstname = :x', x: params[:id])
end
However you must ensure that property you are using in URLs is URL safe and unique. Usually a separate username or slug field is used.
Nothing special is needed in terms of routes.
These gems provide "friendly urls":
stringex
friendly_id
In my rails application I add an "api" area with controllers
In the route.rb file
I add the area
namespace :api do
#get "dashboard/sales_rate"
end
The controllers Class:
class Api::DashboardController < Api::ApplicationController
before_filter :authenticate_user!
def user_service
render :json => {"user_id" => current_user.id}
end
The Path is:
app/controllers/api/dashboard_controller
My question is if I can that the route take all action
for example /api/dashboard/user_service
and I will not add for each route row on the route.rb page
/api/{controller_under_api_namespace}/{action}
You can do with some meta programming sprinkle!
Api.constants.each |c|
c.action_methods.each do |action|
get [c.controller_name, action].join('/')
end
end
This method limits only to GET request. You can overcome it with RESTful routing.
Api.constants.each |c|
resources c.controller_name.to_sym
end
Hope, that helps. :)
I try add the code on the route.rb file
and I got this error
This is my file
But before trying to fix this part of code, I want to know if it's can change the performance or the calls to those pages?
If it not good for the performance I leave this option.
Hey guys i probably have a simple problem which annoys me for 2 hours now.
I try to set up a menu_item_icon which is linked to one of my controller actions.
So far every of these menu_items work. But there is one where I always get the failure message 'Controller_Action path not found' and I am wondering why this is happening.
Here are some code-snippets from
a) The definition of the controller_action itself
b) The route in the routes.rb
c) The menu_item_icon link on some of my views
a) Definition of action in controller sells_controller.rb
def manage_sell
#stored_sells = SaveSell.all
respond_to do |format|
format.html{render 'manage_sells',:layout=>false}
end
end
b) The route for action manage_sell in my routes.rb
resources :sells, :only=>[:show,:new,:create] do
[...]
get :manage_sell, :on=>:collection
[...]
end
c) menu_item_icon link inone of my views
[...]
=menu_item_icon('m_sells','Manage Sells'),sells_manage_sell_path
[...]
So what is going wrong?
The rake routes gives the name you have to use, so in your case you have to write manage_sell_sells_path.
I do have a comment regarding naming: I would prefer the simpler manage and then it would all make sense. If you define the route to be on a member, the path would be manage_sell_path.
So my guess is your route definition should be
resources :sells, only: [:show, :new, :create] do
get :manage, on: :member
end
as the naming now seems to imply you are "managing" a single sell.
I think it will be
manage_sell_sells_path
instead of
manage_sells_sell_path
Because your action is
manage_sell
and controller is
sells
Though there is no action named 'manage_sells' it raise no 'Controller_Action path not found' errors
I'm using Rails 3.0 and I have the following in my routes.rb files:
scope "/:location" do
resources :agents
end
So I can do this:
agents = Agent.where("location = ?", params[:location])
(There might be a better way to do this..? Mostly I want URLs like: myurl.com/london/agents)
Anyway, my problem is with the tests (which I'm still learning), how can I make this work with the scope:
class AgentsControllerTest < ActionController::TestCase
test "should get index" do
get 'index'
assert_response :success
end
end
It just gets a route no found error.
Any help would be great, thanks.
Because you have scoped your resources, you don't actually have direct access to the agents_controller via any actions. So calling get 'index' is really just trying to access /agents. If you tried this in your browser, it would fail as well.
To get a success you'll have to supply a location param like so:
get 'index', { :location => 'hawaii' }
Hope that helps.
Edit: If you want additional access to the agents_controller (meaning location is optional) simply add a top-level resource for it in your routes file:
resources :agents