User Model:

has_many :shipments, dependent: :destroy has_many :friendships has_many :friends, through: :friendships has_many :inverse_friendships, :class_name => 'Friendship', :foreign_key => 'friend_id' has_many :inverse_friends, :through => :inverse_friendships, :source => :user 

Friendship model:

 belongs_to :user belongs_to :friend, class_name: 'User' has_many :shipments 

Shipment model:

 belongs_to :user belongs_to :friendship 

FriendshipsController:

 def index @friendships = Friendship.all end 

Shipment object form:

 <%= form_for(@shipment, html: { multipart: true }) do |f| %> <%= f.text_field :name, placeholder: "Ф.И.О." %> <%= f.user.friendships.each do |friend| %> <%= friend.name %> <% end %> <% end %> 

Nevertheless, it gives:

 NoMethodError in ShipmentsController#new undefined method `user' for #<ActionView::Helpers::FormBuilder:0x007f1e4ee2eac8> 

Full trace

 app/controllers/shipments_controller.rb:35:in `user' app/views/shipments/new.html.erb:50:in `block in _app_views_shipments_new_html_erb__629865209450934427_69883691027280' app/views/shipments/new.html.erb:3:in `_app_views_shipments_new_html_erb__629865209450934427_69883691027280' activerecord (4.2.6) lib/active_record/core.rb:155:in `find' actionpack (4.2.6) lib/abstract_controller/helpers.rb:67:in `user' actionview (4.2.6) lib/action_view/helpers/capture_helper.rb:38:in `block in capture' actionview (4.2.6) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer' actionview (4.2.6) lib/action_view/helpers/capture_helper.rb:38:in `capture' actionview (4.2.6) lib/action_view/helpers/form_helper.rb:444:in `form_for' actionview (4.2.6) lib/action_view/template.rb:145:in `block in render' activesupport (4.2.6) lib/active_support/notifications.rb:166:in `instrument' actionview (4.2.6) lib/action_view/template.rb:333:in `instrument' actionview (4.2.6) lib/action_view/template.rb:143:in `render' actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template' actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument' activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument' activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument' actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument' actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template' actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout' actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:52:in `render_template' actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:14:in `render' actionview (4.2.6) lib/action_view/renderer/renderer.rb:46:in `render_template' actionview (4.2.6) lib/action_view/renderer/renderer.rb:27:in `render' actionview (4.2.6) lib/action_view/rendering.rb:100:in `_render_template' actionpack (4.2.6) lib/action_controller/metal/streaming.rb:217:in `_render_template' actionview (4.2.6) lib/action_view/rendering.rb:83:in `render_to_body' actionpack (4.2.6) lib/action_controller/metal/rendering.rb:32:in `render_to_body' actionpack (4.2.6) lib/action_controller/metal/renderers.rb:37:in `render_to_body' actionpack (4.2.6) lib/abstract_controller/rendering.rb:25:in `render' actionpack (4.2.6) lib/action_controller/metal/rendering.rb:16:in `render' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render' activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms' /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime' activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `ms' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime' activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:43:in `render' actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:10:in `default_render' actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:5:in `send_action' actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action' actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action' activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call' activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile' activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call' activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__' activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks' activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action' actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action' activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument' activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument' actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action' actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action' activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process' actionview (4.2.6) lib/action_view/rendering.rb:30:in `process' actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch' actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action' actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch' actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve' actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve' actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each' actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve' actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call' warden (1.2.6) lib/warden/manager.rb:35:in `block in call' warden (1.2.6) lib/warden/manager.rb:34:in `catch' warden (1.2.6) lib/warden/manager.rb:34:in `call' rack (1.6.4) lib/rack/etag.rb:24:in `call' rack (1.6.4) lib/rack/conditionalget.rb:25:in `call' rack (1.6.4) lib/rack/head.rb:13:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call' rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context' rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call' activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call' activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call' activerecord (4.2.6) lib/active_record/migration.rb:377:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__' activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks' activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call' web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call' web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app' railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call' activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged' activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged' railties (4.2.6) lib/rails/rack/logger.rb:20:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' rack (1.6.4) lib/rack/runtime.rb:18:in `call' activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' rack (1.6.4) lib/rack/lock.rb:17:in `call' actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call' rack (1.6.4) lib/rack/sendfile.rb:113:in `call' railties (4.2.6) lib/rails/engine.rb:518:in `call' railties (4.2.6) lib/rails/application.rb:165:in `call' rack (1.6.4) lib/rack/content_length.rb:15:in `call' puma (2.11.1) lib/puma/server.rb:507:in `handle_request' puma (2.11.1) lib/puma/server.rb:375:in `process_client' puma (2.11.1) lib/puma/server.rb:262:in `block in run' puma (2.11.1) lib/puma/thread_pool.rb:104:in `block in spawn_thread' 

I need that when creating the Shipment model - filling out the form - the User could mark another User: from the list of his friends in the Friendship model by (friend_id). I understand that Rails does not see the name method, but I do not understand why?

    1 answer 1

    I understand that Rails does not see the name method, but I do not understand why?

    Actually, the problem is with the user method and not the name . Everything is simple:

     <%= form_for(@shipment, html: { multipart: true }) do |f| %> 

    f , with which further work is going on, is an object of the rail FormBuilder class, but not in any way of user Shipment . Accordingly, the user has no method (relation).

    Solution options:

    The first

    Use an object bound to the builder (this will be the @shipment that was passed to form_for )

     f.object.user.friendships.each do |friend| # ... end 

    Second

    Use instance variable directly

     @shipment.user.friendship #... 

    Third

    IMHO, the most correct in terms of further work with the code. Use helper_methods :

     class ShipmentsController < ApplicationController helper_method :shipment, :user private def user @user ||= User.find(params[:user_id]) #это для примера end def shipment @shipment ||= user.shipments.new # тоже для примера end end # Вьюха <%= form_for(shipment, html: { multipart: true }) do |f| %> <% user.friendships.each do |friend| %> <%= friend.name %> <% end %> <% end %> 

    UPD: The example above is based on the code that was in question. If you need a choice, it will look something like this:

     <%= form_for(shipment, html: { multipart: true }) do |f| %> <%= f.select :friendship_id, user.friendships.map{ |friendship| [friendship.friend.name, friendship.id] } %> <% end %> 
    • As far as I understand, TSa is interested in <select> or another choice with a list of friends, but the explanation is generally good. - D-side
    • @ D-side, from the text of the question it seemed to me that the problem was different. But just in case he added about it :) - anoam
    • Absolutely true - the method will be select - but this is a formality. The third option looks great, but nevertheless now it gives out: "ActiveRecord :: RecordNotFound in ShipmentsController # new Couldn't find User with 'id' =" - apparently I’ve got something wrong again. I tried and: id and: friend_id and: user_id - the result is the same. - lif3ar
    • @ lif3ar, and you can see the code in which falls? I so suspect that just again there are misunderstandings with entities for which id is passed. - anoam
    • one
      @ lif3ar, eh! Swears on the user method. Apparently you copied the code that I gave for an example? In general, you most likely need a current user, on behalf of which everything happens (and not to search from the database). This is the same user that you previously put in @user . - anoam