I can't figure out why my application isn't working properly.
I've got the following routes.rb:
Rails.application.routes.draw do
root to: 'visitors#index'
# ... other routes
# Default root
get '*path', to: "error#error_404"
end
I enter the http://localhost:3000 in the browser url and get redirected to http://localhost:3000/404. Logs tell me one thing:
Started GET "/404" for 127.0.0.1 at 2015-08-31 14:24:50 +0300
Processing by ErrorController#error_404 as HTML
Parameters: {"path"=>"404"}
Rendered error/error_404.html.slim within layouts/application (0.8ms)
Rendered layouts/_navigation_links.html.slim (2.2ms)
Rendered layouts/_navigation.html.slim (3.2ms)
Rendered layouts/_messages.html.slim (0.1ms)
Rendered layouts/_footer.html.slim (0.1ms)
Completed 200 OK in 876ms (Views: 875.3ms | ActiveRecord: 0.0ms)
Other related files are the following:
visitors_controller.rb
class VisitorsController < ApplicationController
def index
#specializations = Specialization.order(name: :asc)
end
...
views/visitors/index.html.slim
- if #specializations.count == 0
h3.bg-primary.text-center = I18n.t('layout.no_specializations')
- #specializations.each_with_index do |specialization, index|
- if index % 3 == 0
.row
.col-sm-4
= link_to specialization
img src="#{specialization.avatar.url(:thumb)}" /
p.text-center
= specialization.name
- if index % 3 == 2
What can be the issue here?
P.S. When I enter e,g, localhost:3000/specializations/1 the resulting page gets normally loaded.
UPDATE
curl localhost:3000 gets the right HTML, without any redirection.
It turned out to be bad slim code. This line:
img src="#{specialization.avatar.url(:thumb)}" /
should be refactored into
img src=specialization.avatar.url(:thumb) /
Done. works.
Related
I'm new to Redmine and Rails, and I have a question I'm stuck with.
I have a timelogger plugin installed from https://github.com/speedy32129/time_logger and it has a redirect option to the IssuesController edit method in Redmine source
https://github.com/speedy32129/time_logger/blob/master/app/controllers/time_loggers_controller.rb#L82
I want to create a custom controller action called edit_realtime, define a route for this and a custom view because I need to do some changes to this view and don't affect the original edit view.
What I've done:
Changed the edit method to edit_realtime in app/controllers/time_loggers_controller.rb:
redirect_to controller: 'issues',
protocol: Setting.protocol,
action: 'edit_realtime',
id: issue_id,
time_entry: { hours: hours }
end
Created a new controller method in app/controllers/issues_controller.rb
def edit_realtime
return unless update_issue_from_params
respond_to do |format|
format.html {}
format.js
end
end
Created a route in routes.rb
post '/issues/new', :to => 'issues#new'
get '/issues/:id/edit_realtime', :to => 'issues#edit_realtime'
Created the needed views in app/views/issue
edit_realtime.html.erb
edit_realtime.js.erb
_edit_realtime.html.erb
The result: 403 error when stopping the time log
View image
full route: http://localhost:81/redmine/issues/1/edit_realtime?time_entry%5Bhours%5D=0.0
and production log
Started GET "/redmine/time_loggers/stop" for 127.0.0.1 at 2021-08-11 16:26:22 +0300
Processing by TimeLoggersController#stop as HTML
Current user: alisa (id=1)
Redirected to http://localhost:81/redmine/issues/1/edit_realtime?time_entry%5Bhours%5D=0.0
Completed 302 Found in 24ms (ActiveRecord: 14.8ms)
Started GET "/redmine/issues/1/edit_realtime?time_entry%5Bhours%5D=0.0" for 127.0.0.1 at 2021-08-11 16:26:22 +0300
Processing by IssuesController#edit_realtime as HTML
Parameters: {"time_entry"=>{"hours"=>"0.0"}, "id"=>"1"}
Current user: alisa (id=1)
Rendering common/error.html.erb within layouts/base
Rendered common/error.html.erb within layouts/base (0.5ms)
Rendered plugins/time_logger/app/views/time_loggers/_embed_menu.html.erb (6.6ms)
Rendered plugins/time_logger/app/views/time_loggers/_update_menu.html.erb (8.5ms) [cache hit]
Filter chain halted as :authorize rendered or redirected
Completed 403 Forbidden in 44ms (Views: 26.3ms | ActiveRecord: 13.1ms)
what did I do wrong?
I have found many generic posts suggesting this has to do with redirects. I believe this may be due to how I have a form set up.
On the plans.html.erb page I have a form with four submits, each going to the same place with different params:
<%= form_with url: :affiliate_select_plan, class: "mx-auto" do |f| %>
<!-- Paid Plans -->
<% #plans.each_with_index do |plan, i| %>
<%= f.button 'Select Plan', value: plan[:name], type: 'submit' %>
<% end %>
<% end %>
I have the affiliate_select_plan_path setup in my routes.rb:
devise_scope :affiliate do
post 'affiliate/select_plan', :to => 'affiliates/registrations#select_plan'
end
The form successfully hits the select_plan method in the controller, which redirects it to the new_affiliate_registration_path, passing the needed params.
def select_plan
redirect_to new_affiliate_registration_path(plan: plan_params[:button])
end
The new method in the controller is called, directing the user to the sign up page:
# GET /resource/sign_up
def new
#plan = AffiliatePlan.find_by(nickname: params.permit(:plan)[:plan].downcase)
super
end
From this page, if the back button on the browser is selected, it will bring the user back to the page they were at before being at plans.html.erb.
Could this be related to the redirect_to?
EDIT:
Here are the logs:
Started GET "/" for 127.0.0.1 at 2020-02-25 19:06:02 -0500
Processing by Affiliates::RegistrationsController#plans as HTML
Rendering affiliates/registrations/plans.html.erb within layouts/application
Rendered affiliates/registrations/plans.html.erb within layouts/application (5.2ms)
Rendered layouts/_google_analytics.html.erb (0.5ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 195ms (Views: 194.2ms | ActiveRecord: 0.0ms)
Started POST "/partner/select_plan" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#select_plan as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ck8HGRryriXleQrUjCSKjTrIRLIw273EdSu4WZnFn3kAL1mMmk7jqR1tZgnPniHsMzHFMl81vPBRuvA0/W4uSw==", "button"=>"Local"}
Unpermitted parameters: :utf8, :authenticity_token
Redirected to http://localhost:3000/partners/sign_up?plan=Local
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
Started GET "/partners/sign_up?plan=Local" for 127.0.0.1 at 2020-02-25 19:06:13 -0500
Processing by Affiliates::RegistrationsController#new as HTML
Parameters: {"plan"=>"Local"}
AffiliatePlan Load (1.2ms) SELECT "affiliate_plans".* FROM "affiliate_plans" WHERE "affiliate_plans"."nickname" = $1 LIMIT $2 [["nickname", "local"], ["LIMIT", 1]]
↳ app/controllers/affiliates/registrations_controller.rb:11
Rendering affiliates/registrations/new.html.erb within layouts/application
Rendered affiliates/registrations/new.html.erb within layouts/application (4.6ms)
Rendered layouts/_google_analytics.html.erb (1.1ms)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (1.2ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 191ms (Views: 187.6ms | ActiveRecord: 1.2ms)
I have a hunch that this might have to do with form resubmission: Forms and the back button tend to be a bit wonky at times.
However, instead of going more in depth with this, let me point you in another direction. I'm doing this because to me, this looks like a classic case of someone trying to find a solution to the wrong problem. I'm saying this because based on the code and log snippets you've provided, you're jumping through hoops to pass a parameter (in your case the name of a plan) via multiple actions – which, if I'm right, is just unnecessary.
Here's what I would do instead:
<% #plans.each do |plan| %>
<%=
link_to 'Select Plan',
new_affiliate_registration_path(plan: plan.downcase),
class: 'some-button-class
%>
<% end %>
This way, you don't have to mess around in your controllers in any way. Also, since there is no POST request, you won't have any issues with form (re)population and such things.
Weird Error. I have some routes that work perfectly during development but once i deploy and try to access them it comes up with page does not Exist error
I have the following routes.rb file:
TransportUnl::Application.routes.draw do
resources :trucks
resources :shipments do
collection do
get :autocomplete_location_cs
end
end
devise_for :users do
get '/users/sign_in' => 'devise/sessions#new'
get '/users/sign_out' => 'devise/sessions#destroy'
end
root :to => 'info#index'
resources :info do
collection do
get 'about'
get 'contact'
get 'you_dont_have_a_full_account'
get 'help'
end
member do
get 'index'
end
end
resources :companies
end
Not everything is setup yet. but i am getting a page not found error when i go to:
www.website.com/shipments
www.website.com/trucks
as well as others in production. The main index page works and you can login but these pages come up not found.
Production
Development
Production.log
Started GET "/shipments" for 108.235.52.160 at 2015-06-22 13:09:03 -0500
Processing by ShipmentsController#index as HTML
[1m[35m (0.6ms)[0m SELECT MAX("shipments"."price") AS max_id FROM "shipments"
[1m[36mShipment Load (0.3ms)[0m [1mSELECT "shipments".* FROM "shipments" [0m
Rendered shipments/_nav.html.erb (0.6ms)
Rendered shipments/_search_table.html.erb (0.1ms)
Rendered shipments/index.html.erb within layouts/application (1.2ms)
[1m[35mUser Load (0.5ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered shared/_header.html.erb (5.0ms)
Completed 500 Internal Server Error in 12ms
ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"companies"}):
app/views/shared/_header.html.erb:78:in `_app_views_shared__header_html_erb___2847381188393053217_232073740'
app/views/layouts/application.html.erb:17:in `_app_views_layouts_application_html_erb__4421904906041360553_230384600'
app/controllers/shipments_controller.rb:7:in `index'
Started GET "/info/about" for 157.55.39.229 at 2015-06-22 13:10:29 -0500
Processing by InfoController#about as */*
Rendered info/about.html.erb within layouts/application (5.7ms)
Rendered shared/_header.html.erb (3.8ms)
[1m[36m (4.3ms)[0m [1mSELECT * FROM geometry_columns WHERE f_table_name='shipments'[0m
[1m[35mShipment Load (0.9ms)[0m SELECT "shipments".* FROM "shipments" ORDER BY id DESC LIMIT 3
Rendered shared/_footer.html.erb (31.6ms)
Completed 200 OK in 177ms (Views: 151.1ms | ActiveRecord: 25.6ms)
the Link is created like this:
<%= link_to "My Account", edit_company_path(current_user.company_id) %>
As you can see from logs, you got error page because of line 78 of app/views/shared/_header.html.erb file.
In this piece of code, where you creating link
<%= link_to "My Account", edit_company_path(current_user.company_id) %>
Check if company_id is not nil for that particular user. I'm pretty sure it's nil in your case.
As you can see from error logs, it tries to get action edit as collection action of companies controller - companies/edit. You don't have this route defined. But if current user will have company_id, the link will be built correctly and you wont receive an error.
For some reason both these URLS are routing to the same file when they shouldn't be, another thing that I noticed when typing in an invalid url such as localhost:3000/topics/inexjojvnsjg it just stays on the same page.
here is what my rails console is telling me when I try to access the url
localhost:3000/topics/index
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
Rendered topics/show.html.erb within layouts/application (0.1ms)
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =$1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 98ms (Views: 96.5ms | ActiveRecord: 0.8ms)
here is my routes file....
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
get 'welcome/about'
# get "topics/index"
# get "topics/show"
# get "topics/new"
# get "topics/edit"
#for some reason, using resources:topics, index and show both route to show
resources :topics
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
Here is the key info:
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
The :index url for a TopicsController is "/topics".
The :show url for a TopicsController is "/topics/:id" or "/topics/1", where the last part of the url gets associated to the params[:id]. With the url "/topics/1" the :id = 1.
So when you go to the url "/topics/index" you are going to the :show action because of the "index" part of the url. You are just setting the :id to "index" instead of a Integer :id. You can see that in the output you pasted here:
Parameters: {"id"=>"index"}
TLDR: "/topics/index" is a route the will pass the Rails router but is an invalid route, because the :id is a String "index".
This is my route:
scope ":username" do
resources :feedbacks
end
So when I go to mydomain.com/test/feedbacks/10 it shows the correct feedback with id=10 that belongs to username=test.
But, if I go to mydomain.com/test2/feedbacks/10 it shows me the same feedback with id=10, which does NOT belong to username=test2.
How do I restrict this from happening?
I am using the Vanity gem to give me the username in the URL, this is what that route looks like:
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+\#]+/}
end
Edit 1:
That is to say, for clarity's sake, when I go to mydomain.com/test/feedbacks/10 and /test2/feedbacks/10, it shows me the same view for the same record (in which case, the latter version would be wrong because it should be telling me that no such record exists, but it's not. It is just displaying the correct record for test/feedbacks/10).
Edit 2:
Here are the logs of both requests:
The right request
Started GET "/test-3/feedbacks/7" for 127.0.0.1 at 2011-09-14 02:48:15 -0500
Processing by FeedbacksController#show as HTML
Parameters: {"username"=>"test-3", "id"=>"7"}
Feedback Load (0.5ms) SELECT "feedbacks".* FROM "feedbacks" WHERE "feedbacks"."id" = ? LIMIT 1 [["id", "7"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Rendered feedbacks/show.html.erb within layouts/application (36.2ms)
Completed 200 OK in 188ms (Views: 184.3ms | ActiveRecord: 1.8ms)
The wrong request
Started GET "/test2/feedbacks/7" for 127.0.0.1 at 2011-09-14 02:48:28 -0500
Processing by FeedbacksController#show as HTML
Parameters: {"username"=>"test2", "id"=>"7"}
Feedback Load (0.1ms) SELECT "feedbacks".* FROM "feedbacks" WHERE "feedbacks"."id" = ? LIMIT 1 [["id", "7"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Rendered feedbacks/show.html.erb within layouts/application (37.6ms)
Completed 200 OK in 50ms (Views: 47.5ms | ActiveRecord: 1.2ms)
Your show action should look something like
def show
#user = User.find_by_username(params[:username])
if #user == current_user
...
render "show"
else
flash[:alert] = "Record doesn't exist"
redirect_to root_path
end
end
I took the liberty of adding in #Benoit's suggestion.