Rails Controller Action being called twice - ruby-on-rails

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

Related

Why does my Capybara feature test intermittently fail when waiting for Ajax?

I'm using Capybara 2.13.0 and I've got a test that relies on an Ajax request adding dynamic content into the DOM.
test 'contact data becomes visible when providing the correct password' do
login_as_researcher
visit '/participants/3'
assert page.has_css?("ul#redacted-contact-data")
click_button('Unlock Contact Data')
# Use the correct password
fill_in('enter your password', with: 'password1')
# Ajax happens here. It renders a partial and places it in the dom.
click_button('Submit')
# Test sometimes fails here..
assert page.has_css?("ul#contact-data"), -> { page.body }
end
I've read a lot of articles that say Capybara automatically waits for elements to appear in the HTML and there's no need to manually tell Capybara to wait anymore, but I'm not experiencing this in my case.
75% of the time this test passes and when it does fail and the page is dumped, it contains the content of the partial rendered in the JS response instead of the full HTML page.
Any idea what's going on here? I can only assume it's due to how long it takes the server to respond the Ajax request each time and Capybara not waiting long enough in certain situations.
I've set Capybara.default_max_wait_time = 5 in my test_helper.
I've tried with both the selenium and poltergeist drivers and it makes no difference.
Here's the output from test.log
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
Researcher Load (0.3ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Redirected to http://www.example.com/session/new
Filter chain halted as :researcher_only rendered or redirected
Completed 302 Found in 30ms (ActiveRecord: 0.3ms)
Started GET "/session/new" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#new as HTML
Rendered sessions/new.html.erb within layouts/application (9.5ms)
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" IS NULL LIMIT 1
Completed 200 OK in 248ms (Views: 247.6ms | ActiveRecord: 0.1ms)
Started POST "/session" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "username"=>"nick1", "password"=>"[FILTERED]", "commit"=>"Enter"}
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."username" = ? LIMIT 1 [["username", "nick1"]]
Redirected to http://www.example.com/
Completed 302 Found in 78ms (ActiveRecord: 0.1ms)
Started GET "/" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by HomeController#index as HTML
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
Rendered home/index.html.erb within layouts/application (0.6ms)
Completed 200 OK in 10ms (Views: 8.6ms | ActiveRecord: 0.1ms)
Started GET "/participants/3" for 127.0.0.1 at 2017-05-01 18:09:23 +0100
Processing by ParticipantsController#show as HTML
Parameters: {"id"=>"3"}
Participant Load (1.9ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1 [["id", 3]]
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
ContactData Load (0.2ms) SELECT "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
AlcoholService Load (0.1ms) SELECT "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1 [["id", 16]]
Baseline Load (0.3ms) SELECT "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
Rendered participants/_unlock_contact_data.html.erb (1.1ms)
Rendered participants/_details.html.erb (52.8ms)
Rendered participants/show.html.erb within layouts/application (58.8ms)
Completed 200 OK in 94ms (Views: 69.9ms | ActiveRecord: 3.0ms)
Started POST "/participants/unlock_contact_data?participant_id=3" for 127.0.0.1 at 2017-05-01 18:09:24 +0100
Processing by ParticipantsController#unlock_contact_data as JS
Parameters: {"utf8"=>"✓", "from"=>"show", "password"=>"[FILTERED]", "commit"=>"Submit", "participant_id"=>"3"}
Researcher Load (0.1ms) SELECT "researchers".* FROM "researchers" WHERE "researchers"."id" = ? LIMIT 1 [["id", 2]]
Participant Load (0.0ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" = ? LIMIT 1 [["id", 3]]
ContactData Load (0.1ms) SELECT "contact_data".* FROM "contact_data" WHERE "contact_data"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
AlcoholService Load (0.1ms) SELECT "alcohol_services".* FROM "alcohol_services" WHERE "alcohol_services"."id" = ? LIMIT 1 [["id", 16]]
Baseline Load (0.0ms) SELECT "baselines".* FROM "baselines" WHERE "baselines"."participant_id" = ? LIMIT 1 [["participant_id", 3]]
Rendered participants/_details.html.erb (4.6ms)
Rendered participants/unlock_contact_data.js.erb (6.7ms)
Completed 200 OK in 81ms (Views: 10.8ms | ActiveRecord: 0.3ms)
(0.1ms) rollback transaction
(0.1ms) begin transaction
Is there anything I might be missing?

how to manage users by an admin in ruby on rails

how can I manage and edit other users profiles as an admin since I have one model and controller (users) ?
I tried to add a new action called updateusers
def updateusers
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
the problem here :it is updating my admin user with the other_user's
data
stack trace
Started GET "/manage" for ::1 at 2016-03-19 21:06:08 +0300 Processing by
UsersController#manage as HTML User Load (1.0ms) SELECT "users".* FROM
"users" Rendered users/manage.html.erb within layouts/application (5.0ms) User
Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1
[["id", 1]] Completed 200 OK in 53ms (Views: 51.0ms | ActiveRecord: 1.0ms)
'Started GET "/users/10" for ::1 at 2016-03-19 21:06:10 +0300 Processing by
UsersController#show as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered
users/show.html.erb within layouts/application (0.0ms) User Load (0.0ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200
OK in 37ms (Views: 36.0ms | ActiveRecord: 0.0ms)
Started GET "/editusers/10" for ::1 at 2016-03-19 21:06:11 +0300 Processing
by UsersController#editusers as HTML Parameters: {"id"=>"10"} User Load (0.0ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
Rendered users/editusers.html.erb within layouts/application (4.0ms) User Load
(1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Completed 200 OK in 41ms (Views: 39.0ms | ActiveRecord: 1.0ms)
Started PATCH "/users/10" for ::1 at 2016-03-19 21:06:15 +0300 Processing by
UsersController#update as HTML Parameters: {"utf8"=>"✓",
"authenticity_token"=>"6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm+bYcihgGDxTjDdFGU2Riou2p‌​
cRk5ncjCtFDGwfBj17Uq7gc0u329w==", "user"=>{"first_name"=>"g", "last_name"=>"g",
"email"=>"g#g.g", "role"=>"editor", "image"=>"pic.png", "admins"=>""},
"other"=>"update", "id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users"
WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Unpermitted parameters: role, admins
(0.0ms) begin transaction SQL (1.0ms) UPDATE "users" SET "first_name" = ?,
"last_name" = ?, "email" = ?, "updated_at" = ? WHERE "users"."id" = ?
[["first_name", "g"], ["last_name", "g"], ["email", "g#g.g"], ["updated_at",
"2016-03-19 18:06:15.488284"], ["id", 1]] (47.0ms) commit transaction Redirected
to localhost:8080/profile Completed 302 Found in 54ms (ActiveRecord: 48.0ms)
The user ID of the form in "editusers" is set to your admin (or logged in user). It's hard to say without seeing the code but I think you've set up the editusers form incorrectly. Perhaps using a hidden field to hold the ID of the user you want to update.
Try to avoid that and set up the #user object in the 'editusers' action #user = User.find(10)
Then in your view use a form_for #user do |f| without any hidden fields for the ID.
after 10 days ,, Yes i did it - the solution is in the name of submit , I named the two submits with diffrent names <%= f.submit "update", name:"other" %> then i used the update action like this
def update
if params[:current]
#user = current_user
if #user.update_attributes(user_params)
redirect_to '/profile'
else
redirect_to '/edit'
end
elsif params[:other]
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
end

500 (Internal Server Error) when i display the data from the variable in rails

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!

Ruby on Rails send_file, code in controller action running twice

I'm running into a weird issue with user downloadable files, the file is downloading properly, but other code in the controller action is executing twice. I'm using CarrierWave, which is mounted on a Document model at .file.
I want users to be able to click this link to download the file:
<%= link_to document.file.file.filename, document_path(document) %>
This is what my controller looks like:
def show
document = Document.find(params[:id])
# Track this download
CourseDownload.create(course: document.course, user: current_user)
# Download the file
send_file 'public' + document.file.url.to_s
end
When I click the link, the file downloads, but 2 CourseDownload records are created. In the logs it looks like the GET request is happening twice:
Started GET "/documents/1" for 127.0.0.1 at 2014-04-17 18:12:47 -0400
Processing by DocumentsController#show as HTML
Parameters: {"id"=>"1"}
Document Load (0.2ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxx' LIMIT 1
CACHE (0.0ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
public/uploads/document/file/1/bootstrap-3.0.0.zip
Sent file public/uploads/document/file/1/bootstrap-3.0.0.zip (0.1ms)
Completed 200 OK in 7ms (ActiveRecord: 0.4ms)
Started GET "/documents/1" for 127.0.0.1 at 2014-04-17 18:12:47 -0400
Processing by DocumentsController#show as HTML
Parameters: {"id"=>"1"}
Document Load (0.2ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'xxx' LIMIT 1
CACHE (0.0ms) SELECT "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1 [["id", "1"]]
public/uploads/document/file/1/bootstrap-3.0.0.zip
Sent file public/uploads/document/file/1/bootstrap-3.0.0.zip (0.1ms)
Completed 200 OK in 5ms (ActiveRecord: 0.3ms)
Any idea what I'm doing wrong?
Thanks!
The issue ended up being caused by a turbolinks gotcha.
Resolved with:
<%= link_to document.file.file.filename, document_path(document), 'data-no-turbolink' => true %>

Github-like username url

I have this route that let me build custom url for users like
/thisismyname, and it works fine. But when I look at the log there is
something I don't like...
When I hit /gregory this is what is happenning:
1. Going to public_profile#public # Good
2. hitting /assets and trying to find a user with asset token # Not good
I thought my constraints would avoid this but it doesn't seem like it...
class PublicProfileConstraint
def self.matches?(request)
!['assets', 'admin'].include?(request.session[:token])
end
end
get "/:token" => "profiles#public", :as => :public_profile,
:constraints => PublicProfileConstraint
Here is the log:
Started GET "/gregory" for 127.0.0.1 at 2012-03-05 12:44:43 -0800
Processing by ProfilesController#public as HTML
Parameters: {"token"=>"gregory"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."token" = 'gregorymarcilhacy' LIMIT 1
Rendered profiles/_modal.haml (0.1ms)
....
Rendered profiles/show.haml within layouts/application (154.7ms)
Completed 200 OK in 431ms (Views: 174.0ms | ActiveRecord: 5.5ms)
... Redering js files ...
# I DONT WANT THIS
Started GET "/assets/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Served asset - 404 Not Found (10ms)
Processing by ProfilesController#public as */*
Parameters: {"token"=>"assets"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."token" = 'assets' LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 312ms
... Rendering images ...
# AND I DONT WANT THIS
Started GET "/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Processing by LandingController#landing as */*
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
Rendered landing/landing.haml within layouts/landing (0.8ms)
Completed 200 OK in 288ms (Views: 23.5ms | ActiveRecord: 2.2ms)
You are searching request.session for the token, but this will always fail as that's the session store rather than the request parameters. You probably want the equivalent of params[:token] in the constraint class. The request object documentation indicates that request.path_parameters[:token] might contain the value you are looking for.

Resources