Empty vars on rails application - ruby-on-rails

So i have a rails var. It grabs informations from the table and puts it into the page (table)
Heres the controller:
giga = #event.gigaurl
#gigaresults = Gigatable.find_by_eventurl(gigantic)
Now if the #gigaresults is empty. The whole page crashes.
The table it appends to is this.
<tr>
<td>
<%= #gigaresults.title%>
</td>
</tr>
If any of these are empty it crashes the page (on heroku) Any ways to avoid this?
Sam

Use this:
<%= #gigaresults.try(:title) %>
will not crash even if #gigaresults is nil.

Related

Check if ruby on rails model column is required

How can I check if a column for a Ruby on Rails column is required? I am building a process where people can import a CSV and match the headers in the file to relevant columns in the model. I can't guarantee that they will match so would rather the user could match them manually.
I'm ideally looking for something like Model.column_names[0].required?
If the column is required I want the user to be able to specify a generic value for all imports so that they don't fail unexpectedly (hence the unused input fields in the demo below).
I can upload the file contents and parse them, and have built a table where the user can select which CSV column matches which model column but would like to warn the use which are required. I am yet to create the actual import element but there are various other questions on SO with that well documented.
I've had a google and cant see anything, maybe I'm using the wrong search terms but any help would be appreciated.
Thanks in advance!
Controller:
require 'csv'
myfile = params[:file]
csv_text = File.read(myfile.path)
#csv = CSV.parse(csv_text, :headers => true)
#table_headers = Patient.column_names
View:
<% #table_headers.each_with_index do |header, table_i| %>
<tr>
<td>
<%= header %>
</td>
<td>
<select id="table_column[<%= table_i %>]"=>
<option value="skip">Skip</option>
<option value="custom">Custom</option>
<% #csv.headers.each_with_index do |column, file_i| %>
<option value="<%= file_i %>"><%= column %></option>
<% end %>
</select>
</td>
<td>
<input>
</td>
<tr>
<% end %>
If I get you right, you can use custom validation that will return specific error (that you want to share for user).
https://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations
Basically you need to override validate! method.
https://apidock.com/rails/ActiveModel/Validations/validate%21
For identification of such record (that is not valid) you can user valid? method.
https://apidock.com/rails/v6.0.0/ActiveModel/Validations/valid%3F

Show the last object with some condition Rails

In Rails I want to put a link in the view to the last object with some condition, I know how to found the object, like this code
Equipment.where(name: "Bulldozer").last
But I donĀ“t know the right syntax to put in the href to go to the show of the last Equipment with name Bulldozer
<td> text </td>
Thanks in advance!!!!
you're almost there, you need to assign the object to an instance variable, it'll be available within the view. So, in the controller action:
#equipment = Equipment.where(name: "Bulldozer").last
then, in your view:
<td>
<%= link_to "text", equipment_path(#equipment) %>
</td>
link_to helper will generate the <a> tag for you

having problems with ng-repeat

I am working on a Rails 3.2.11 app using angular 1.0.5.
Currently, a user will select a Cycle from a dropdown, and that will return a bunch of JSON from my controller using ng-resource.
Here is the method
$scope.update = function(cycleId) {
Cycle.get({action: cycleId}, function(resource) {
$scope.selectedCycle = resource;
$scope.tasks = resource.tasks;
$scope.newTask = {cycle_id: resource.cycle.id};
});
};
Here is an example of what json my controller is returning, which is 'resource' in above function: https://gist.github.com/anonymous/01ffe5a37e370661f6fb
Basically I am needing to use ng-repeat twice (one of them nested) using angulars ng-repeat, so that I can get the task_type_name in there as a header. I'm getting some weird interesting results. See the shorted code in my view below and the full thing here
<section ng-repeat="(task_type_name,task_type) in tasks ">
<h2>{{task_type_name}}</h2>
<table>
<tr>
<th>Task Name</th>
</tr>
<section id="task-edit">
<tr ng-repeat="task in task_type">
<td>
<%= link_to "{{task.name}}", '', "ng-click"=>"toggleShowHistory(task.id)" %>
</td>
</tr>
</section
</table>
So my is occuring at this part here
<section id="task-edit">
<tr ng-repeat="task in task_type">
If I try to combine that section and tr, OR change tr to ANYTHING but a tr, {{task}} no longer becomes available.
<section class="task-edit" ng-repeat="task in task_type">
{{task}} is available right here
<tr>
{{task}} is not available right here
<td>
{{task}} is not avaiable right here
</td>
</tr>
</section>
I tested the same concept on the first loop, and it seems to be fine on that loop just not the second, nested loop.
I'm assuming it has something to do with the scope. But i'm just not getting it.
Also, if you have any tips, i'm very new to angular and would love them.
I created a demo, and I am not seeing any issue. Please check your data source and make sure you plug in the tasks value of the json.
You need to change the nested section to tbody.
Demo on jsfiddle

JQuery not detecting partials views in Rails 3?

I`m trying to add autocomplete to a field that is included in a partial view in a Rails 3 app.
I can`t tell JQuery to look for it via form name, since a random attribute is generated every time the field is rendered.
Here's rundown of the code:
cardio_exercises.js.coffee
jQuery ->
$('input.Cardio').autocomplete
source: ['Elliptical trainer','Stair stepper (Stairmaster)','Stationary or recumbent bike','Jacob`s ladder','Arm ergometer (arm cycle)','Rowing machine','Treadmill walking','Treadmill running','Kettlebells','Interval training','Box jumps','Step ups','Plyometrics','Cycling','Jogging','Running','Power walking','Swimming','Water jogging/running','Bleacher running','Spinning','Step aerobics','Walking the dog','Playing with your dog','Taking the stairs','Jumping jacks']
In the partial I added a class of Cardio to the input field:
_cardio_exercise_fields.html.erb
<table>
<tr>
<td>
<b>Exercise Name</b><br>
<%= f.text_field :exercise_name, :class => 'Cardio' %></td>
<td>
<b>Duration</b><br>
<%= f.text_field :duration %> minutes
</td>
</table>
I have no clue about JS/jQuery so I would appriaciate any advice on the matter.
I know that it works (autocomplete) in normal views, just not in the partial in trying to implement it in.
jQuery works normally in Rails3, I mean, once the page is rendered,
$('input.Cardio') // => will get a result: [input.Cardio]
I suggest you install the firebug and watch the error message from its console.
( or developer tool in chrome & safari)

take the table element while clicking on the same table element

I'm developing a project and I have a problem about this subject.
I'm constructing ruby on rails project which will provide listing students with name.
First of all , I will list my students information in a table and one part is missing.
When the user clicks on Student Name , another table will appear and show the other information of students(like course or GPA).
The table shows the students name ( this part is succeed.)
But I don't know how to post my variable when user clicks on the students name.
Moreover , I know fundementals of AJAX.But I don't know how to pass my variable while user clicks on students name in the table.
I don't use form , and I try to fix this problem with link_to_remote method but I can't post my variable.Because I can construct my table in for-loop according to my listing variable which contains whole database.I filter for getting only names and surnames in controller part of ruby on rails and i have listing variable for this.when in for-loop i use <%=student.name%> tag for displaying the part of my variable.and I must post this variable to page for filtering GPA or Course name according to names.I need something like link_to_remote method for posting my variable student.name
I'm waiting your help.
Code is below :
<table border="1">
<tr>
<th>
Name
</th>
</tr>
<% #listing.each do |student| %>
<tr>
<td>
<%=student.name%> <%= student.surname %>
</td>
</tr>
<% end %>
problem fixed ,:with => "'id=blabla'" in link_to_remote method

Resources