I have a namespaced controller (Interviews)
namespace :recruitment do
resources :interviews
end
I created a route (outside of the namespace)
get "myinterviews", to: "recruitment/interviews#my", as: :myinterviews
SO that /myinterviews shows the user his own interviews
Whereas rails seems to find the right view, it totally skips the controller action
def my
puts "Hi there"
exit
#recruitment_interviews=current_user.interviews
puts #recruitment_interviews.inspect
end
This is ofcourse inside Recruitments::InterviewController.
The view(recruitment/interviews#my) is rendered (it results in an error since recruitment_interviews is not found
Here's the process log:
Started GET "/myinterviews" for 127.0.0.1 at 2015-07-03 18:54:48 +0530
Processing by Recruitment::InterviewsController#my as HTML
Rendered recruitment/interviews/my.html.slim within layouts/application (3.7ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
Rendered layouts/_navbar.html.slim (2.4ms)
Completed 200 OK in 199ms (Views: 197.6ms | ActiveRecord: 0.2ms)
Related
I got this error.
GET http://localhost:3000/activities/undefined 500 (Internal Server Error)
In browser console log.
When i tried to display the data by this variable in my .erb template file.
<h1><%= #activity.name %></h1>
This is my controller method.
def activities_details
#activity = Activity.find_by(id: params[:id])
#temple = Temple.find_by(id: #activity.temple_id)
render :template => 'front_pages/activity_details'
end
This page still works fine but i still confusing why this error keeps happening.
#
I found the reason it seems like rails call
get request from my link twice.
First time is the correct request,but second time is the wrong one.
this is log from the server
Started GET "/activities/24" for ::1 at 2016-02-22 14:19:13 +0800
Processing by FrontPagesController#activities_details as HTML
Parameters: {"id"=>"24"}
Activity Load (0.4ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."created_at" DESC LIMIT 1 [["id", 24]]
Temple Load (0.4ms) SELECT "temples".* FROM "temples" WHERE "temples"."id" = $1 ORDER BY "temples"."created_at" DESC LIMIT 1 [["id", 7]]
Rendered front_pages/activity_details.html.erb within layouts/application (0.3ms)
Rendered shared/_header.html.erb (3.8ms)
Rendered shared/_footer.html.erb (0.1ms)
Completed 200 OK in 419ms (Views: 414.3ms | ActiveRecord: 0.8ms)
Started GET "/assets/shutterstock_96393731.jpg" for ::1 at 2016-02-22 14:19:13 +0800
Started GET "/activities/undefined" for ::1 at 2016-02-22 14:19:13 +0800
Processing by FrontPagesController#activities_details as HTML
Parameters: {"id"=>"undefined"}
Activity Load (0.6ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."created_at" DESC LIMIT 1 [["id", 0]]
Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.6ms)
NoMethodError (undefined method `temple_id' for nil:NilClass):
app/controllers/front_pages_controller.rb:36:in `activities_details'
This is my link from the previous page.
<%= link_to "Details", activity_show_path(activity.id) ,class: "btn_1"%>
My routes
get '/activities/:id' => 'front_pages#activities_details', :as => 'activity_show'
Thanks!
I am redirecting the user after a model save.
if #client.save
redirect_to :new_course
on the new course controller I have no redirection
def new
#course = Course.new
end
After the course_controller#new is been executed I am been redirected on another controller view.
I can't figure out where does this redirection comes from.
I am tracing the application and seen that
Started GET "/courses/new" for ::1 at 2015-10-01 15:01:46 -0400
Processing by CoursesController#new as */*
Therapist Load (0.6ms) SELECT "therapists".* FROM "therapists" WHERE "therapists"."id" = $1 ORDER BY "therapists"."id" ASC LIMIT 1 [["id", 14]]
Rendered courses/new.html.erb within layouts/application (228.3ms)
Client Exists (0.5ms) SELECT 1 AS one FROM "clients" WHERE "clients"."therapist_id" = $1 LIMIT 1 [["therapist_id", 14]]
Completed 200 OK in 679ms (Views: 652.7ms | ActiveRecord: 3.5ms)
Started GET "/clients/show" for ::1 at 2015-10-01 15:01:46 -0400
Processing by ClientsController#show as HTML
Therapist Load (0.7ms) SELECT "therapists".* FROM "therapists" WHERE "therapists"."id" = $1 ORDER BY "therapists"."id" ASC LIMIT 1 [["id", 14]]
Rendered clients/show.html.erb within layouts/application (0.4ms)
Client Exists (0.4ms) SELECT 1 AS one FROM "clients" WHERE "clients"."therapist_id" = $1 LIMIT 1 [["therapist_id", 14]]
Client Exists (0.3ms) SELECT 1 AS one FROM "clients" WHERE "clients"."therapist_id" = $1 LIMIT 1 [["therapist_id", 14]]
Client Exists (0.2ms) SELECT 1 AS one FROM "clients" WHERE "clients"."therapist_id" = $1 LIMIT 1 [["therapist_id", 14]]
Completed 200 OK in 371ms (Views: 367.8ms | ActiveRecord: 1.6ms)
The redirect_to method sends a response to the browser that indicates it should load a new page. Then Rails is finished and waits for another request, that controller object is destroyed as it's no longer needed.
This is typically done with the Location: header in the HTTP response.
The next request to come in is the result of this redirection.
Rails sets up some default redirects based on common functionality. Basically, after you create a new object it assumes you will want to go view it and redirects to the #show action. If you want it to go somewhere else you need to set up that route.
As I recall there are also instances where rails will default an action you haven't created for instance, if you have an index action in your routes but never set it up rails will make one for you with #my_items = MyItems.all and render the index view.
In my rails app, I have a link so that a user can download a GIF on the site:
<%= link_to "gif", :controller => "projects", :action => :export_gif, :id => #project.id %>
This is the corresponding controller action:
def export_gif
if #project.gif.blank?
#project.generate_gif #this creates #project.gif
end
gif_path = #project.gif.gif_file_url
gif_path.sub! 'https', 'http'
send_data open(gif_path).read, filename: "project_#{#project.id}.gif", type: "image/gif"
end
When the user clicks on the link, the export_gif action is being called twice. How do I ensure that it only gets called once?
Here's what the logs look like after I click the link:
Started GET "/projects/38/export_gif" for ::1 at 2015-06-16 17:08:55 -0400
Processing by ProjectsController#export_gif as HTML
Parameters: {"id"=>"38"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 38]]
Gif Load (0.1ms) SELECT "gifs".* FROM "gifs" WHERE "gifs"."project_id" = ? LIMIT 1 [["project_id", 38]]
Rendered text template (0.0ms)
Sent data project_38.gif (3.6ms)
Completed 200 OK in 207ms (Views: 3.4ms | ActiveRecord: 0.2ms)
Started GET "/projects/38/export_gif" for ::1 at 2015-06-16 17:08:55 -0400
Processing by ProjectsController#export_gif as HTML
Parameters: {"id"=>"38"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 38]]
Gif Load (0.1ms) SELECT "gifs".* FROM "gifs" WHERE "gifs"."project_id" = ? LIMIT 1 [["project_id", 38]]
Rendered text template (0.0ms)
Sent data project_38.gif (0.6ms)
Completed 200 OK in 196ms (Views: 0.5ms | ActiveRecord: 0.1ms)
For me it was because of <img src="#">. I had this in one of the files of layouts.
So I changed "#" with "http://example.com" and issue got resolved.
I got this solution from here(aNoble's answer):
Rails seems to be serving the page twice
I have a destroy method within a notecards controller that I am calling from a users page to delete a notecard. In the first example below.. the redirect is passing the notecard ID resulting in a page not found, while the second the user ID is being passed correctly finding the user page.. can someone help me understand why?
Redirects to user passing id of notecard
def destroy
#note = Notecard.find_by_id(params[:id])
delete_note(#note)
redirect_to user_path(#current_user)
end
Redirects to user passing id of user
def destroy
#note = current_user.notecards.find_by_id(params[:id])
delete_note(#note)
redirect_to user_path(#current_user)
end
Update:
Thanks for the responses. The code is here: https://github.com/incorvia/plumnotes/.. The authentication is in the sessions helper and the sessions controller. As for the log:
With Notecard.find_by_id(params[:id])
Started DELETE "/notecards/177" for 127.0.0.1 at 2011-10-15 11:53:38 -0400
Processing by NotecardsController#destroy as HTML
Parameters: {"authenticity_token"=>"WctbONb/qAO+hesHZ6Yw5zU19eCPNGeILIhxnW9Pi1Y=", "id"=>"177"}
Notecard Load (0.3ms) SELECT `notecards`.* FROM `notecards` WHERE `notecards`.`id` = 177 LIMIT 1
SQL (0.8ms) DELETE FROM `notecards` WHERE `notecards`.`id` = 177
Redirected to http://localhost:3000/users/177
Completed 302 Found in 12ms
Started GET "/users/177" for 127.0.0.1 at 2011-10-15 11:53:39 -0400
Processing by UsersController#show as HTML
Parameters: {"id"=>"177"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 LIMIT 1
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 177 LIMIT 1
Completed 404 Not Found in 14ms
ActiveRecord::RecordNotFound (Couldn't find User with id=177):
app/controllers/users_controller.rb:11:in `show'
Rendered /Users/Kevin/.rvm/gems/ruby-1.9.2-p290#notes/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
Rendered /Users/Kevin/.rvm/gems/ruby-1.9.2-p290#notes/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /Users/Kevin/.rvm/gems/ruby-1.9.2-p290#notes/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.6ms)
With current_user.notecards.find_by_id(params[:id])
Started DELETE "/notecards/179" for 127.0.0.1 at 2011-10-15 11:56:18 -0400
Processing by NotecardsController#destroy as HTML
Parameters: {"authenticity_token"=>"WctbONb/qAO+hesHZ6Yw5zU19eCPNGeILIhxnW9Pi1Y=", "id"=>"179"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 LIMIT 1
Notecard Load (0.6ms) SELECT `notecards`.* FROM `notecards` WHERE `notecards`.`user_id` = 9 AND `notecards`.`id` = 179 LIMIT 1
SQL (0.9ms) DELETE FROM `notecards` WHERE `notecards`.`id` = 179
Redirected to http://localhost:3000/users/9
Completed 302 Found in 95ms
Started GET "/users/9" for 127.0.0.1 at 2011-10-15 11:56:18 -0400
Processing by UsersController#show as HTML
Parameters: {"id"=>"9"}
Hard to guess what is defined to local var #current_user and where it occurs.
If you really interested, why, - probably more piece of code could be helpful.
Sometimes, keep it simple could be quite an interesting idea (:
redirect_to user_path( current_user.id )
It seems that #current_user wasn't actually being defined for the #Notcard controller unless the current_user method was called. That's why that the current_user.notecards... worked
It also seems that with user_path(#current_user) .. if #current_user isn't defined then it just takes the id from the params.. which happens to be the notecard id and not a user id...
The solution to fixing it was to either user current_user.notecards.. or add a before_filter for the controller that authorizes users and defines the #current_user instance variable :-)
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.