Understanding Rails controller - view connection via instance variable of associated model -


i newbie trying understand how use instance variables of associated models in controller make them available view. how work? there standard ? keep getting undefined method errors. what's proper usage of @example = example.find(params[:id]) or @example = example.find(example_params) ? associated models? each controller action have specific @instance variable formulas (i.e example.all index ) ? know of comprehensive guide on controller view usage of instance variables?

code

model

class presupuesto < activerecord::base       belongs_to :cliente       has_many :ordenes, dependent: :destroy       has_many :medios, :through => :ordenes       has_one :factura       validates :fecha, :titulo, :cliente_id, :producto, presence: true     end      class factura < activerecord::base        has_many :factura_items        belongs_to :cliente        belongs_to :presupuesto        validates :fecha_de_expedicion, presence: true     end 

view

   _form.html.erb <% content_for :title %>factura<% end %>     <h3>factura</h3>     <div class="form">       <%= simple_form_for @factura |form| %>         <%= form.error_notification %>         <%= form.input :fecha_de_expedicion %>         <%= form.association :cliente, :label_method => :nombre,  label: "cliente", :include_blank => false %>         <%= form.association :presupuesto, :label_method => :titulo,  label: "presupuesto", :include_blank => false %>         <%= form.button :submit, 'submit', class: 'submit' %>       <% end %>     </div>      new.html.erb     <h1>new factura</h1>      <%= render 'form' %>      <%= link_to 'back', facturas_path %> 

controller

class facturascontroller < applicationcontroller        # /facturas       # /facturas.json       def index         @factura = factura.all         @cliente = cliente.all       end       # /facturas/1       # /facturas/1.json       def show         @factura = factura.find(params[:id])         @cliente = @factura.cliente         @presupuesto = @factura.presupuesto       end       # /facturas/new       def new        @factura = factura.new        @presupuesto = presupuesto.all        #@ordenes = ordene.where(aprobado_por_cliente: 1)        end       # /facturas/1/edit       def edit       end       # post /facturas       # post /facturas.json       def create         @factura = factura.new(factura_params)         respond_to |format|           if @factura.save             format.html { redirect_to @factura, notice: 'factura created.' }             format.json { render :show, status: :created, location: @factura }           else             format.html { render :new }             format.json { render json: @factura.errors, status: :unprocessable_entity }           end         end`       end        # patch/put /facturas/1       # patch/put /facturas/1.json       def update         respond_to |format|           if @factura.update(factura_params)             format.html { redirect_to @factura, notice: 'factura updated.' }             format.json { render :show, status: :ok, location: @factura }           else             format.html { render :edit }             format.json { render json: @factura.errors, status: :unprocessable_entity }           end         end       end        # delete /facturas/1       # delete /facturas/1.json       def destroy         @factura.destroy         respond_to |format|           format.html { redirect_to facturas_url, notice: 'factura destroyed.' }           format.json { head :no_content }         end       end        private         # use callbacks share common setup or constraints between actions.         def set_factura           @factura = factura.find(params[:id])         end          # never trust parameters scary internet, allow white list through.         def factura_params           params.require(:factura).permit(:fecha_de_expedicion, :cliente_id, :presupuesto_id )         end     end 

error nomethoderror @ /facturas/new undefined method `presupuesto_id' #

started "/facturas/new" ::1 @ 2015-05-30 21:06:26 -0500 processing facturascontroller#new html   cliente load (0.2ms)  select "clientes".* "clientes"   presupuesto load (0.1ms)  select "presupuestos".* "presupuestos"   rendered facturas/_form.html.erb (21.1ms)   rendered facturas/new.html.erb within layouts/application (22.6ms) completed 500 internal server error in 29ms  nomethoderror - undefined method `presupuesto_id' #<factura:0x007feadf9dc930>:   activemodel (4.2.0) lib/active_model/attribute_methods.rb:433:in `method_missing'   actionview (4.2.0) lib/action_view/helpers/tags/base.rb:28:in `value'   actionview (4.2.0) lib/action_view/helpers/tags/collection_select.rb:16:in `block in render'   actionview (4.2.0) lib/action_view/helpers/tags/collection_select.rb:16:in `render'   actionview (4.2.0) lib/action_view/helpers/form_options_helper.rb:202:in `collection_select'   actionview (4.2.0) lib/action_view/helpers/form_options_helper.rb:789:in `collection_select'   simple_form (3.1.0) lib/simple_form/inputs/collection_select_input.rb:9:in `input'   simple_form (3.1.0) lib/simple_form/wrappers/leaf.rb:19:in `render'   simple_form (3.1.0) lib/simple_form/wrappers/many.rb:28:in `block in render'   simple_form (3.1.0) lib/simple_form/wrappers/many.rb:26:in `render'   simple_form (3.1.0) lib/simple_form/wrappers/root.rb:15:in `render'   simple_form (3.1.0) lib/simple_form/form_builder.rb:115:in `input'   simple_form (3.1.0) lib/simple_form/form_builder.rb:191:in `association'   app/views/facturas/_form.html.erb:8:in `block in _app_views_facturas__form_html_erb___1959803079335200881_70323378590380'   actionview (4.2.0) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'   actionview (4.2.0) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'   actionview (4.2.0) lib/action_view/helpers/capture_helper.rb:38:in `capture'   actionview (4.2.0) lib/action_view/helpers/form_helper.rb:444:in `form_for'   simple_form (3.1.0) lib/simple_form/action_view_extensions/form_helper.rb:26:in `block in simple_form_for'   simple_form (3.1.0) lib/simple_form/action_view_extensions/form_helper.rb:45:in `with_simple_form_field_error_proc'   simple_form (3.1.0) lib/simple_form/action_view_extensions/form_helper.rb:25:in `simple_form_for'   app/views/facturas/_form.html.erb:4:in `_app_views_facturas__form_html_erb___1959803079335200881_70323378590380'   actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'   activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'   actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'   actionview (4.2.0) lib/action_view/template.rb:143:in `render'   actionview (4.2.0) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'   actionview (4.2.0) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'   actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'   activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'   activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'   activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'   actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'   actionview (4.2.0) lib/action_view/renderer/partial_renderer.rb:309:in `render'   actionview (4.2.0) lib/action_view/renderer/renderer.rb:47:in `render_partial'   actionview (4.2.0) lib/action_view/helpers/rendering_helper.rb:35:in `render'   app/views/facturas/new.html.erb:3:in `_app_views_facturas_new_html_erb___1900732511694118275_70323400237940'   actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'   activesupport (4.2.0) lib/active_support   :in `instrument'   actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'   actionview (4.2.0) lib/action_view/template.rb:143:in `render'   actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'   actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'   activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'   activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'   activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'   actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'   actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'   actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'   actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'   actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'   actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'   actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'   actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'   actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'   actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'   actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'   actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'   actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'   actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'   actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'   activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'   /users/davefogo/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'   activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'   actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'   actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'   activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'   actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'   action 

it looks have undefined method #presupuesto_id in view, in app/views/facturas/_form.html.erb line 8. may have required params being commented out @ bottom of controller file or trying call method(presupuesto_id) on wrong object (@factura).it helpful see both of rendered views in order see how objects being passed i.e _form.html.erb , new.html.erb.

this write gives decent explanation of controller/view relationship explanation of instance variable within each action. http://www.sitepoint.com/building-your-first-rails-application-views-and-controllers/

try change form id of presupuesto instead of title. if works need add new method or scope factura model find presupuesto name 'title' , assign newly created instance of factura.

        <%= form.association :cliente, :label_method => :nombre,  label: "cliente", :include_blank => false %>     <%= form.association :presupuesto, :label_method => :nombre,  label: "presupuesto", :include_blank => false %> 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -