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
Related
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.
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.
I am wondering what would be the best practise and approach to handle routing error for specific controller?
In my case i have 2 resources
resources :user
resources :special_user
I want that everything that throws routing error redirects back to that resources index, for example:
request to mydomain.com/users/blahblah, will redirect to mydomain.com/users
and same for special user mydomain.com/special_users/blahblah will redirect back to mydomain.com/special_users
What would be the best approach for this?
You can do:
resources :users do
# if you need to add new routes, add them before the catch all
get '*a', to: redirect('/users')
end
resources :special_users do
# if you need to add new routes, add them before the catch all
get '*a', to: redirect('/special_users')
end
An alternative is to do:
resources :special_users do
# if you need to add new routes, add them before the catch all
get '*a', action: :catch_all
end
and in your special_users controller, you defined the action:
def catch_all
# maybe set some flash message
redirect_to special_users_path
end
Thanks to #JiříPospíšil 's comment I realize you may have one more thing to check.
In your show actions (for users for instance):
def show
#user = User.find_by(id: params[:id])
if #user
# usual stuff here
else
# flash message?
redirect_to users_path
end
end
(I assumed you forgot to pluralize your resources)
I am making a login route and added this to the routes.rb resources :sign_in
I made a controller like this:
class Mobile::Sign_inController < ApplicationController
layout "mobile/application"
def get
respond_to do |format|
format.html
end
end
def index
respond_to do |format|
format.html
end
end
end
and it seems to get routed correctly, but my view file which is located here:
/app/views/mobile/sign_in.html.haml
which just has 1 line for test purposes:
%strong{:class => "code", :id => "message"} Hello Signin!
But when I go to the url: http://m.cmply.local:8800/signin in the browser, the screen is totally white with nothing rendered in the browser.
Any idea why this happens and how to fix it?
Thanks!
A few problems here:
Your controller name should be SignInsController, not Sign_inController. Consider changing your name to UserSessionsController or similar, since that better reflects the resource it represents. You can still specify an alternate name for the URL (such as sign_in).
Why is your controller namespaced under Mobile? Your routes given don't reflect that, but you don't seem to have provided them all. The route should probably be under a scope:
scope :module => "mobile" do
resource :sign_in
end
Since there is only "one" sign in, it should have its route declared resource :sign_in, and probably even resource :sign_in, :only => [:new, :create, :destroy], depending on what you want. This means that the index action no longer exists, and you probably want to replace it with the new action`.
There is no get action by default for RESTful resources, I'm not sure what you meant it to be, but it should be something else.
I am developing a app in ruby on rails for a local business. The pages are 'static', but changeable through a backend CMS I am building for them. Is there a best practice to creating a controller for static pages? Right now I have a sites controller with all static routes, like this.
routes.rb
get "site/home"
get "site/about_us"
get "site/faq"
get "site/discounts"
get "site/services"
get "site/contact_us"
get "site/admin"
get "site/posts"
or would I be better off creating member routes for the site controller like this without the crud, because a 'Site' will not need to have the CRUD.
resources :sites, :except => [:index, :new, :create, :update, :destroy]
member do
get :home
get :about_us
get :faq
get :discounts
get :services
get :contact_us
get :admin
get :posts
end
Or is there a best practice / better way? Any answers would be appreciated. Thanks
If the static pages list are not going to increase, then you can keep the list, but if you want a dynamic list like site/any_new_url , save the routes as
get 'site/:cms_page' => 'cms#show' # all requests matching site/any_page will go CmsController, show method
This will help reduce keep the routes from bloating, but the downside is you do not know what all routes are the valid ones. Your sample code can be
def show
#page_data = Page.find_by_page(:params[:cms_page])
end
show.html.erb
<%= #page_data.html_safe %>
Dunno yet if I consider this a best practice or an abomination but here is what I came up with when tackling the same problem.
My reasoning is that the site was providing some specified functionality (which doesn't really matter for this discussion) + a bunch of information about the organisation itself (about us, contact, FAQ, homepage blurb, whatever). Since all that data was really related to the organisation, an Organisation model seemed reasonable with each of those things as attributes. Here is the model:
class Organisation < ActiveRecord::Base
...validations stuff...
def self.attrs_regex
Regexp.new(self.attrs.join("|"))
end
def self.attrs
self.column_names.reject{|name| name =~ /id|created_at|updated_at/}
end
end
Then I use the attrs class method to generate routes based on the columns. This is in my routes.rb:
Organisation.attrs.each do |attr|
get "#{attr}" => "organisation##{attr}", :as => attr.to_sym
get "#{attr}/edit" => "organisation#edit", :as => "#{attr}_edit".to_sym, :defaults => { :attribute => attr }
post "#{attr}" => "organisation#update", :as => :organisation_update, :defaults => { :attribute => attr}, :constraints => Organisation.attrs_regex
end
The controller gets a little weird and I am not thrilled with the code here but here it is anyway. I need to make sure the attribute is set and available to the views so I can do the right thing there so I set it in the application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_attribute
def set_attribute
#attribute = action_name.parameterize
end
end
For the organisation controller I just set the #organisation variable to be the first and only row in the database in the before_filter and then let Rails do its usual magic of calling the method, failing, and rendering a view of the same name. The edit action just uses one view file to edit all the different attributes:
class OrganisationController < ApplicationController
before_filter :set_organisation
def edit
authorize! :edit, #organisation
#attribute = params[:attribute].parameterize
end
def update
authorize! :update, #organisation
#attribute = params[:attribute]
respond_to do |format|
if #organisation.update_attributes(params[:organisation])
format.html do
redirect_to "/#{#attribute}", notice: t('successful_update')
end
format.json { head :ok }
else
format.html { render action: "edit" }
end
end
end
private
def set_organisation
#organisation = Organisation.first
end
end
So that is where I ended up. Like you I hit up SO to tap into the seething mass of genius here but ended up with disappointing results. If there is something better out there I am still hoping to find it.
What I like about what I did is that routes are automatically generated based on the structure of the organisation table.
What I don't like about what I did is that routes automatically generated based on the structure of the organisation table.
I know I will pay for that design decision when I have to deal with i18n routing and there are probably a thousand other reasons that this is a bad idea that I have yet to discover but for the moment I have a happy client.
In the end this is not a suggestion that you should do this, but I am hoping to give you more than I got so you can advance your thinking on this and hopefully end up a little closer to that best practice.
If you are going to construct a CMS, which likely connects to a database, and allow your customer to change the text on the pages of their site, I would not recommend using static pages. In Rails terms, a static page would refer to creating html files in your /views/pages directory. If you go this route, then you're walking outside of the way that Rails was designed.
I believe that what you want to do is create tables in the database that correspond to and store the data for your posts, etc. You can pull information into the controller from the model that it corresponds to and then user a view to display the data. You can create a layout for these pages and then create controllers for each of the pages that you add.
As far as routes, I would recommend using the following:
map.resource :controller_name
you then would add the code that displays the information from the CMS in the corresponding show controller action and view for each page.