Rails - Converting ransack result to xls - ruby-on-rails

I already tried:
<%= link_to "Download Excel File", yours_path(params.merge(:format => 'xls')) %>
which does not take into account the search form I have done on the same screen (it downloads everything).
Search form (works perfectly well, I get the right results on the page):
<%= search_form_for #q, url: yours_path do |f| %>
<div>
<%= f.search_field :last_name_cont, placeholder: "Type a last name" %>
</div>
I also already tried:
make xls
which tries to use the URL to only download the right results but it downloads everything.
Index:
def index
#q = Member.ransack(params[:q])
end
I would like to download only the result from ransack instead of all the objects.

Use this gem axlsx_rails for xlsx output and templates.
https://github.com/straydogstudio/axlsx_rails/blob/master/README.md

Related

How to set mail attachments without saving file in database?

I have created simple application form and i was trying to upload file and send it as email attachment. And i did it with this approach:
class ApplyController < ApplicationController
def prepare_email_content
ApplyMailer.with(params).apply.deliver_now
end
end
class ApplyMailer < ApplicationMailer
def apply
#company = Company.find(params[:company_id])
#candidate = params[:name]
#candidate_mail = params[:email]
#email = #company.email
attachments[params[:cv].original_filename] = params[:cv].read
mail to: #email, subject: 'Hello'
end
end
<h1>APPLY</h1>
<%= form_tag(apply_path, method: :post, multipart: true) do %>
<%= label_tag(:name, "First and last name:") %>
<%= text_field_tag(:name) %>
<%= label_tag(:email, "Email:") %>
<%= text_field_tag(:email) %>
<%= hidden_field_tag :company_id, params[:company_id] %>
<%= file_field_tag 'cv' %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= link_to 'All offers', hello_path %>
Everything was working fine - i have tested application and it was fine. Then i have developed my application and when i come back to testing i started getting this error:
On the way i have installed some gems and updated few of them. I was checking out to commit where this feature was working and it is. But i'm not able to find any differences in my code. There were some changes in /.idea folder but i don't know if any of this files could trigger this issue.
I'm using rails 6.0.3 and ruby 2.5.8
EDIT
I can see that there is a problem inside called methods. Looks like it cannot find #sort_order value and it sets data value as nil. But i have no idea how to change working of this.
Replace the line you're attaching the file with
attachments["#{params[:cv].original_filename}"] = File.read(params[:cv].path)
Even though you're not saving the file, there is a still a local path to grab the file from.
Also, you should really consider passing the parameters to the mailer from the controller, rather than passing params in its entirety. That way if the information or format of the upload is incorrect, you can redirect back to the form and bypass sending the email.

How to submit a form multiple time in Ruby on Rails

I have a form made with simple_form that accept csv file to be converted it in an xlsx file (operating some operation to manipulate data).
<%= simple_form_for order, url: convert_orders_url, html: {multipart: true} do |f| %>
<%= f.file_field :file %>
<%= f.submit "Convert" %>
<% end %>
In my controller:
def convert
filename = #call to function that return the file path
send_file filename
end
The problem I have is that I need to upload multiple file one by one.
Anyway every time I need to reload the page manually cause I cannot submit the form multiple times and I can't reload the page in the controller because it raise a Double Render Error, because of the "send_file".
Is there a way to submit a form multiple time without reload the page in Ruby on Rails?
You can use remote: true on your form to submit the form using AJAX without reloading the page. However, with file uploads you'll find that remote: true won't work out of the box and you'll need something like remotipart.

uploading empty file goes to edit instead of new

I recently noticed that on my form, if I try to upload an empty file the page will get redirected to edit instead of create. If I try to upload the file with some text in it, the form will direct to create. I couldn't find any indication that this would happen as I create my object every time (it's never persisted). Is there an explanation behind this?
The code looks something like this:
//controller
def upload
#new_cool_file = CoolFile.new
end
//form in upload.html.erb
<%= form_for #new_cool_file, html: {role: "form"} do |f| %>
<div class="form-group">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<%= f.submit "Submit"%>
<% end %>
I think you do some wrong in the rails routes.rb file , can you please see tutorial about file upload.
https://richonrails.com/articles/allowing-file-uploads-with-carrierwave
http://railscasts.com/episodes/253-carrierwave-file-uploads

ExternalQuestion form not displaying on Amazon Mechanical Turk

So I'm currently trying to post a HIT to mturk using my rails app and the gem 'rturk'. The entire process works smoothly, except when I open the HIT's amazon url, my rails app's form doesn't show up in the iframe. It's just a blank white iframe. When I check the source of that page, I can clearly see the correct link with assignmentId, hitId, and workerId in it. So there is no reason that it shouldn't be displaying, right? Here is part of the form to display on mturk:
<%= form_for #results, :url => 'https://workersandbox.mturk.com/mturk/externalSubmit', html: {class: "takeTest takeScale"} do |f| %>
<%= hidden_field_tag 'assignmentId', #assignment_id %>
<%= hidden_field_tag 'hitId', #hit_id %>
<%= hidden_field_tag 'workerId', #worker_id %>
<%= f.submit("Submit Answers", class: 'btn btn-info') %>
And here is part of my controller creating that form as a HIT using rturk:
#post to amazon mturk
RTurk.setup('MYKEY', 'MYOTHERKEY', :sandbox => true)
#hit = RTurk::Hit.create(:title => "#{#scale.title}") do |hit|
hit.max_assignments = 5
hit.description = 'post'
hit.question("https://my-rails-app.herokuapp.com/tests/scale/take/#{#scale.id}",
:frame_height => 500) # pixels for iframe
hit.reward = 0.05
hit.qualifications.add :approval_rate, { :gt => 80 }
end
redirect_to root_url, :notice => "posted at: #{#hit.url} !!!"
I am wondering if it has something to do with my form or if it is something to do with hosting on heroku? Any help would be much appreciated. Here is the source of an iframe on Amazon by the way, to show that it should all be working:
<iframe height="500" scrolling="auto" frameborder="0" align="center" src="https://my-rails-app.herokuapp.com/tests/scale/take/7?assignmentId=2IBHSTLB3L0FPI5VSJAEMREVGFBIU0&hitId=2FC98JHSTLB3ZZBONH9OEJEJ4E6FRO&workerId=A1GHEKG3TRCQ5Y&turkSubmitTo=https%3A%2F%2Fworkersandbox.mturk.com" name="ExternalQuestionIFrame"></iframe>
Thank you!
I am able to confirm same issue with turkee gem on Rails 4.2
The solution was as #AlexTheGoodman noted in the comments above.
Modify your ~/config/application.rb and add the following:
class Application < Rails::Application
config.action_dispatch.default_headers.clear
end

Rails 3.0.1 set default file on a file_field

I have two rails applications which are connected through an API. In the first rails app I have many users with avatar images. Now I want to send them through a file_field (in form_for) to the second rails app.
The problem is that I don't want manualy select the image-path for each user. Better would be if the image_path would be automaticaly inserted when the page is loaded. At the moment there is for each user a page where I select his avatar image and click on send.
The location of the images is on the first rails server. So this means that I want to send avatar images from one rails server to another with a file_field.
Is that somehow possible?
If you are trying to set a default image when opening a record for editing you have to do the following
In your edit view:
<%= form_for(#photo, :html => { :multipart => true }) do |f| %>
<%= f.text_field :name %>
<%= f.file_field :image %>
<%= f.submit "Change" %>
<% end %>
In your controller:
#photo = Photo.find(params[:id])
When you form gets displayed it will automatically assign the image in the database to the file_field.
Although the form might show "No File Selected", When you update your form your original image will still be there.

Resources