Why is Rails displaying memory addresses on my page? - ruby-on-rails-3

My view:
<h1><%= #territory.name %></h1>
<%= link_to 'List of Territories', territories_path %>
<%= render 'shared/address_form' %>
<table>
<tr>
<td><strong>Name</strong></td>
<td><strong>Street</strong></td>
<td><strong>District</strong></td>
<td><strong>Note</strong></td>
<tr>
<%= #addresses.each do |address| %>
<tr>
<td><%= address.name %></td>
<td><%= address.street %></td>
<td><%= address.district %></td>
<td><%= address.note %></td>
</tr>
<% end %>
</table>
The form I render here is:
<%= form_for [#territory, #new_address] do |f| %>
<div>
<p>
<%= f.label :address %><br />
<%= f.text_area :address %>
</p>
</div>
<div class='file-wrapper'>
<%= f.submit "Submit" %>
</div>
<% end %>
Here is the territories controller, where the instance variable addresses is defined:
class TerritoriesController < ApplicationController
def index
#territories = Territory.all
end
def show
#territory = Territory.find(params[:id])
#new_address = #territory.addresses.build
#addresses = #territory.addresses
end
.
.
.
Why is Rails displaying
#<Address:0x7e088224>#<Address:0x7e0881d4>#<Address:0x7e088134>#<Address:0x7e088094># <Address:0x7e087ff4>#<Address:0x7e087f54>#<Address:0x7e087eb4>#<Address:0x7e087e14>#<Address:0x7e087d74>#<Address:0x7e0bce48>
after the form and before the table?
Thanks
Thomas

Check your layouts (app/views/layouts/*). Most likely you have included some ERB code in the one that is being rendered with this page that displays these addresses. Is that the full code of your view?
Edit: I found your solution. Right now, you have <%= #addresses.each ... %>. The each method runs the block on all elements, and then returns the list of elements. You do not want this code to be displayed. Remove the = so that <%= is just <%

You have some view code somewhere (in a layout or a view helper) that is implicitly calling the to_s method of your Address model instances. Look for something like <%= #address %>.
As you have seen, the non-overridden behaviour of the to_s method is to output the memory address of the object instance.

Those are not memory addresses. Those are instances of your Address class. If you'd override the to_s method in that class, you'd see that output there instead. And the reason you see those object printed out is your use of <%=. Changing this line
<%= #addresses.each do |address| %>
to this
<% #addresses.each do |address| %>
should fix it.

First: I can see no form in your view.
Second: Your view looks ok.
Have a look at your layout files.

Related

undefined method `model_name' for NilClass:Class in edit form

I have a rails app where I can create and edit records. I've created a form to enter data which works fine when I use the new/create actions. It will create a record no problem. But when I hit the edit action it gives me an undefined method 'model_name' for NilClass:Class.
I'm not sure what this means. Can someone give me a hand?
Form:
<%= form_for(#patient) do |f| %>
<%= f.label :Patient_Last_Name %>
<%= f.text_field :patient_last %>
<%= f.label :Patient_First_Name %>
<%= f.text_field :patient_first %>
<%= f.label :Patient_DOB %>
<%= f.date_select :patient_dob %>
<%= f.label :Primary_Diagnosis %>
<%= f.collection_select(:diagnosis_id, Diagnosis.all, :id, :diagnosis_name)%></br>
<%= f.label :Primary_Physician %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name)%></br>
<%= f.button :submit %>
<% end %>
View Code:
<td><%= link_to 'Edit', edit_patient_path(patient), :class => 'btn btn-close btn-mini'%></td>
Controller Code:
def edit
#patient = Patient.find(params[:id])
end
Edit view:
<%= render 'form' %>
When I remove the partial render from the form, the URL will go to the correct route/url. But I keep getting that error when the form partial is rendered.
There was an extra end statement in my controller which was cutting off half of the class which included the edit action. This was not allowing me to use the edit action. Once this typo was fixed things started working normally.
Sorry for the confusion.

Rails 3 export csv from partial?

I followed RBate's Railscast on the subject, but I have a caviat.
The view starts with a form to define the table's variables. On clicking submit, the form posts back to the same view and re-renders the partial (which is just the HTML table). It's not AJAX, it's all form params.
RBates uses a single variable and calls .to_csv on it. I have a complex table with many variables defined by the params and so don't know how to call .to_csv on the entire HTML table.
I need to be able to export just the results in the partial.
Thanks in advance for the ideas.
&&&&&&&& AS REQUESTED, SAMPLE CODE: &&&&&&&&
So, the full code is probably more confusing than helpful, but here's a slimmed down version:
stats/reporter.html.erb: (stat_reporter_path matches this URI)
<%= form_tag( :stat_reporter, method: :post ) do %>
form is in here
<%= submit_tag "Get my report" %>
<% end %>
<div>
<%= render 'stat_select' %>
</div>
stats/_stat_select:
<table>
<thead>
<tr>...column heads...</tr>
</thead>
<tbody>
<% #counties.each do |c| %>
<td><%= c.name %></td>
<% c.programs.each do |p| %>
<td><%= p.name %></td>
<% p.events.each do |e| %>
<td><%= e.name %></td>
...and so on...
</table>
Here's a screenshot
Looking back on this post, I would:
Create a link to a new view and pas all the variables to in the URL.
That view would use all the variables to create an HTML output that could be used to create the to_csv method
Once the csv file was created, the controller would render the original view.

Better way to check to see if an image name exists outside of a view

Basically I have a table in the index view where if a pictures name = army.unit_name, then it renders that picture. If not, it displays in text that the image is missing. I'm still new at Rails, but there must be a way to put this conditional check somewhere else other than the view. Could someone please enlighten me? Do I do it in the model?
<% #armies.each do |army| %>
<tr>
<% if File.exists?("/users/<name>/Ruby Projects/thrones/app/assets/images/#{army.unit_name}.png") %>
<td><span class="picture-hover"><%= image_tag("#{army.unit_name}.png" %></span></td>
<% else %>
<td>Unit Card art missing</td>
<% end %>
<td><%= link_to army.unit_name, army_path(army) %></td>
</tr>
<% end %>
You can try this
...
<% if Rails.application.assets.find_asset "#{army.unit_name}.png" %>
...

How to get correct children ids using fields_for "parents[]", parent do |f| using f.fields_for :children, child?

I'm editing multiple instances of a parent model in an index view in one form, as in Railscasts #198.
Each parent has_many :children and accepts_nested_attributes_for :children, as in Railscasts #196 and #197
<%= form_tag %>
<% for parent in #parents %>
<%= fields_for "parents[]", parent do |f|
<%= f.text_field :job %>
<%= f.fields_for :children do |cf| %>
<% cf.text_field :chore %>
<% end %>
<% end %>
<% end %>
<% end %>
Given parent.id==1
f.text_field :job correctly generates
<input id="parents_1_job" type="text" value="coding" size="30" name="parents[1][job]">
But cf.text_field :chore generates ids and names that don't have the parent index.
id="parents_children_attributes_0_chore"
name="parents[children_attributes][0][chore]"
If I try passing the specific child object to f.fields_for like this:
<% for child in parent.children %>
<%= f.fields_for :children, child do |cf| %>
<%= cf.text_field :chore %>
<% end %>
<% end %>
I get the same. If I change the method from :children to "[]children" I get
id="parents_1___children_chore"
which gets the right parent_index but doesn't provide an array slot for the child index.
"[]children[]" isn't right either:
id="parents_1__children_3_chore"
as I was expecting attributes_0_chore instead of 3_chore.
Do I need to directly modify an attribute of the FormBuilder object, or subclass FormBuilder to make this work, or is there a syntax that fits this situation?
Thanks for any thoughts.
I did solve this problem by reading the source code for FormBuilder.fields_for
One possible answer: Yes, modify the f.object_name attribute of the FormBuilder object.
Specifically in this situation
f.fields_for :children
is going to call
f.fields_for_with_nested_attributes
which sets the name variable based on f.object_name. The id for the generated element looks like it is based on the name,so both match in the resulting html.
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
.....
So one way to tell f.fields_for to do what I wanted is to set f.object_name to include the parent id for the duration of the f.fields_for block
<% old_object_name = f.object_name %>
<% f.object_name="parents[#{f.object.id}]" %>
<% =f.fields_for :children do |cf| %>
<%= cf.text_field :chore %>
<% end %>
<% f.object_name=old_object_name #should be "parents[]" %>
Then everything within the f.fields_for block can use the standard rails helpers unmodified.

how to query in view ruby on rails

i have this in the controller
#ads = Ad.all(:joins => 'LEFT JOIN states ON ads.state_id = states.id')
but i have problem to query field of states table.
any idea?
<% #ads.each do |ad| %>
<tr>
<td><%= ad.title %></td> <- title is ad field.no problem
<td><%= ad.name %></td> <- name is states field.problem at here
</tr>
<% end %>
I don't think this will work unless you have associations set up. Unless performance is a concern, you may just want to use the association without joins
ad.rb
class Ad < ActiveRecord::Base
belongs_to :state
end
state.rb
class State < ActiveRecord::Base
has_many :ads
end
controller
#ads = Ad.all
view
<% #ads.each do |ad| %>
<tr>
<td><%= ad.title %></td>
<td>
<%= ad.state.name %>
</td>
</tr>
<% end %>
I think you need to put this: ads.state_id = states.id like this: #{ads.state_id = states.id}
The #{ } will evaluate the ruby code inside. Otherwise what you have is just text inside a string.
I'm not quite sure what your issue is, though, so I'm not totally sure that will fix it.