How to download the generated CSV file in Rails? - ruby-on-rails

I am trying to download the CSV file processed by the ReportExport service, in the logs the sent data is a "Sent data Business analyst-report-2022-12-12.csv". But the file is not downloaded remotely.
reports_controller.rb
class ReportsController < ApplicationController
def export
status = HoursUploader.new(params[:hours_file]).call
return redirect_to reports_path, alert: status if status != TasksUploader::OK
total_hours = Hour.last
status = HoursDistributor.new(total_hours).call
return redirect_to reports_path, alert: status if status != HoursDistributor::OK
send_data ReportExport.new(total_hours).to_csv, filename: "#{total_hours.position_english}-report-#{Time.zone.today}.csv", content_type: 'text/csv'
end
end
_form.html.erb
<%= form_with url: export_reports_path, method: :post do |f| %>
<form action="#" class="space-y-7">
<div class="mt-3">
<%= f.label :hours_file, class: "block my-2 text-sm font-medium text-gray-900 dark:text-gray-300"%>
<%= f.file_field :hours_file, direct_upload: 'false', class: "block w-full p-1.5 text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 cursor-pointer dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400" %>
</div>
<button type="submit" class="mt-10 flex w-full items-center justify-center rounded-md border border-transparent bg-indigo-600 py-3 px-8 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
<%= f.submit "Create" %>
</button>
</form>
<% end %>
ReportExport generates the required CSV file
I added data: {turbolinks: false}, disposition: attachment, did submit with a link and a button but it didn't help. Also it worked before I added styles.
<%= form_with url: export_reports_path, method: :post do |form| %>
<%= form.label :hours_file, "Hours" %>
<%= form.file_field :hours_file %>
<%= form.button "Create" %>
<% end %>
How do I fix the error? Thank you in advance!

Related

simple form - Reddit-like site

I completed a search and found this topic that is close, How do I make simple form to use, I'm too new to fully understand
what I was looking at.
The answer was as follows:
Step 1
rails generate simple_form:install --bootstrap (which I did after adding the Gem and running Bundle Install)
Step 2
Told me to add wrappers (not sure if I need to complete this step)
Step 3
modify the simple_form and has this example:
<%= simple_form_for(#contact, url: supplier_contacts_path, html: { class:
'form-horizontal' }) do |f| %>
<div class="form-inputs">
<%= f.input :c_regular,wrapper: :horizontal_input_group do %>
<span class="input-group-addon">$</span>
<%= f.input_field :c_regular, :label => "Regular",class: "form-control" %>
<% end %>
</div>
<% end %>
My code is as follows(views/comments/show.html.erb):
<div id="comments">
<%= render :partial => #link.comments %>
</div>
<%= simple_form_for(#link, html {class: 'form-horizontal' }) do |f| %>
<div class="field">
<%= f.text_area :body, size: "60x12" %>
</div>
<%= f.submit "Add Comment", class: 'btn btn-primary" %>
<% end %>
Here's my partial(views/comments/_comments.html.erb):
<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
<div class="pull-left">
<p class="lead"><%= comment.body %></p>
<p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %>
ago</strong> by
<%= comment.user.email %></small></p>
</div>
<div class="btn-group pull-right">
<% if comment.user == current_user %>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you
sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
<% end %>
</div>
<% end %>

Getting undefined method `[]' for nil:NilClass even when the instance variable is defined

I am trying to call a controller action at two different places.
The controller is as follows:
def create
#phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number])
#phone_number.generate_pin
#phone_number.send_pin
respond_to do |format|
format.js # render app/views/phone_numbers/create.js.erb
end
end
I am trying to call create on a link in view:
<%=link_to "Resend Pin", phone_numbers_path, method: :post %>
My rake routes output looks like this:
phone_numbers POST /phone_numbers(.:format) phone_numbers#create
new_phone_number GET /phone_numbers/new(.:format) phone_numbers#new
phone_numbers_verify POST /phone_numbers/verify(.:format) phone_numbers#verify
I have data in PhoneNumber activerecord. When I click on "Resend Pin" I am getting following error:
NoMethodError in PhoneNumbersController#create
undefined method[]' for nil:NilClass`
Can somebody please tell me what am I missing here ?
html.erb
<div id="send-pin">
<h3>What's your phone number?</h3>
<%= form_for #phone_number, remote: true do |f| %>
<div class="form-group">
<%= f.text_field :phone_number %>
</div>
<%= f.submit "Send PIN", class: "btn btn-primary", id: 'send-pin-link' %>
<% end %>
</div>
<div id="verify-pin">
<h3>Enter your PIN</h3>
<%= form_tag phone_numbers_verify_path, remote: true do |f| %>
<%= hidden_field_tag 'hidden_phone_number', '' %>
<div class="form-group">
<%= text_field_tag :pin %>
</div>
<%= submit_tag "Verify PIN", class: "btn btn-primary" %>
<% end %>
<%=link_to "Resend Pin", phone_numbers_path, method: :post %>
</div>
params[:phone_number][:phone_number] is null.
Try the below in view
<div id="send-pin">
<h3>What's your phone number?</h3>
<%= form_for #phone_number, remote: true, html: {id: "first_form"} do |f| %>
<div class="form-group">
<%= f.text_field :phone_number %>
</div>
<%= f.submit "Send PIN", class: "btn btn-primary", id: 'send-pin-link' %>
<% end %>
</div>
<div id="verify-pin">
<h3>Enter your PIN</h3>
<%= form_tag phone_numbers_verify_path, remote: true do |f| %>
<%= hidden_field_tag 'hidden_phone_number', '' %>
<div class="form-group">
<%= text_field_tag :pin %>
</div>
<%= submit_tag "Verify PIN", class: "btn btn-primary" %>
<% end %>
<%=link_to "Resend Pin",'#', id: "resend_pin_code" %>
</div>
<div id="status-box" class="alert alert-success">
<p id="status-message">Status: Haven’t done anything yet</p>
</div>
<script>
$("#resend_pin_code").click(function(){
$("#first_form").submit();
}
)
</script>

Problems with Modal in Rails opening the same object

I'm maintaining an older Rails 3.2.22 app and I'm trying to add a modal to pop up with a simple form to add a note.
Here is my view (cut down for readability)
<% #assigned.each do |call| %>
<%= link_to "Notes", '#ajax-modal', data: { toggle: "modal", target: "#note-modal" }, class: 'btn btn-small btn-inverse' %>
<%= render 'layouts/note_modal', :call => call %>
<% end %>
Here is my modal app/views/layouts/_note_modal.html.erb
<div id="note-modal" class="modal hide fade" tabindex="-1">
<div class='modal-header'>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title"><%= call.incident_number %> Notes</h3>
</div>
<div class='modal-body'>
<div class="modal-body-content">
<%= form_for(#note, url: call_note_calls_path, html: {class: "form-inline"}) do |f| %>
<%= f.text_field :message, :maxlength => 255 %>
<%= f.hidden_field :call_id, value: call.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.button "Add Note", class: 'btn btn-info btn-small', data: {disable_with: "<i class='icon-spinner'></i>Posting..."} %>
<% end %></div>
<div class="ajax-loader">
</div>
</div>
<div class='modal-footer'>
<button type="button" data-dismiss="modal" class="btn">Close</button>
</div>
</div>
When I load the page and there are several calls I click on the notes link, the partial renders in a modal and I can submit a note. The problem is if I have 10 calls on the screen no matter which notes link I click on it adds a note to the first call on the screen.
Am I passing locals incorrectly? I know this code is inside of the block so it should pull the call object and pass it as a local to the partial in the modal but it's not.
Any help is greatly appreciated.
I think the issue is that you render the modal each time for a note but they all have the same id. So you are always opening the first modal. Actually IMHO you just need to render the modal once and pass the note_id via an data attribute on the link which triggers the modal. So basically configure the modal via the caller.
I was able to figure this out on my own and still render the partial within the loop by calling an index as part of the target ID for the modal. Working code below.
<% #assigned.each_with_index do |call, index| %>
<%= link_to "Notes", '#note-modal', data: {toggle: "modal", target: "#note-modal#{index}" }, class: 'btn btn-small btn-inverse' %>
<%= render 'layouts/note_modal', call: call, index: index %>
<% end %>
<div id="note-modal<%= index %>" class="modal hide fade" tabindex="-1">
<div class='modal-header'>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title"><%= call.incident_number %> Notes</h3>
</div>
<div class='modal-body'>
<div class="modal-body-content">
<%= form_for(#note, url: call_note_calls_path, html: {class: "form-inline"}) do |f| %>
<%= f.text_field :message, :maxlength => 255 %>
<%= f.hidden_field :call_id, value: call.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.button "Add Note", class: 'btn btn-info btn-small', data: {disable_with: "<i class='icon-spinner'></i>Posting..."} %>
<% end %></div>
<% call.notes.each do |n| %>
<li><%= n.message %> added by <%= n.user.username %></li>
<% end %>
<div class="ajax-loader">
</div>
</div>
<div class='modal-footer'>
<button type="button" data-dismiss="modal" class="btn">Close</button>
</div>
</div>

carrierwave AJAX upload with remotipart

In my rails4 I would like to submit a form via AJAX that has a file uploaded via carrierwave. As far as I know it can be done via remotipart, but can't figure it out how I can do that. (Remotipart installed properly since it's working with refile gem that I also use in the app.)
With the current code below the form gets submitted according to the logs and it says the create.js.erb is rendered, but the code in the create.js.erb file never runs.
Rendered posts/create.js.erb (387.0ms)
What should I do?
form
<%= form_for #post, remote: true, method: :post, :html => { :multipart => true, class: "post-create-form" } do |f| %>
<img id="img_prev" style="margin-bottom:10px;" width=300 height=200 src="#" class="img-thumbnail hidden"/>
<%= f.text_area :body, placeholder: "Share something useful..", class: "form-control post-create-body" %>
<%= f.button "SHARE", class: "btn btn-primary btn-sm btn-create-post", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Saving..."} %>
<span class="btn btn-success btn-sm btn-message-file">Upload Image
<%= f.file_field :post_image, class: "choose-message-file", id: "avatar-upload" %>
</span>
<%= f.hidden_field :post_image_cache %>
<% end %>
create.js.erb
$('ul.errors').hide();
$('.alert-danger').hide();
$('.post-index').prepend('<%= j render #post %>');
$('.post-create-body').val('');
$('.btn-create-post').prop('disabled',true);
_post.html.erb
<div class="col-md-2">
<%= link_to user_path(post.user) do %>
<%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
<% end %>
</div>
<div class="col-md-8">
<div class="post-info">
<%= link_to user_path(post.user) do %>
<span class="post-user-name"><%= post.user.full_name %></span>
<% end %>
<span class="post-updated"><%= local_time_ago(post.updated_at) %></span>
</div>
<div class="post-body">
<%= simple_format(find_links(h post.body)) %>
</div>
<div class="post-image" data-postlink="<%= post_path(post) %>">
<% if post.post_image? %>
<button data-toggle="modal" data-target="#post-image-modal">
<%= image_tag post.post_image.url(:base_thumb) %>
</button>
<% end %>
</div>
</div>
UPDATE:
The problem is connected to updating the post object. The update form code can be found in the _post.html.erb, so that's why <%= j render #post %> didn't work. Still don't know what the problem is.
So what works:
1. creating post that has no image
2. updating post that has no image
3. updating post that has image BUT image is not edited
Not working:
1. creating post that has image
2. updating post that has image which is edited
<%= form_for post, method: :patch, remote: true, :html => { :multipart => true } do |f| %>
<div class="modal fade updatepost" id="updatepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content" style="text-align:left">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="edittaskmodalohhhhh">Edit Task</h4>
</div>
<div class="modal-body">
<div class="alert alert-danger" style="display:none">
<ul class="errors" style="display:none">
<%= render 'layouts/error_messages', object: f.object %>
</ul>
</div>
<div class="field form-group">
<%= f.file_field :post_image, id: "avatar-upload-update" %>
<%= f.hidden_field :post_image_cache %>
</div>
<div class="field form-group">
<%= f.text_area :body, class: "form-control edit-post-body" %>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="updatepostclose">Close</button>
<%= f.button "Update post", class: 'btn btn-primary edit-post-submit', data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Saving..."} %>
</div>
</div>
</div>
</div>
<% end %>
update.js.erb
$("ul.errors").html("");
<% if #post.errors.any? %>
$('.alert-danger').show();
$('ul.errors').show();
<% #post.errors.full_messages.each do |message| %>
$("ul.errors").append($("<li />").html("<%= message.html_safe %>"));
<% end %>
<% else %>
$('ul.errors').hide();
$('.alert-danger').hide();
$('#updatepost_<%= #post.id %>').modal('hide');
$post = $('<%= j render #post %>');
editPostHideDanger($post);
$('#post_<%= #post.id %>').remove();
$(".new-post-insert").prepend($post);
$("html, body").animate({ scrollTop: 0 }, "slow");
<% end %>
There was some issue (still don't know what) with the bootstrap modals. I changed the structure of the modals and now it's working fine.

undefined method `merge' for 35:Fixnum

Found it
Don't use <%= f.hidden_field :field, number %> , use <%= f.hidden_field :field, value: number %>
Issue below
An ActionView::Template::Error occurred in bookings#new:
undefined method `merge' for 35:Fixnum
app/views/bookings/_form.html.erb:31:in `block in _app_views_bookings__form_html_erb__2731573742378725623_70113682151640'
Getting this horrible general error from our production website, and it's not clear why. It's not happening on our local hosts. Here's the line referenced above:
<%= current_employer.locations.first.name_or_address_1 %>
where name_or_address_1 is:
return "from #{name}" if name.present?
"from #{address_1}"
I've gone into the console and run "name_or_address_1" for every location in our database, which works fine, and "locations.first.name_or_address_1" for every employer in our database. Again, works fine. So surely it can't be this line?
Edit: I just commented out the line, deployed to production, and the error still occurs. What's going on? Why is the wrong line being referenced?
Here's the partial:
<%= form_for #employer, url: bookings_path, method: :post, html: { class: "main-form", id: "create-booking" } do |f| -%>
<% #employer.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<div id="bookings">
<ol class="numbers">
<li>
<legend>Location, Pay, & Vehicle</legend>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Type of job</label><br>
<%= f.select(:job_type_id, options_from_collection_for_select(JobType.all, :id, :name_with_delivery), {}, { id: 'job-type', class: 'form-control' }) %>
</div>
<div class="col-sm-6">
<label>Vehicle needed</label><br>
<%= f.select(:vehicle_id, options_from_collection_for_select(Vehicle.all, :id, :name), {}, { id: 'vehicle-type', class: 'form-control' }) %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Location</label>
<% if current_employer.locations.size > 1 %>
<%= f.select :location_id, options_from_collection_for_select(current_employer.locations.all, :id, :name_or_address_1), {}, { class: 'form-control' } %>
<% elsif current_employer.locations.size == 1 %>
<p><strong>Location: </strong><%#= current_employer.locations.first.name_or_address_1 %></p>
<%= f.hidden_field :location_id, current_employer.locations.first.id %>
<% end %>
<%= link_to "or add new location", new_employer_location_path(current_employer, Location.new) %>
</div>
<div class="col-sm-6">
<%= f.label :pay %>
<span id="length-message" class="pull-right" style="color: #a94442"></span>
<br>
<%= f.text_field :pay, class: 'form-control', id: 'pay', maxlength: '18' %>
</div>
</div>
</div>
</li>
<legend>Shifts</legend>
<%= render 'booking', booking: Booking.new %>
</ol>
</div>
<%= link_to "Add another shift", "javascript:;", class: 'add-shift', style: 'margin-left: 40px;' %>
<script type="text/javascript">
$(".add-shift").click(function(){
$("ol.numbers").append("<%= j render 'booking', booking: Booking.new %>")
});
</script>
<%= f.submit "Post Shifts", class: 'btn green-button pull-right' %>
<br>
<br>
<br>
<span class="error-message bg-danger pull-right">
</span>
<% end %>
Don't use
<%= f.hidden_field :field, number %>
use
<%= f.hidden_field :field, value: number %>

Resources