How to use association to display related data?

111 views
Skip to first unread message

rickcasey

unread,
Jun 26, 2012, 7:15:40 PM6/26/12
to plataformate...@googlegroups.com
I'm having trouble using SimpleForm to do a seemingly simple thing: display data from two tables that are related to a third table. In effect, there are to 1-M relations on either side of the third table. I need to edit data from this third table, along with two select lists from the 1-M related tables.

The central table is called genotypes, and the two related tables are called samples and markers.

I got my code to not fail (after reading a lot about SimpleForm here and on the documentation page), but the view is showing up weird, with hex code in the select lists. I'm sure I've just got some syntax wrong, but it's hard to figure out what's wrong just from the docs, and I'm not that experiences a Ruby programmer yet.

Anyway, here's the essence of my code:

The models are based on 3 entities: samples, genotypes and makers.

marker has_many :genotypes
sample has_many :genotypes
genotypes belongs_to markers
genotypes belongs_to samples

Here's my view for editing of a genotype:
<%= simple_form_for @genotype do |f| %>
  <%= f.input :allele1 %>
  <%= f.input :allele1 %>
  <%= f.association :markers, :collection => Marker.all(:order => 'marker'), :prompt => "Choose a Marker"%>
  <%= f.association :samples, :collection => Sample.all(:order => 'box'), :prompt => "Choose a Sample"%>
  <%= f.button :submit %>
<% end %>
 
Also, this is not exactly what I want to do, because Samples actually has a composite key based on two attributes, box and well.
I need to display the box+well as the select list, not just box...but I haven't been able to figure that out yet.

I learned about SimpleForm from RailsCast and really hope to use it in my project; it looks very helpful -- if I just figure out this syntax.

Thanks,
Rick





Carlos Antonio da Silva

unread,
Jun 26, 2012, 7:41:22 PM6/26/12
to plataformate...@googlegroups.com
Hello Rick,

lets see, the problem you're having with the select boxes showing the # objects is because they do not have a known "name/label" column that SimpleForm can use. In such cases, you have to give that on your own, by adding the :label_method option.

By default, SimpleForm will look for the following methods in the given object (Marker or Sample in your case): :to_label, :name, :title, :to_s [1]. If you don't have any of the first 3, it'll default to :to_s, giving you the #Class string that look weird. So you have to either implement one of these methods on your model, or give the :label_method option when calling f.association, pointing to a different method/attribute that should be used as label instead of the default attempts. (you can also change your initializer and add any other methods to that default list if you want)


Hope that helps, and welcome to SimpleForm :)

-- 
At.
Carlos Antonio

On Tuesday, June 26, 2012 at 8:15 PM, rickcasey wrote:

 

rickcasey

unread,
Jun 28, 2012, 4:58:37 PM6/28/12
to plataformate...@googlegroups.com
Carlos

Thanks much for this answer; it helps. So, I put this method in my model for markers (actually, gmarkers):

def name
  return :marker
end

Is this what is needed?


On another note, I am still getting the error:

syntax error, unexpected $end, expecting keyword_end


because of this line of code:

<%=f.association :gmarkers, :collection => Gmarker.all(:order => 'marker'), :label => "Marker" %>


I do not understand why this code causes such an error? Any suggestions much appreciated....

Thanks,
Rick

Carlos Antonio da Silva

unread,
Jun 28, 2012, 5:12:40 PM6/28/12
to plataformate...@googlegroups.com
If you want to list the "marker" attribute in the select box, you can do it as follows:

    f.association :gmarkers, :collection => Gmarker.order('marker').all, :label => 'Marker', :label_method => :marker

And you wouldn't need the model method. In case you want to go using it though, it'd look like this:

  def name
    marker
  end

Which is basically an "alias" for your "marker" attribute, an alias that SimpleForm understands out of the box.

Now about the error you're getting, I'm quite sure it's not in that particular line, since it looks ok to me. It seems your template has some sort of error, some missing <% end %> tag or something like that. It'd be easier if you can share the entire form so we can look.

Regars.

-- 
At.
Carlos Antonio

Rick Casey

unread,
Jun 28, 2012, 5:51:43 PM6/28/12
to plataformate...@googlegroups.com
Ok; thanks for the offer of help. Here is the entire view that has the error:


<h1>Editing genotype</h1>

<%= simple_form_for @genotype do |f| %>
  <%= f.input :allele1 %>
  <%= f.input :allele2 %>
  <%= f.input :run_date %>


  <%= f.association :gmarkers, :collection => Gmarker.order('marker').all, :label => 'Marker', :label_method => :marker %>
  <%= f.association :gsamples, :collection => Gsample.order('box').all, :label => "Box", :label_method => :box %>

  <%= f.button :submit %>
<% end %>

<%= link_to 'Show', @genotype %>
<%= link_to 'Back', genotypes_path %>


I think the error is in those lines because when I comment them out, there error goes away.....

--rick
--
----------------------------------------------------------------------------
Rick Casey :: case...@gmail.com :: 303.345.8893

Carlos Antonio da Silva

unread,
Jun 28, 2012, 7:18:21 PM6/28/12
to plataformate...@googlegroups.com
Hm that's interesting, I can't see anything wrong on this form. The error might be something else around there, you'll have to take a look at the stack trace, or try posting it here, not sure I'll be able to help though.

-- 
At.
Carlos Antonio

rickcasey

unread,
Jul 10, 2012, 4:35:35 PM7/10/12
to plataformate...@googlegroups.com
Hi Carlos,

Finally getting back to this.  This is crazy, but I still don't see the syntax error, which seems to be in the simple_form_for do loop; but here's the entire stack trace. I'd sure like to get SimpleForm working for my project!  --Rick

SyntaxError in Genotypes#edit

Showing /Net/delta/home/caseyr/public_html/geno/app/views/genotypes/edit.html.erb where line #9 raised:

/Net/delta/home/caseyr/public_html/geno/app/models/gmarker.rb:5: syntax error, unexpected $end, expecting keyword_end

Extracted source (around line #9):

6:   <%= f.input :run_date %>
7: 
8: 
9:   <%= f.association :gmarkers, :collection => Gmarker.order('marker').all, :label => 'Marker', :label_method => :marker %>
10:   <%= f.association :gsamples, :collection => Gsample.order('box').all, :label => "Box", :label_method => :box %>
11: 
12:   <%= f.button :submit %>

Rails.root: /Net/delta/home/caseyr/public_html/geno

Application Trace | Framework Trace | Full Trace
activesupport (3.2.3) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.3) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.3) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.3) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.3) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.3) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.3) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.3) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.3) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.3) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.3) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.3) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.3) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.3) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.3) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.3) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.3) lib/active_support/dependencies.rb:190:in `const_missing'
app/views/genotypes/edit.html.erb:9:in `block in _app_views_genotypes_edit_html_erb___25286279_91078190'
actionpack (3.2.3) lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
actionpack (3.2.3) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
actionpack (3.2.3) lib/action_view/helpers/capture_helper.rb:40:in `capture'
actionpack (3.2.3) lib/action_view/helpers/form_helper.rb:606:in `fields_for'
actionpack (3.2.3) lib/action_view/helpers/form_helper.rb:377:in `form_for'
simple_form (2.0.2) lib/simple_form/action_view_extensions/form_helper.rb:33:in `block in simple_form_for'
simple_form (2.0.2) lib/simple_form/action_view_extensions/form_helper.rb:51:in `with_simple_form_field_error_proc'
simple_form (2.0.2) lib/simple_form/action_view_extensions/form_helper.rb:32:in `simple_form_for'
app/views/genotypes/edit.html.erb:3:in `_app_views_genotypes_edit_html_erb___25286279_91078190'
actionpack (3.2.3) lib/action_view/template.rb:143:in `block in render'
activesupport (3.2.3) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.3) lib/action_view/template.rb:141:in `render'
actionpack (3.2.3) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.3) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.3) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.3) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.3) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.3) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.3) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.3) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.3) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.3) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.3) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.3) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.3) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.3) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.3) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.3) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.3) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/caseyr/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.2.3) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.3) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.3) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.3) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.2.3) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.3) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.3) lib/active_support/callbacks.rb:414:in `_run__664257445__process_action__912317710__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.3) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.3) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.3) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.3) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.3) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.3) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.3) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__73493139__call__888738921__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/caseyr/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/caseyr/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/caseyr/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'


On Thursday, June 28, 2012 5:18:21 PM UTC-6, Carlos Antonio da Silva wrote:
Hm that's interesting, I can't see anything wrong on this form. The error might be something else around there, you'll have to take a look at the stack trace, or try posting it here, not sure I'll be able to help though.

-- 
At.
Carlos Antonio

On Thursday, June 28, 2012 at 6:51 PM, Rick Casey wrote:

Ok; thanks for the offer of help. Here is the entire view that has the error:


<h1>Editing genotype</h1>

<%= simple_form_for @genotype do |f| %>
  <%= f.input :allele1 %>
  <%= f.input :allele2 %>
  <%= f.input :run_date %>


  <%= f.association :gmarkers, :collection => Gmarker.order('marker').all, :label => 'Marker', :label_method => :marker %>
  <%= f.association :gsamples, :collection => Gsample.order('box').all, :label => "Box", :label_method => :box %>

  <%= f.button :submit %>
<% end %>

<%= link_to 'Show', @genotype %>
<%= link_to 'Back', genotypes_path %>


I think the error is in those lines because when I comment them out, there error goes away.....

--rick

Rafael Mendonça França

unread,
Jul 10, 2012, 4:37:24 PM7/10/12
to plataformate...@googlegroups.com
The syntax error is in the model as the stack trace said.

models/gmarker.rb:5


Rafael Mendonça França
http://twitter.com/rafaelfranca
https://github.com/rafaelfranca

rickcasey

unread,
Jul 11, 2012, 4:11:27 PM7/11/12
to plataformate...@googlegroups.com
Thank you Rafael; that was so obvious! 

I feel like a kid who's learning to read....

--Rick
Reply all
Reply to author
Forward
0 new messages