#<#<Class:0x4409040>:0x43d7720>的未定义局部变量或方法'plane'(Ruby on Rails)

时间:2019-09-19 02:29:35

标签: ruby-on-rails ajax

当我尝试在 planes.html.erb 中执行link_to edit_plane_path(plane)时,我尝试使用模式形式在Ruby on Rails中创建一个编辑对象,但出现以下错误:

#<#:0xd580e98>的未定义局部变量或方法'plane' 你的意思是? @飞机                plane_url                @planes

但是,尝试其中的任何一项并不能真正改变我仍然在页面上看到错误,甚至无法加载它的事实。

planes.html.erb

<table>
    <thead>
        <tr>
            <th>Image</th>
            <th>Operating agency</th>
            <th>Date</th>
            <th>Call sign</th>
            <th>Country</th>
            <th>Info</th>
            <th>Action</th>
      </tr>
<thead>
    <tbody>
        <% @planes.each do |p| %>
            <tr class="gradeC">
                <td>
                     <%= image_tag(p.image, size: "150x150")%></td>
                        <td class="w-25"><%= p.provider%>
                        </td>
                        <td><%= p.brand%></td>
                        <td class="center"><%= p.year%></td>
                        <td class="center"><%= p.call_sign%></td>
                        <td><%= p.country%></td>
                        <td><%= p.info%></td>
                        <td class="text-right">
                            <div class="btn-group">
                                <%= link_to 'Edit', edit_plane_path(plane), remote: true, :class =>'btn white btn btn-xs' %>
                                <button class="btn-white btn btn-xs">Delete</button>
                            </div>
                        </td>
                    </tr>

                    </tbody>
                    <% end %>
</table>

_form.html.erb

<!-- FORM EDIT -->
    <div id="edit-plane" class="modal fade" aria-hidden="true">
    <div class="modal-dialog modal-md">
    <div class="modal-content">
    <div class="modal-body">
    <div class="row">
    <div class="col-sm-12"><h3 class="m-t-none m-b">Edit plane</h3>
    </div>
    </div>
    <%= form_for @plane, url: planes_path, remote: true do |f| %>
    <div class="form-group row">
    <div class="col-sm-6">
    <label>Brand</label> 
        <%= f.text_field :brand, class:"form-control"%> 
    </div>
    <div class="col-sm-5">
    <label>Model</label> 
        <%= f.text_field :model, class:"form-control"%> 
    </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-5">
        <label>Year</label>
            <%= select_tag :hyear, options_for_select(["2015","2016","2017","2018","2019"], "2019"), class:"select2_demo_1 form-control" %>
            <%= f.hidden_field :year %>
        </div>

        <div class="col-sm-6">
        <label>Country</label>
            <%= select_tag :hcountry, nil, class:"select2_demo_1 form-control" %>
                <%= f.hidden_field :country %>
            </div>

            <script language="javascript">
                populateCountries("hcountry");
            </script>
    </div>

    <div class="form-group row">
        <div class="col-sm-6">
            <label>Provider</label>
                <%= select_tag :hprovider, options_for_select([ "Test", "Test2"], "Test"), class:"select2_demo_1 form-control"%></select>
                <%= f.hidden_field :provider %>
        </div>


        <div class="col-sm-5">
        <label>Tactical call sign</label> 
            <%= f.text_field :call_sign, class:"form-control"%> 
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-6">
        <label>Extra info</label> 
            <%= f.text_area :info %>
        </div>

        <div class="col-sm-5">
            <label>Image</label>       
            <%= f.file_field :image %>   
        </div>

    </div>

    <!-- SUBMIT -->
    <button class="btn btn-primary btn-lg float-right ml-2" id="cancelbtn">Cancel</button>
    <%= f.submit "Submit", id:"edpla", class: 'btn btn-primary btn-lg float-right'%>
    </div>
    <% end %>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>

edit.js.erb

// Add the dialog title
$('#edit-plane h3').html("<i class=' glyphicon glyphicon-pencil'></i> Edit Plane");

// Render the edit form
$('.modal-body').html('<%= j render("form") %>');

// Show the dynamic dialog
$('#edit-plane').modal("show");

// Set focus to the first element
$('#edit-plane').on('shown.bs.modal', function () {
      $('.first_input').focus()
  })

update.js.erb

$('#edit-plane').modal('toggle');
$('#customer_<%= @plane.id %>').replaceWith('<%= j render (@plane) %>')

planes_controller.rb

class PlanesController < ApplicationController
    def planes 
        @plane = Plane.new
        @planes = Plane.all
    end

    def create
        @plane = Plane.new(plane_params)
        if @plane.save
          flash[:success] = "Plane successfully added"
          redirect_to :planes => 'post', :action => 'planes'
        else
          flash[:error] = "Something went wrong"
          render 'planes'
        end
    end

    def edit
      @plane = Plane.find(params[:id])
    end

    def update
      respond_to do |format|
        if @plane.update(plane_params)
          format.json { head :no_content }
          format.js
        else
          format.json { render json: @customer.errors.full_messages, status: :unprocessable_entity }
        end
      end
    end

    private
    def plane_params
        params.require(:plane).permit(:brand, :model, :provider, :call_sign, :user, :country, :image, :info, :year)
    end

end


我的目标是通过模式表格在表格中编辑平面,但是我现在甚至无法显示页面。

1 个答案:

答案 0 :(得分:0)

这里:

plane

...您正在将每个p作为<%= link_to 'Edit', edit_plane_path(p), remote: true, class: 'btn white btn btn-xs' %> 传递。因此,请尝试:

>>> f = StringIO('''\
... <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
... <xsd:element name="a" type="AType"/>
... <xsd:complexType name="AType">
...   <xsd:sequence>
...     <xsd:element name="b" type="xsd:string" />
...   </xsd:sequence>
... </xsd:complexType>
... </xsd:schema>
... ''')
>>> xmlschema_doc = etree.parse(f)
>>> xmlschema = etree.XMLSchema(xmlschema_doc)
>>> valid = StringIO('<a><b></b></a>')
>>> doc = etree.parse(valid)
>>> xmlschema.validate(doc)
True