Rails adding a random path to URLs - ruby-on-rails

So when the link to my website is clicked (not typed), rails app adds paths to url like
http://www.drolle.co/NgOLZ/posts/187
the "NgOLZ" is added and it's unique every time.
I have no idea why. Here's my code:
# Define root URL
root 'pages#index'
resources :posts do
resources :comments do
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
resources :replies do
member do
put "like", to: "replies#upvote"
put "dislike", to: "replies#downvote"
end
end
end
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
put "report", to: "posts#report"
end
end
And in the logs
ActionController::RoutingError: No route matches [GET] "/NgOLZ/posts/187"
the extra letters are added even with http://drolle.co
I did have an error with a missing nodejs module so I added universalify, may be this has to do with it? I don't really know.
Thanks for helping.

Related

Rails nested routes not working as expected

I have nested resources in my routes like so. These work perfectly on my other rails 5 app, but not on my rails 6 app. I cannot figure out why it recognizes only the first level of nested stuff.
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
resources :notations
end
Here is what rake routes gives me:
blogs_user GET /users/:id/blogs(.:format) users#blogs
like_blog PUT /blogs/:id/like(.:format) blogs#upvote
dislike_blog PUT /blogs/:id/dislike(.:format) blogs#downvote
blog_comments GET /blogs/:blog_id/comments(.:format) comments#index
POST /blogs/:blog_id/comments(.:format) comments#create
new_blog_comment GET /blogs/:blog_id/comments/new(.:format) comments#new
edit_blog_comment GET /blogs/:blog_id/comments/:id/edit(.:format) comments#edit
blog_comment GET /blogs/:blog_id/comments/:id(.:format) comments#show
PATCH /blogs/:blog_id/comments/:id(.:format) comments#update
PUT /blogs/:blog_id/comments/:id(.:format) comments#update
DELETE /blogs/:blog_id/comments/:id(.:format) comments#destroy
PUT /blogs/:id/like(.:format) comments#upvote
PUT /blogs/:id/dislike(.:format) comments#downvote
notations GET /blogs/:id/notations(.:format) notations#index
POST /blogs/:id/notations(.:format) notations#create
new_notation GET /blogs/:id/notations/new(.:format) notations#new
edit_notation GET /blogs/:id/notations/:id/edit(.:format) notations#edit
notation GET /blogs/:id/notations/:id(.:format) notations#show
PATCH /blogs/:id/notations/:id(.:format) notations#update
PUT /blogs/:id/notations/:id(.:format) notations#update
DELETE /blogs/:id/notations/:id(.:format) notations#destroy
On my other app, for example, it would produce
/blogs/:blog_id/comments/:id/like
I make a copy of your routes and replicated in two apps (Rails 5 and Rails 6) and both produced same routes (without three nested level). If you want the /blogs/:blog_id/comments/:id/like route, you must do a small change.
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments do
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
end
resources :notations
end
You are missing the "do" "end" block syntax
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments do # here
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
resources :notations
end # and here
end
Anyway, more than two levels of nesting is discouraged by the rails guidelines.

Ruby on Rails Devise id='sign up'

So a few people have had this problem, but none of their code actually matches mine so their solutions aren't working.
Basically, whenever I try to access /users/:id it passes in the params correctly {'id' => '1'}, but then the page errors out on 'no user with id='sign_out'
My routes:
Rails.application.routes.draw do
devise_for :users, path: 'devise'
get 'sessions/new'
get 'intro', to: 'index#intro'
root 'index#intro'
authenticated :user do
root 'index#homepage', as: :authenticated_root
end
devise_scope :user do
get '/start', to: 'devise/sessions#new', as: "login"
get '/devise/sign_out', to: 'devise/sessions#destroy', as: "logout"
get '/signup', to: 'users#new', as: "signup"
end
get '/forgotPassword', to: 'index#forgotPassword'
get '/homepage', to: 'index#homepage'
get '/meetUs', to: 'index#meetUs'
get '/makeSuggestion', to: 'index#makeSuggestion'
get 'profile', to: 'index#profile'
resources :suggestions
resources :rewards
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Code in controller erroring out:
# GET /users/1
# GET /users/1.json
def show
#users = User.find(params[:id])
end
I'm pretty sure the issue has something to do with the routes thinking /users is both a resource of mine and of devise. But I don't know how to tell it to use the default route rails made as opposed to the one devise made.
If anyone could help, and try and explain why it's happening, that'd be great. Thanks in advance!
edit: Picks of the error and trace

Weird path_helper error when using english locale

I am experiencing a weird error that I no longer want to work around but understand.
When recently adding an english version of our homepage, I found that I can't generate the pathes as I do for the german locale:
2.3.0 :023 > I18n.available_locales
=> [:de, :en]
2.3.0 :019 > apps_path(locale: :de)
=> "/de/apps"
2.3.0 :020 > apps_path(locale: :en)
ActionController::UrlGenerationError: No route matches
{:action=>"apps", :controller=>"pages", :locale=>:en} missing required keys: [:locale]
As I said, I was able to work around that, by using either url_for or – when sticking with our example apps_en_path.
But I wanna know what causes this weird problem.
EDIT
A git bisect session helped me to find out that this happens since I added the route-translater gem to my project. Besides this issue it works fine for us.
I had this settings (in development.rb):
# RouteTranslator.config
config.force_locale = true
config.locale_param_key = :locale
My routes.rb:
require_relative "#{Rails.root}/app/route_constraints/can_access_devops"
Rails.application.routes.draw do
get 'health', to: 'health#show'
# Redirect requests with trailing 'null'
get '/*path/null', to: redirect('/%{path}')
namespace :admin do
root to: redirect('/admin/magazine_articles')
resources :jobs
resources :users
resources :magazine_categories
resources :magazine_articles
resources :media_assets
resources :advertisements do
member do
get 'preview'
end
end
namespace :devops do
authenticate :user do
constraints CanAccessDevOps do
mount PgHero::Engine, at: '/pghero'
end
end
end
end
scope '/:locale', locale: /#{I18n.available_locales.join("|")}/ do
root to: 'pages#home'
localized do
devise_for :users, controllers: { sessions: 'sessions' }
# redirect terms of use to cockpit; this is required because
# the packaging has a link that should point to cockpit
get 'home', to: 'pages#home'
get 'imprint', to: 'pages#imprint', as: 'imprint'
get 'lottery', to: 'pages#lottery', as: 'lottery'
get 'privacy', to: 'pages#privacy', as: 'privacy'
get 'terms', to: 'pages#terms', as: 'terms'
get 'faq', to: 'pages#faq', as: 'faq'
get 'declaration-of-conformity', to: 'pages#declaration_of_conformity', as: 'declaration_of_conformity'
# Contact Page / Support Cases
resource :support_cases, only: [:create, :new], path_names: { new: 'new_support_case' } do
get :success, on: :collection
end
# redirect using the url helper to respect route localization. ugly but it works
get 'kontakt', to: redirect { |_, _| Rails.application.routes.url_helpers.new_support_cases_path }
# Workshop
get 'workshop', to: 'pages#workshop', as: 'workshop'
get 'autofit', to: 'pages#autofit', as: 'autofit'
# App-related
get 'app', to: 'pages#app', as: 'app'
get 'apps', to: 'pages#apps', as: 'apps'
get 'cars', to: 'pages#cars', as: 'cars'
get 'roadmap', to: 'pages#roadmap', as: 'roadmap'
# Press Material
get 'press', to: 'press_materials#index', as: 'press_materials'
get 'press/brand', to: 'press_materials#brand', as: 'press_materials_brand'
get 'press/team', to: 'press_materials#team', as: 'press_materials_team'
get 'press/app', to: 'press_materials#app', as: 'press_materials_app'
get 'press/pace-link', to: 'press_materials#pace_link', as: 'press_materials_pace_link'
get 'press/graphics', to: 'press_materials#graphics', as: 'press_materials_graphics'
# Feature pages
get 'features/automatic-emergency-call', to: 'features#ecall', as: 'ecall'
get 'features/fuel-saving-trainer', to: 'features#fuel_saving_trainer', as: 'fuel_saving_trainer'
get 'features/trouble-code-analysis', to: 'features#trouble_code_analysis', as: 'trouble_code_analysis'
get 'features/find-my-car', to: 'features#find_my_car', as: 'find_my_car'
get 'features/logbook', to: 'features#logbook', as: 'logbook'
# old route – preserved as there might be old links somewhere pointing at this
get 'features/automatisches-fahrtenbuch', to: 'features#automatic_logbook'
# actual route: find-the-cheapest-gas-station
get 'features/gas-station-finder', to: 'features#gas_station_finder', as: 'gas_station_finder'
get 'features/fuel-cost-tracking', to: 'features#fuel_cost_tracking', as: 'fuel_cost_tracking'
get 'features/performance-monitor', to: 'features#performance_monitor', as: 'performance_monitor'
get 'features/traffic-monitor', to: 'features#traffic_monitor', as: 'traffic_monitor'
# Endpoints for car finder
get 'cars/query', to: 'cars#identify'
get 'cars/:id', to: 'cars#show'
# Job adverts
get 'jobs', to: 'jobs#index', as: 'jobs'
get 'jobs/:slug', to: 'jobs#show', as: 'job'
# Newsletter related routes
post 'newsletter-sign-up', to: 'subscribers#newsletter_create', as: 'newsletter'
get 'newsletter-confirmation(/:list)', to: 'subscribers#newsletter_after_confirm'
end
# Routes that we want to have under the german locale, but without translated route go here:
# Magazine
scope 'magazin' do
get '', to: 'magazine#index', as: 'magazine', constraints: { locale: 'de' }
# Redirect path without locale to locale equivalent path
get '/*path', to: redirect("/#{I18n.locale}/%{path}"),
constraints: lambda { |req|
!req.path.match(%r{^\/(404|422|500)$}) &&
!req.path.match(%r{^\/(#{I18n.available_locales.join("|")})\/.*})
}
# Redirect root to detected locale
root to: 'application#redirect_to_localized_root', as: :redirected_root
end
I had a quick play through with your routes, and I had I similar issue with the de route. I think the apps_path route you're getting will only allow your default locale as param.
Adding the following option solved it for me:
RouteTranslator.config do |config|
config.generate_unlocalized_routes = true
end
This will generate the unlocalized routes correctly, so you can pass multiple locales to apps_path.
There is nothing that looks super suspicious to me except this localized block. Does this come from some gem? If so does the apps_path(locale: 'en') request work outside of this localized block?
You can use Rails.application.routes.url_helpers.app_path(locale: :en) on the console to test this after adjusting the routes file.

Update a model column value by clicking a link ruby on rails

I have a servicebooking model and I am trying update its accept_booking column with a value of 1 when the following link is clicked in my servicebooking show view:
<%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>
I get the following error:
undefined method `acceptbooking_servicebooking_path' for #<#<Class:0x5a35e98>:0x5a2bbf0>
Below I have declared the route in routes.rb
get 'acceptbooking', to: 'servicebookings#acceptbooking'
I have the following acceptbooking method in my servicebookings controller:
def acceptbooking
render nothing: true
#servicebooking = Servicebooking.find(params[:id])
#servicebooking.update_attribute(:accept_booking, 1)
end
my Routes.rb
Appa::Application.routes.draw do
devise_for :users
devise_for :views
get "welcome/index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
get 'myevents', to: 'events#myevents'
get 'myvenues', to: 'venues#myvenues'
get 'myservices', to: 'services#myservices'
get 'make_available', to: 'services#make_available'
get 'myservicebookings', to: 'servicebookings#myservicebookings'
get 'acceptbooking', to: 'services#acceptbooking'
#match 'acceptbooking', to: 'servicebookings#acceptbooking', via: [:get, :post]
resources :events do
resources :comments
end
resources :users do
resources :events
end
resources :venues do
resources :comments do
collection do
post :venuec
end
end
end
resources :services do
resources :comments do
collection do
post :servicec
end
end
end
resources :servicebookings do
resources :services
end
end
Can anyone see where Iam going wrong here? Thanks guys.
Try this: get 'acceptbooking/:id', to: 'servicebookings#acceptbooking', as: 'acceptbooking_servicebooking'
With just the route definition (from your route file):
get 'acceptbooking', to: 'servicebookings#acceptbooking'
the name of the route will be acceptbooking so you should be using acceptbooking_path. But based on your <%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>, it appears like you want a url like /servicebookings/:service_booking_id/acceptbooking. If this is the case then you should update your route definition to:
resources :servicebookings do
resources :services
# Move the following line here from the top.
get 'acceptbooking', to: 'servicebookings#acceptbooking'
end
And this will give you servicebooking_acceptbooking_path (Note the order of servicebooking and acceptbooking here).

Rails nesting scope block in resources

I'm trying to create a path like product/:id/monthly/revenue/ and product/:id/monthly/items_sold and the equivalent named routes product_monthly_revenue and product_monthly_items_sold, and these routes would simply show the charts. I tried
resources :products do
scope 'monthly' do
match 'revenue', to: "charts#monthly_revenue", via: 'get'
match 'items_sold', to: "charts#monthly_items_sold", via: 'get'
end
end
But this gives me the routes:
product_revenue GET /monthly/products/:product_id/revenue(.:format) charts#monthly_revenue
product_items_sold GET /monthly/products/:product_id/items_sold(.:format) charts#monthly_items_sold
where monthly gets appended in front instead, and the route naming is off. I know I could just do:
resources :products do
match 'monthly/revenue', to: "charts#monthly_revenue", via: 'get', as: :monthly_revenue
match 'monthly/items_sold', to: "charts#monthly_items_sold", via: 'get', as: :monthly_items_sold
end
but that isn't DRY, and it gets crazy when I try to add more categories like yearly. Using a namespace would force me to create a new controller for each namespace, when I want to consolidate all the charts into a single controller.
So I guess the summarised question would be: is it possible to namespace routes without namspacing controllers? Or is it possible to consolidate the creation of categories of named routes?
Edit: Using
resources :products do
scope "monthly", as: :monthly, path: "monthly" do
match 'revenue', to: "charts#monthly_revenue", via: 'get'
match 'items_sold', to: "charts#monthly_items_sold", via: 'get'
end
end
would give me the routes
monthly_product_revenue GET /monthly/products/:product_id/revenue(.:format) charts#monthly_revenue
monthly_product_items_sold GET /monthly/products/:product_id/items_sold(.:format) charts#monthly_items_sold
which similar to the first block, is unexpected because I expect that if a scope is nested in a resources block, only the routes in the scope block would affected by the the scope, not the resources block.
Edit 2: Forgot to include this information earlier, but I'm on Rails 4.0.0, with Ruby 2.0.0-p247
The real solution is to use nested:
resources :products do
nested do
scope 'monthly', as: :monthly do
get 'revenue', to: 'charts#monthly_revenue'
get 'items_sold', to: 'charts#monthly_items_sold'
end
end
end
Ref: https://github.com/rails/rails/issues/12626
Here's how I might approach:
periods = %w(monthly yearly)
period_sections = %w(revenue items_sold)
resources :products do
periods.each do |period|
period_sections.each do |section|
get "#{period}/#{section}", to: "charts##{period}_#{section}", as: "#{period}_#{section}"
end
end
end
It is also possible to use named routes and pass the values to your controller method via params (be sure to properly validate before using):
resources :products do
get ":period/:section", to: "charts#generate_report", as: :report
end
# report_path(period: 'monthly', section: 'revenue')

Resources