Use form that corresponds to another controller

1

Context

  • Form is created through command

    $ rails g scaffold Cotizacion
    
  • Template installed from link

  • Objective and problem

    The idea is to integrate the form new , which creates a new quote to the bootstrap template.

    Understanding that I am trying to use a form that corresponds to another controller.

    The structure of the project is as follows. You are trying to call the _form.html.erb form of the quotes view, from the index.html.erb file corresponding to the view < em> creatives . Both views occupy different controllers (can that be done?)

    Invocation to form from index.html.erb :

    <%= render :partial =>'cotizaciones/form' , cotizacion: @cotizacion%>
    <%= render 'navbar' %>
    <%= render 'header' %>
    <%= render 'services' %>
    <%= render 'portfolio' %>
    <%= render 'call_to_action' %>
    <%= render 'contact' %>
    

    The message displayed by the application when lifting the page is the following:

      

    undefined local variable or method 'quote' for   < #: 0x007fcae29cb080 > Did you mean? quote_url

         

    Extracted source (around line # 1):

          <%= form_with(model: cotizacion, local: true) do |form| %>
          <%= form_with(model: cotizacion, local: true) do |form| %>
          <% if cotizacion.errors.any? %>
          <div id="error_explanation">
          <h2><%= pluralize(cotizacion.errors.count, "error") %> prohibited 
           this cotizacion from being saved:</h2>
    
        
    asked by Sebastian Campos Davila 21.07.2017 в 20:12
    source

    1 answer

    0

    The problem is that you are not recognizing the variable cotizacion because you are performing the assignment incorrectly: to pass variables to a partial you must use the attribute locals .

    To solve it simply assign the variable cotizacion using the attribute locals :

    <%= render partial: 'cotizaciones/form', locals: { cotizacion: @cotizacion } %>
    

    Alternatively, as an alternative option, you can eliminate the use of the attribute partial and so you can pass the variable as you were doing:

    <%= render 'cotizaciones/form', cotizacion: @cotizacion %>
    

    The two options generate the same result, so I recommend using the second one (it is shorter), however use the one that is the most simple / intuitive and consistent in its use throughout your application.

        
    answered by 21.07.2017 / 23:50
    source