I am importing a file using SmarterCSV and I have 1 function that validates certain aspects of the file, and then redirects you, displaying the results.
I would like this display to also include a button that says "import", which allows you to import that same file as long as you are satisfied with what is displayed.
How can I pass the file to the second function after redirect without having to select the file again?
# validate file and check display
def check_file
#success, #error = Timecard.check_timecard(params[:file])
redirect_to timecards_path, notice: [#success, #error]
end
#import into database if display is satisfactory
def bulk_upload
x = Timecard.import(params[:file])
redirect_to timecards_path, notice: "Times imported successfully."
end
#view showing display
<% if flash[:notice].is_a? String %>
<%= flash[:notice] %>
<% elsif flash[:notice].is_a? Array %>
<div class="container">
<div class="col-xs-10 col-xs-offset-1">
<table class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Regular</th>
<th>Overtime</th>
<th>Sick</th>
<th>Vacation</th>
<th>Holiday</th>
</tr>
</thead>
<tbody>
<% notice[0].each do |success| %>
<tr>
<td style="color: green"><%= success[0] %></td>
<% success[1].each do |type| %>
<td><%= type %></td>
<% end %>
</tr>
<% end %>
<% notice[1].each do |failure| %>
<tr>
<td style="color: red"><%= failure[0] %></td>
<td><%= success[1] %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="container-fluid">
<div class="col-xs-12 col-md-6 col-md-offset-3 text-xs-center">
<div class="card card-nav-tabs">
<div class="header header-info">
Import Timecard and Send Email
</div>
<div class="content">
<!-- IMPORT GOES HERE -->
</div>
</div>
</div>
</div>
</div>
<% end %>
There is a nice way to do this. You should move the file to a location within your application and then, store the path of the file in the database against the record you are trying to upload this file to. Consider the following example. Assuming you are uploading this file to a TimeCard record and an instance exist as #time_card
name = params[:upload][:file].original_filename
directory = "public/images/upload"
path = File.join(directory, name)
File.open(path, "wb") { |f| f.write(params[:upload][:file].read) }
#time_card.csv_path = path
Then, redirect the user to the validation error page. Then, after use submits the page you can import the CSV using the file in #time_card.csv_path
Related
In my home page I iterate over collections of objects, and for each object I render its attributes in a table row. There are four collections of objects, defined as instance variables in my controller, all making Guard (according to the used method) raising one of the following errors:
ActionView::Template::Error: undefined method `each_with_index' for nil:NilClass
ActionView::Template::Error: undefined method `any?' for nil:NilClass
The code in my application view raising the above errors is:
<table class="col-md-4 col-md-offset-1">
<thead>
<tr>
<th> Rank </th>
<th> Gamer </th>
<th> Points </th>
</tr>
</thead>
<tbody>
<% #atp_gamers.each_with_index do |gamer, index| %>
<tr>
<td class="index"> <%= index+1 %> </td>
<td class="gamer"> <%= gamer.name %> </td>
<td class="atppoints"> <%= gamer.atpscore %> </td>
</tr>
<% end %>
<tr class="current-user">
<td> <%= #atp_gamers.to_a.index(current_user) + 1 %> </td>
<td> <%= current_user.name %> </td>
<td> <%= current_user.atpscore %> </td>
</tr>
</tbody>
</table>
<table class="col-md-4 col-md-offset-2">
<thead>
<tr>
<th> Year </th>
<th> Champion </th>
<th> Points </th>
</tr>
</thead>
<tbody>
<% if #atp_champions.any? %>
<% #atp_champions.each do |champion| %>
<tr>
<td class="year"> <%= champion.created_at.year %> </td>
<td class="winnername"> <%= champion.name %> </td>
<td class="winnerpoints"> <%= champion.points %> </td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
The above code is part of a partial (named _gamers_home.html.erb) rendered in the original home page:
<% if logged_in? %>
<% if current_user.gamer? %>
<%= render 'static_pages/gamers_home' %>
<% else %>
<%= render 'static_pages/non_gamers_home' %>
<% end %>
<% else %>
<%= render 'static_pages/non_logged_in_home' %>
<% end %>
The logged_in? method is defined as !current_user.nil?
The instance variables that result nil are: #atp_gamers, #wta_gamers, #atp_champions and #wta_champions, defined in the controller as follows:
def home
if logged_in? && !current_user.gamer?
...l
elsif logged_in? && current_user.gamer?
#gamers = User.where(gamer: true)
#atp_gamers = #gamers.order(atpscore: :desc).limit(50)
#wta_gamers = #gamers.order(wtascore: :desc).limit(50)
#atp_champions = AtpChampion.all
#wta_champions = WtaChampion.all
...
end
end
The first instance variable raising the error (each_with_index' for nil:NilClass) is #atp_gamers. In view I tried to change it with its explicit value, that is User.where(gamer: true).order(atpscore: :desc).limit(50), and the respective code is accepted. After this change, Guard raises an error for #atp_champions.
With rails console #atp_gamers and #wta_gamers are not empty, returning 50 records out of 100 users. #atp_champions and #wta_champions are not nil, but empty arrays.
I suspect that this might be an issue raised only by Guard, because the rails server succeeds in rendering the view.
def home
if logged_in? # delete this line
...
end # delete this line
end
Delete the if logged_in?, and see what happens.
Maybe you have to use before_action :logged_in_user, only :home in controller and define the logged_in_user method as private method.
If non-logged-in users also allowed to access the home action, you need to use if statement erb in the view. Like,
<% if logged_in? %>
<% #atp_gamers.each_with_index do |gamer, index| %>
...
<% end %>
--UPDATE--
Maybe, it needs to toss variables to the partial.
Replace
<%= render 'static_pages/gamers_home' %>
to
<%= render partial: 'static_pages/gamers_home', locals: {atg_gamers: #atp_gamers, wta_gamers: #wta_gamers, atp_champions: #atp_champions, wta_champions, #wta_champions} %>
and, replace the #atp_gamers, #wta_gamers, #atp_champions, #wta_champions in the partial to atp_gamers, wta_gamers, atp_champions, wta_champions.
Try and see what happens.
I've coded in a page in which data from a database is pulled and subsequently when I click on what is displayed it enters a corresponding value into the search function on the application and displays the results, the code can be seen below:
course view:
<!-- Index of all Courses -->
<% provide(:title, "Course") %>
<!--containers for design/layout -->
<div class = "signinstyle">
<div class = "row">
<!--Page information -->
<%= form_tag(degree_new_path, :method => "get", id: "search-data") do %>
<table border="1" class="table">
<thead>
<tr>
<th>Courses</th>
</tr>
</thead>
<tbody>
<% #ads.each do |degree| %>
<tr>
<td> <%= link_to degree.cname, keyword_search_path(search: degree.cname) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Select" %>
<% end %>
<!--closing the design/layout containers -->
</div>
</div>
degree controller (the above view is within this):
class Degree < ActiveRecord::Base
def Degree.search(search)
where("cname LIKE ? OR ucas LIKE ?", "%#{search}%", "%#{search}%")
end
end
search controller (as I use my keyword search in the view):
def keyword_search
#search = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
#search_degree = Degree.search(params[:search]).order('cname ASC')
end
end
def course
#select = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
#select_degree = Degree.search(params[:search])
end
end
I'm trying to replicate the above code so I can click on similar links which will enter data into the ransack search function I have but have been unable to do so, if anybody could help me out it would be appreciated. Below is the code I'm currently trying to get to work:
Searches controller:
def adsearch
#adsearch = Degree.ransack(params[:q])
#data = #adsearch.result
#adsearch.build_condition if #adsearch.conditions.empty?
#adsearch.build_sort if #adsearch.sorts.empty?
end
the view file:
<!-- Index of all Courses -->
<% provide(:title, "Course") %>
<!--containers for design/layout -->
<div class = "signinstyle">
<div class = "row">
<!--Page information -->
<%= form_tag(degree_new_path, :method => "get", id: "search-data") do %>
<table border="1" class="table">
<thead>
<tr>
<th>Courses</th>
</tr>
</thead>
<tbody>
<% #ads.each do |degree| %>
<tr>
<td> <%= link_to degree.subject_group, adsearch_path(name: ucas & value: degree.ucas_letter) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Select" %>
<% end %>
<!--closing the design/layout containers -->
</div>
</div>
With the last two exerts of code it displays what I'm asking it to display on the initial view but doesn't enter the value I wish it to enter into the ransack search and as such doesn't create a search when clicked upon like the first example does.
I have a Form with some checkboxes loaded from the Database, and an option to add other items manually to the items table. This is done by Ajax in the code below...
item_controller.rb
def manual_item_add
#manual_item = Item.find(params[:id_item].to_i)
respond_to do |format|
format.js
end
end
manual_item_add.js.erb
$("#items_table").append("<%= escape_javascript(render 'manual_item_add', :manual_item => #manual_item) %>")
_manual_item_add.html.erb
...
<td><%= check_box_tag("manual_items[]", item.id, true) %></td>
...
edit_items.html.erb
<%= form_tag( {controller:"item", action:"edit_items"}) do %>
<p align="center">
<%= select_date(#account.start_date, prefix: 'start_date') %>
to
<%= select_date(#account.end_date, prefix: 'end_date') %>
</p>
<%= hidden_field_tag 'id_account', #account.id %>
<table id="items_table" class="subtable" align="center" width="55%">
....
<tr>
<th colspan="6">Items added manually</th>
</tr>
<tr>
<th># ID</th>
<th>Type</th>
<th>Description</th>
<th>Ammount</th>
<th>Date</th>
<th>Include in the account</th>
</tr>
</table>
<p align="center"><%= submit_tag("Save", class: "btn") %></p>
<% end %>
<%= form_tag( {controller:"item", action:"manual_item_add"}, method:"get", remote: true) do %>
<h4 align="center">Add item manually</h4>
<p align="center">Item ID:
<%= text_field_tag "item_id", nil , size:5, maxlength:5 %>
<%= submit_tag("Add Item", class: "light_btn") %>
</p>
<% end %>
So... the problem here is that though I see the new checkboxes i am adding to the table (they are being created normally), the "manual_items[]" array is not being passed to the controller when I submit the resulting form (by the way, the "items_table" is inside the form definition).
Does anybody know what I am doing wrong? Sorry for the newbie question, I'm starting to work with Ruby + Rails.
Unfortunately, I don't have a definitive answer for this problem. The only working solution I've tried was to use JQuery to force parameters to be part of the form:
$(document).ready(function() {
$("#edit_items_form").submit(function(event) {
$(this).append($("input[name='manual_items[]']:checked"));
$(this).submit();
});
});
I am definitely not comfortable to this solution, I'd like to know why these Ajax checkboxes are not automatically recognized as being part the form.
Good day, there are a lot of questions on this problem here, but they don't help me.
For signed_in user I have a homepage with bookmarks and folders, which contains bookmarks belonged to this folder.
home method is in static_pages controller, which looks like this:
def home
if signed_in?
#folder = current_user.folders.build
#folders = current_user.folders.all
#bookmark = current_user.bookmarks.build
#bookmarks = current_user.bookmarks.all
end
end
This is part of home.html.erb
<div class="span3">
<table class="table table-hover table-condensed">
<%= render #bookmarks %>
</table>
</div>
<div class="span3">
<table class="table table-hover table-condensed">
<%= render #folders %>
</table>
</div>
So I have 2 tables with all bookmarks and all folders, belonged to current_user
This is _folder.html.erb from folders folder in views
<tr data-toggle="tooltip" data-placement="right" data-html="true">
<td data-href="<%= folder %>" class="bookmark"><i class="icon-folder-close"></i>
<%= link_to folder.name, folder_path %>
</td>
<td>
<%= link_to '<i class="icon-edit"></i>'.html_safe, '#' %>
</td>
<td>
<%= link_to '<i class="icon-trash"></i>'.html_safe, folder_path, method: :delete,
data: {confirm: 'Are you sure?'},
title: 'Delete folder ' + folder.name.to_s %>
</td>
</tr>
folders_controller.rb, with show method:
def show
#folder = Folder.find(params[:id])
#bookmarks = #folder.bookmarks.all
end
also resources :folders exists routes.rb
All I need is to go to localhost:3000/folder/1, when clicking on folder_name on homepage.
This is what I get:
Routing Error
No route matches {:action=>"show", :controller=>"folders"}
In rake routes everything is ok.
When put localhost:3000/folder/1 in browser, it works correctly.
Thank you!
Try Replacing:
<%= link_to folder.name, folder_path %>
with
<%= link_to folder.name, folder_path(folder) %>
as you need to provide the id to the show action.
You can get more information here
I have a problem where on each users profile they have an edit button for a section called wishlist, my problem is that when the user tries to click on the edit button it returns an error saying it cannot find wishlist with id e.g. 34 which is the user.id not the wishlist.id. But from what I can understand from the code that is not what I have wrote.
My code is as followed here:
users/show.html.erb
<div class="post3">
<h1><%= t('.mwg')%></h1><br />
<div id = "table-2">
<table width="240" >
<tbody>
<% Wishlist.where(:user_id => #user).each do |wishlist| %>
<tr><td width="30"><b>1:</b></td><td><%= wishlist.number_1 %></td></tr>
<tr><td width="30"><b>2:</b></td><td><%= wishlist.number_2 %></td></tr>
<tr><td width="30"><b>3:</b></td><td><%= wishlist.number_3 %></td></tr>
<tr><td width="30"><b>4:</b></td><td><%= wishlist.number_4 %></td></tr>
<tr><td width="30"><b>5:</b></td><td><%= wishlist.number_5 %></td></tr>
</tbody>
</table>
</div>
<%if current_user %>
<% if current_user.id == wishlist.user_id %>
<div id="text3"><%= link_to t('.edit'), edit_wishlist_path(#wishlist) %></div><br />
<%end%>
<%end%>
<%end%>
No I tried changing the current_user.id == wishlist.user_id line to the following:
<% if current_user.id == #wishlist.user_id %>
But I get the error of undefined method user_id
If I click on the edit for the original code it tries to find a wishlist who's id matches that of the user but I want it to find a user whos id matches the user_id column.
You need to call edit_wishlist_path(wishlist), not #wishlist.