Ruby on rails 4 通过关联模型的实例变量了解Rails控制器-视图连接

Ruby on rails 4 通过关联模型的实例变量了解Rails控制器-视图连接,ruby-on-rails-4,view,controller,associations,instance-variables,Ruby On Rails 4,View,Controller,Associations,Instance Variables,我是一个新手,试图了解如何在控制器中正确使用关联模型的实例变量,使它们可用于视图。这是怎么回事?有标准吗?我不断得到未定义的方法错误。@example=example.find(params[:id])或@example=example.find(example\u params)的正确用法是什么?对于关联模型?每个控制器操作是否都有特定的@instance变量公式(例如,所有用于索引)? 有人知道关于实例变量的控制器视图使用的全面指南吗 代码 模型 class-prespuesto:orden

我是一个新手,试图了解如何在控制器中正确使用关联模型的实例变量,使它们可用于视图。这是怎么回事?有标准吗?我不断得到未定义的方法错误。@example=example.find(params[:id])或@example=example.find(example\u params)的正确用法是什么?对于关联模型?每个控制器操作是否都有特定的@instance变量公式(例如,所有用于索引)? 有人知道关于实例变量的控制器视图使用的全面指南吗

代码

模型

class-prespuesto:ordenes
有一个:factura
验证:fecha、:titulo、:cliente_id、:producto、presence:true
结束
类Factura
看法

\u form.html.erb
Factura
Factura
:nombre,标签:“客户”,:include_blank=>false%>
:titulo,标签:“Presupuesto”,:include_blank=>false%>
new.html.erb
新工厂
控制器

class FacturasController < ApplicationController

      # GET /facturas
      # GET /facturas.json
      def index
        @factura = Factura.all
        @cliente = Cliente.all
      end
      # GET /facturas/1
      # GET /facturas/1.json
      def show
        @factura = Factura.find(params[:id])
        @cliente = @factura.cliente
        @presupuesto = @factura.presupuesto
      end
      # GET /facturas/new
      def new
       @factura = Factura.new
       @presupuesto = Presupuesto.all
       #@ordenes = Ordene.where(aprobado_por_cliente: 1) 
      end
      # GET /facturas/1/edit
      def edit
      end
      # POST /facturas
      # POST /facturas.json
      def create
        @factura = Factura.new(factura_params)
        respond_to do |format|
          if @factura.save
            format.html { redirect_to @factura, notice: 'Factura was successfully 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 do |format|
          if @factura.update(factura_params)
            format.html { redirect_to @factura, notice: 'Factura was successfully 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 do |format|
          format.html { redirect_to facturas_url, notice: 'Factura was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_factura
          @factura = Factura.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def factura_params
          params.require(:factura).permit(:fecha_de_expedicion, :cliente_id, :presupuesto_id )
        end
    end
class facturascoontroller
错误 命名错误at/facturas/new #的未定义方法“prespuesto_id”

Started GET "/facturas/new" for ::1 at 2015-05-30 21:06:26 -0500
Processing by FacturasController#new as HTML
  Cliente Load (0.2ms)  SELECT "clientes".* FROM "clientes"
  Presupuesto Load (0.1ms)  SELECT "presupuestos".* FROM "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' for #<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
2015-05-30 21:06:26-0500开始获取“/facturas/new”for::1
FacturasController处理#新为HTML
客户负载(0.2ms)从“客户”中选择“客户”。*
预压力加载(0.1ms)从“预压力”中选择“预压力”。*
渲染的facturas/_form.html.erb(21.1ms)
布局/应用程序中呈现的facturas/new.html.erb(22.6ms)
在29毫秒内完成500个内部服务器错误
NoMethodError-未定义的#的方法“presupuesto_id”:
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\u view/helpers/tags/collection\u select.rb:16:in'block in render'
actionview(4.2.0)lib/action\u view/helpers/tags/collection\u select.rb:16:in'render'
actionview(4.2.0)lib/action\u view/helpers/form\u options\u helper.rb:202:in'collection\u select'
actionview(4.2.0)lib/action\u view/helpers/form\u options\u helper.rb:789:in'collection\u select'
简单表单(3.1.0)lib/simple\u form/inputs/collection\u选择输入。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_19598037335200881_70323378590380'
actionview(4.2.0)lib/action\u view/helpers/capture\u helper.rb:38:in'block in capture'
actionview(4.2.0)lib/action\u view/helpers/capture\u helper.rb:200:in`with\u output\u buffer'
actionview(4.2.0)lib/action\u view/helpers/capture\u helper.rb:38:in'capture'
actionview(4.2.0)lib/action_view/helpers/form_helper.rb:444:in'form_for'
simple\u form(3.1.0)lib/simple\u form/action\u view\u extensions/form\u helper.rb:26:in`block in simple_
class FacturasController < ApplicationController

      # GET /facturas
      # GET /facturas.json
      def index
        @factura = Factura.all
        @cliente = Cliente.all
      end
      # GET /facturas/1
      # GET /facturas/1.json
      def show
        @factura = Factura.find(params[:id])
        @cliente = @factura.cliente
        @presupuesto = @factura.presupuesto
      end
      # GET /facturas/new
      def new
       @factura = Factura.new
       @presupuesto = Presupuesto.all
       #@ordenes = Ordene.where(aprobado_por_cliente: 1) 
      end
      # GET /facturas/1/edit
      def edit
      end
      # POST /facturas
      # POST /facturas.json
      def create
        @factura = Factura.new(factura_params)
        respond_to do |format|
          if @factura.save
            format.html { redirect_to @factura, notice: 'Factura was successfully 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 do |format|
          if @factura.update(factura_params)
            format.html { redirect_to @factura, notice: 'Factura was successfully 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 do |format|
          format.html { redirect_to facturas_url, notice: 'Factura was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_factura
          @factura = Factura.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def factura_params
          params.require(:factura).permit(:fecha_de_expedicion, :cliente_id, :presupuesto_id )
        end
    end
Started GET "/facturas/new" for ::1 at 2015-05-30 21:06:26 -0500
Processing by FacturasController#new as HTML
  Cliente Load (0.2ms)  SELECT "clientes".* FROM "clientes"
  Presupuesto Load (0.1ms)  SELECT "presupuestos".* FROM "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' for #<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
        <%= form.association :cliente, :label_method => :nombre,  label: "Cliente", :include_blank => false %>
    <%= form.association :presupuesto, :label_method => :nombre,  label: "Presupuesto", :include_blank => false %>