File not received when sent with send_file in rails - ruby-on-rails

I am sending file to client when they click on download in my application, however, the file don't get downloaded, by the client but the console indicates file been sent.
show.html.erb
<div class="col-10 music-detail">
<hr/>
<%= form_for :music, url: music_download_path do |f| %>
<%= f.hidden_field :music_id, value: #music.id %>
<%= f.submit :download, class:"download_btn "%>
<span class="sownload_count"><%= #music.download_count%></span>
<% end %>
</div>
MusicController.rb
def download
music_id = params[:music][:music_id]
music = Music.find(music_id)
if music.valid?
music_rel_path = music.music_path
music_file = gen_absolute_file_path music_rel_path
send_file(music_file)
download_count = music.download_count + 1
music.update(download_count: download_count)
redirect_to "/musics/#{music_id}"
end
end
rails server console output indicating file sent
Started POST "/music_download" for 127.0.0.1 at 2018-09-19 08:30:31 -0700
Processing by MusicsController#download as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JmeNUnm7HgW8T55fALqQxN9a2v9MDKmJZCWsQ0MYfu3t1ETteowbAL UMB0bf9Tu0zxFFORNj2TmOgnWW8EARIQ==", "music"=>{"music_id"=>"2"}, "commit"=>"download"}
Music Load (0.0ms) SELECT "musics".* FROM "musics" WHERE "musics"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Sent file C:/Users/USER/Desktop/projects/filmz_legendary/public/secure/musics/samuel_samuel-samuel/all_of_my_days.mp3 (48.5ms)
(0.0ms) begin transaction SQL (3.0ms) UPDATE "musics" SET "download_count" = ?, "updated_at" = ? WHERE "musics"."id" = ? [["download_count", 7], ["updated_at", "2018-09-19 15:30:32.789257"], ["id", 2]]
(202.7ms) commit transaction
Redirected to http://localhost:3000/musics/2
Completed 302 Found in 274ms (ActiveRecord: 205.7ms)
Started GET "/musics/2" for 127.0.0.1 at 2018-09-19 08:30:33 -0700
Processing by MusicsController#show as HTML
Parameters: {"id"=>"2"}
Music Load (0.0ms) SELECT "musics".* FROM "musics" WHERE "musics"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Rendering musics/show.html.erb within layouts/application
Rendered musics/show.html.erb within layouts/application (5.0ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (1.0ms)
Completed 200 OK in 5278ms (Views: 5087.8ms | ActiveRecord: 0.0ms)
I am using :
ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
ralis 5.1.6

Related

Why Rails replace the nested form fills with nils when inserting in database, but insert "created_at" and "updated_at"

I have a Ruby on Rails app that contain two controllers (categories and documentations). The documentations controller is a nested controller (from categories, of course).
In the documentations controllers, I have a form to add data in my database. When I use this form, Rails add nil values in database, instead of the content of the form.
I use the default Rails database (sqlite3)
I use two gems in this parts of the code: friendly_id and haml.
This is the log:
Started GET "/categories/ruby_on_rails" for 127.0.0.1 at 2018-03-20 14:18:47 -0400
Processing by CategoriesController#show as HTML
Parameters: {"id"=>"ruby_on_rails"}
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? LIMIT ? [["slug", "ruby_on_rails"], ["LIMIT", 1]]
Rendering categories/show.html.haml within layouts/application
Rendered documentations/_form.html.haml (2.5ms)
Documentation Load (0.1ms) SELECT "documentations".* FROM "documentations" WHERE "documentations"."category_id" = ? [["category_id", 2]]
Rendered categories/show.html.haml within layouts/application (5.4ms)
Completed 200 OK in 18ms (Views: 16.5ms | ActiveRecord: 0.3ms)
Started POST "/categories/ruby_on_rails/documentations" for 127.0.0.1 at 2018-03-20 14:24:06 -0400
Processing by DocumentationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zY+HjYiKv98/2BvWwn7ZFqzzdS3JfoFIV8aNnOc1t9LV+SYC9F2xqV+O5UgZI49+aJkuGf3zAeVIUAOa8BQa5w==", "documentation"=>{"titre"=>"test", "contenu"=>"test"}, "commit"=>"Créer la documentation", "category_id"=>"ruby_on_rails"}
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? LIMIT ? [["slug", "ruby_on_rails"], ["LIMIT", 1]]
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "documentations" ("category_id", "created_at", "updated_at") VALUES (?, ?, ?) [["category_id", 2], ["created_at", "2018-03-20 18:24:06.217357"], ["updated_at", "2018-03-20 18:24:06.217357"]]
(9.9ms) commit transaction
Redirected to http://127.0.0.1:3000/categories/ruby_on_rails
Completed 302 Found in 28ms (ActiveRecord: 11.5ms)
Started GET "/categories/ruby_on_rails" for 127.0.0.1 at 2018-03-20 14:24:06 -0400
Processing by CategoriesController#show as HTML
Parameters: {"id"=>"ruby_on_rails"}
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? LIMIT ? [["slug", "ruby_on_rails"], ["LIMIT", 1]]
Rendering categories/show.html.haml within layouts/application
Rendered documentations/_form.html.haml (2.8ms)
Documentation Load (0.2ms) SELECT "documentations".* FROM "documentations" WHERE "documentations"."category_id" = ? [["category_id", 2]]
Rendered categories/show.html.haml within layouts/application (6.2ms)
Completed 200 OK in 24ms (Views: 22.3ms | ActiveRecord: 0.3ms)
class DocumentationsController < ApplicationController
def create
##category = Category.friendly.find(params[:id])
##documentation = #category.documentation.create(documentation_params)
#category = Category.friendly.find(params[:category_id])
#documentation = #category.documentations.create(params[:documentations_params])
redirect_to category_path(#category)
end
def show
#category = Category.friendly.find(params[:category_id])
##documentation = #category.documentations.friendly.find(params[:id])
end
private
def documentation_params
#params.require(:category[:category_id], :documentation).permit(:titre, :contenu, :slug, :proprietaire)
params.require(:documentation).permit(:titre, :contenu, :slug, :proprietaire)
end
end
This is my form (_form.html.haml)
= form_with(model: [ #category, #category.documentations.build ], local: true) do |f|
%label= :titre
%br
= f.text_field :titre
%br
%br
= f.label :contenu
%br
= f.text_area :contenu
%br
%br
= f.submit 'Créer la documentation'
Thanks for your answers!

Rails action being called multiple times per request

I have a controller action that's being called three times per browser request. Any ideas? config/routes.rb and application_controller.rb are totally straightforward.
Code below,
config/routes.rb
Rails.application.routes.draw do
root 'tiles#index'
+
controllers/tiles_controller.rb
class TilesController < ApplicationController
def index
puts "this line prints three times in the console per request"
end
+
tiles/index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<div id="tiles">
<%= render "tile", tile: { type: "sam", id: "0" } %>
<%= render "tile", tile: { type: "inputs", id: "1" } %>
<%= render collection: #tiles, partial: "tile" %>
<%= render "tile", tile: { type: "social", id: "1000" } %>
</div>
+
This is the console log:
log/development.log
Started GET "/" for 127.0.0.1 at 2017-08-31 16:56:28 -0400
Processing by TilesController#index as HTML
Started GET "/" for 127.0.0.1 at 2017-08-31 16:56:28 -0400
Processing by TilesController#index as HTML
[1m[36mProject Load (0.5ms)[0m [1m[34mSELECT "projects".* FROM "projects" ORDER BY "projects"."launch_date" ASC LIMIT $1[0m [["LIMIT", 10]]
[1m[36mProject Load (9.1ms)[0m [1m[34mSELECT "projects".* FROM "projects" ORDER BY "projects"."launch_date" ASC LIMIT $1[0m [["LIMIT", 10]]
Rendering tiles/index.html.erb within layouts/application
Rendered tiles/_tile_sam.html.erb (0.3ms)
Rendered tiles/_tile.html.erb (8.0ms)
Rendering tiles/index.html.erb within layouts/application
Rendered tiles/_tile_sam.html.erb (0.0ms)
Rendered tiles/_tile.html.erb (0.9ms)
Rendered tiles/_tile_instagram.html.erb (12.6ms)
...
Rendered tiles/_tile_twitter.html.erb (0.5ms)
Rendered collection of tiles/_tile.html.erb [48 times] (125.9ms)
Rendered tiles/_tile_project.html.erb (0.1ms)
Rendered tiles/_tile_social.html.erb (0.6ms)
Rendered tiles/_tile.html.erb (2.7ms)
Rendered tiles/index.html.erb within layouts/application (166.1ms)
Rendered tiles/_tile_twitter.html.erb (0.6ms)
...
Rendered collection of tiles/_tile.html.erb [48 times] (158.5ms)
Rendered tiles/_tile_social.html.erb (0.1ms)
Rendered tiles/_tile.html.erb (1.0ms)
Rendered tiles/index.html.erb within layouts/application (165.3ms)
Completed 200 OK in 1310ms (Views: 217.1ms | ActiveRecord: 9.1ms)
Completed 200 OK in 1325ms (Views: 204.5ms | ActiveRecord: 0.5ms)
Help!
WOW
Finally figured this one out by removing code related to this action/view line by line. Basically, I was generating a few img tags with empty src fields. Apparently this causes many modern browsers to load a website several times, possibly in an attempt to find missing images or assuming the root URL is the image itself?
Found the behavior cited here:
https://forums.asp.net/t/1804472.aspx?Bizzare+safari+thing+two+page+loads+for+each+page+
page loads twice in Google chrome

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!

Rails Controller Action being called twice

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

Resources