Respected ppl ...
In my application i want to display the last hospital name for a given employee ...
for which i tried this :
<%= #employee.postings.last.hospital.hospital_name %>
All the required associations are correct ...as this works perfectly for all the employees who have a posting ... but i get the error for the employees who dont have even a single posting ...
I tried doing
<%= #employee.postings.last.hospital.hospital_name.to_s %>
and even
<% if !#employee.postings.last.hospital.nil? %>
and even a "try" function ....
I just want it to not display any data when there dosent exist one ... instead of the intimidating error ...
if i could just learn how to skip over printing nil values then it would be awesome ..as i am facing similar issues elsewhere too ...
For ex :
in my employees main page i want to display all the qualifications for each employee for which im doing :
<tbody>
<% #employees.each do |employee| %>
<tr>
<td><%= employee.emp_id %></td>
<td><%= employee.emp_treasury_id %></td>
<td><%= link_to employee.emp_full_name,employee_path(employee) %></td>
<% #employee.qualifications.each do |qualification| %>
<td><%= qualification.qualification_name.Qualification_name %></td>
<% end %>
</tr>
<% end %>
but i end up getting "undefined method `qualifications' for nil:NilClass" error once again ...
Im trying a lot ... but still ...
Thanx and Sincere Regards
-Sky
Try this
<%= #employee.postings.last.try(:hospital).try(:hospital_name) || "N/A" %>
using try
<%= #employee.postings.last.try(:hospital).try(:hospital_name) %>
using if
<%= #employee.postings.last.hospital.hospital_name if #employee.postings.exists? && #employee.positings.last.hospital %>
It should be
employee not #employee
Rest you use try or respond_to for being more safe. As you might not have run the migrations.
.respond_to?(:field) && model.try(:field)
Thanks
<% #employees.each do |employee| %>
<tr>
<td><%= employee.emp_id %></td>
<td><%= employee.emp_treasury_id %></td>
<td><%= link_to employee.emp_full_name,employee_path(employee) %></td>
<% employee.qualifications.each do |qualification| %>
<td><%= qualification.try(:qualification_name).try(:Qualification_name) %></td>
<% end %>
</tr>
<% end %>
Related
I have got a problem with saving multiple record.
This script wil load a list of instruments that belong to a department through a join table.
This form will make a new record for another join table, the problem is when I'f got 4 instruments it only will save the last instrument.
Image generated list
Can anybody help me out to solve this problem or point me into the right direction ??
<%= form_for(:joindaylisting) do |j| %>
<% #instrumentslist.each do |instrument| %>
<tr class="<%= cycle('odd', 'even') %>">
<td>
<% j.label(:instrument_id, "#{instrument.name}") %>
<%= link_to("#{instrument.name}", {:controller => 'instruments', :action => 'show_instruction', :instrument_id => instrument.id}, :onclick=>"window.open('height=670, width=675');return false;") %>
</td>
<%= j.hidden_field(:instrument_id, :value => instrument.id) %>
<td></td>
<% j.label(:ammountdesinfection, "") %>
<td><%= j.text_field(:ammountdesinfection) %></td>
<% j.label(:ammountinstruments, "") %>
<td><%= j.text_field(:ammountinstruments) %></td>
<% j.label(:ammountrelease, "") %>
<td><%= j.text_field(:ammountrelease) %></td>
<% j.label(:notes, "") %>
<td><%= j.text_area(:notes) %></td>
</tr>
<% j.label(:department_id) %>
<%= j.hidden_field(:department_id, :value => #department.id) %>
<% end %>
<% end %>
Only the last one is saved because the fields have the same name and the last one overrides all other values. See the params you are getting on the receiving controller.
You probably want/need to configure departments to accepts_nested_attributes_for :instruments and a form for the department object with fields_for: instruments. I've highlighted keywords that my help you get the information you want, the Rails Guides is a good place to start.
Rails 3.2
I have the following in my view:
<% #organizations.each do |o| %>
<% organization = Organization.find(o.organization_id) %>
<% if organization == nil %>
<% next %>
<% end %>
<tr>
<td><%= o.org_name %></td>
<td><%= number_to_currency(o.revenue, :precision => 0) %></td>
<td><%= o.rank %></td>
</tr>
<% end %>
I was under the impression that if organization is not found, execution would jump to the next record. Instead, I am getting the "something went wrong".
Looking through the log file, one of the organization_id is not found in the organizations table, and that's what's triggering the error.
Why isn't execution jumping to the next record?
Temporary Solution: I changed
<% organization = Organization.find(o.organization_id) %>
To:
<% organization = Organization.find_by_id(o.organization_id) %>
and that gives me a nil when it does not find anything. If it's nil, the execution skips to the next record
According to http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find, Organization.find(o.organization_id) will raise an ActiveRecord::RecordNotFound for the id that does not exist in the table.
Addendum:
Try something like the following:
<% ids = #organizations.map(&:organization_id) %>
<% organizations = Organization.where(id: ids) %>
<% organizations.each do |o| %>
<tr>
<td><%= o.org_name %></td>
<td><%= number_to_currency(o.revenue, :precision => 0) %></td>
<td><%= o.rank %></td>
</tr>
<% end %>
With this method, you'll fetch all the organizations with 1 query from the DB as opposed to 1 for every organization.
In my class AleShot I have some dynamic mongoid-attributes. In order to index them, I collect all attributes in an array called "dynamos". Now when I want to list these (see code below) I get: undefined method 'dyn_f' for #<AleShot:0x007f8f7ab18328>
Any Ideas why the dyn_f-variable isn't translated correctly?
<% #ale_shots.each do |ale_shot| %>
<tr>
<td><%= ale_shot.name %></td>
<% dynamos.each do |dyn_f| %>
<td><%= ale_shot.dyn_f %></td>
<% end %>
</tr>
<% end %>
That could be because dyn_f is not defined as a field in the model.
Access it like this
ale_shot['dyn_f']
I am learning rails by trying to model a collectible card game.
I have a champion model and a rarity model. I have the has_many/belongs_to in the model definition and this works in the console:
c = Champion.find(1)
c.rarity.name
=> "Uncommon"
When I do the same thing in a template, I get
<%= champion.rarity.name %>
undefined method `name' for nil:NilClass
Any ideas on how to get this to work?
This is on Rails 3.2.2.
Update: Full .erb code
<% #champions.each do |champion| %>
<tr>
<td><%= champion.name %></td>
<td><%= champion.rarity.name %></td>
</tr>
<% end %>
If every Champion does not have a Rarity association (some are nil), you can use a .try() to print the name. Otherwise the .each will fail with a NoMethod when one without a Rarity is encountered.
<% #champions.each do |champion| %>
<tr>
<td><%= h champion.name %></td>
<td><%= h champion.rarity.try(:name) %></td>
</tr>
<% end %>
Or the less clever unless nil method:
<% #champions.each do |champion| %>
<tr>
<td><%= h champion.name %></td>
<td><%= h champion.rarity.name unless champion.rarity.nil? %></td>
</tr>
<% end %>
Note: I have also added the h() helper method to encode these for HTML output, though this is done automatically in Rails 3.
Code in my product model (product.rb):
def self.search(search)
if search
find(:all)
else
find(:all)
end
end
Code in my search controller (search_controller.rb):
def index
#products = Product.search("Apple")
end
Code in my view (index.html.erb):
<h1>Products</h1>
<% form_tag client_search_path , :method => :get do %>
<p>
<%= search_field_tag :term, params[:term], :class=> "auto_search_complete"%>
<%= submit_tag "Search", :name => nil, :class => 'button', :id => "search_bn" %>
</p>
<% end %>
<table border="1px">
<tr>
<th>Name</th>
<th>Brand</th>
<th>Quantity available</th>
<th>Category</th>
<th>Shopcenter name</th>
<th>Shopcenter streetnumb</th>
<th>Shopcenter streetname</th>
<th>Shopcenter postal</th>
<th>Shopcenter province</th>
</tr>
<% for product in #products%>
<tr>
<td><%= product.name %></td>
<td><%= product.brand %></td>
<td><%= product.quantity_available %></td>
<td><%= product.category %></td>
<td><%= product.shopCenter_name %></td>
<td><%= product.shopCenter_streetNumb %></td>
<td><%= product.shopCenter_streetName %></td>
<td><%= product.shopCenter_postal %></td>
<td><%= product.shopCenter_province %></td>
</tr>
<% end %>
</table>
I load this all is good, but if I comment one of the line of codes in my model:
def self.search(search)
if search
#find(:all)
else
find(:all)
end
end
I expect this to work also at least for the initial render, or when I submit an empty search term, but it's not. And changing the code to of the model to:
def self.search(search)
if search
find_all_by_name(search)
else
find(:all)
end
end
Doesn't work it gives me an error that the view is working with a nil object, which is impossible because my database has entries.
Can someone explain what is going on? I have the impression that both the conditions in my model are being executed. At least that's what 2 puts statement in each case showed me.
Please advice.
I think you should set search = nil if search == "" in your controller otherwise it will always go to the first condition.
It had some compatibility issues with Rails 3.
I updated rails and ruby and it works fine now