rails 4.1中未定义的局部变量或方法`form'

时间:2015-11-05 14:11:05

标签: ruby-on-rails ruby-on-rails-4

在我的_form部分中,我渲染了另一个部分,如下面的

<%= render :partial => 'test', :object => @application.type, \
              :locals => {:form => form, \
              :application_type => "application[type]"} %>

当我尝试加载表单时,我收到此错误

undefined local variable or method `form' for #<#<Class:0x6daf2c8>:0x8dd3c80>

我的测试部分

<%= fields_for application_type, application do |application_f| %>
    <div>
      <div>
        <%= application_f.label :uni_id, "University" %>
        <%= application_f.collection_select :uni_id, @unis, :id, :check, {:include_blank => true} %>
      </div>
    </div>
    <div>         

<% end %>

我最近从3.2更新到rails 4.1。它以前工作但现在显示错误。我想这是语法错误但无法解决它。

3 个答案:

答案 0 :(得分:0)

也许您需要在顶级模板中替换这样的内容:

<%= render partial: "form", form: form %>

或者像这样:

<%= render partial: "form", object: form %>

有这样的东西(现在需要 locals 参数)

<%= render partial: "form", locals: { form: form } %>

答案 1 :(得分:0)

相当多的错误:

<%= render partial: 'test', locals: {form: form }, object: @application %>

<%= form.fields_for application.type, application do |application_f| %>
    <div>
      <div>
        <%= application_f.label :uni_id, "University" %>
        <%= application_f.collection_select :uni_id, @unis, :id, :check, {:include_blank => true} %>
      </div>
     </div>
    </div>         
<% end %>

应该为您提供正确的语法。

错误归结为未填充的form变量。这似乎来自<%= render电话。没有上下文,我能给出的最好的解决方案是它应该如何

<%= form_for @variable do |form| %>
   <%= render partial: "test", locals: {form:form}, object: @application %>
<% end %>

答案 2 :(得分:0)

在处理 Rails 6 应用程序

时,我遇到了同样的问题

由于来自的一些冲突,我对自己的路线进行了覆盖

resources :sessions

resources :sessions, as: :sets

这是我修复的方式

app / views / sessions / new.html.erb

<h1>New Session</h1>

<%= form_with(model: @session, url: sets_path, local: true) do |form| %>
  <%= render partial: 'form', session: @session, locals: { form: form } %>
<% end %>

<%= link_to 'Back', sets_path %>

注意:由于路由替换已完成,因此为新表单定义了URL。因此,form_with容器与_form.html.erb部分隔离。这是为了避免为_form.html.erbnew.html.erb创建多个edit.html.erb files部分,因为它们具有不同的URL。

app / views / sessions / edit.html.erb

<%= form_with(model: @session, url: set_path(@session), local: true) do |form| %>
  <%= render partial: 'form', session: @session, locals: { form: form } %>
<% end %>

<%= link_to 'Show', set_path(session) %> |
<%= link_to 'Back', sets_path %>

注意:由于路由替换已完成,因此为新表单定义了URL。因此,form_with容器与_form.html.erb部分隔离。这样做是为了避免为_form.html.erbnew.html.erb创建多个edit.html.erb files部分,因为它们具有不同的URL。

app / views / sessions / _form.html.erb

<% if @session.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@session.errors.count, "error") %> prohibited this session from being saved:</h2>

    <ul>
      <% @session.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
<% end %>

<div class="field">
  <%= form.label :name %>
  <%= form.text_field :name %>
</div>

<div class="field">
  <%= form.label :prefix %>
  <%= form.text_field :prefix %>
</div>

<div class="actions">
  <%= form.submit %>
</div>

注意:请注意,_form.html.erb文件中没有表单帮助程序定义,因为它们现在已移至new.html.erbedit.html.erb文件中由于路由的更改,URL有所不同。

app / views / sessions / index.html.erb

<p id="notice"><%= notice %></p>

<h1>Sessions</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Prefix</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @sessions.each do |session| %>
      <tr>
        <td><%= session.name %></td>
        <td><%= session.prefix %></td>
        <td><%= link_to 'Show', set_path(session) %></td>
        <td><%= link_to 'Edit', edit_set_path(session) %></td>
        <td><%= link_to 'Destroy', set_path(session), method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Session', new_set_path %>

注意:请注意new操作中对链接URL的修改。

app / views / sessions / show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @session.name %>
</p>

<p>
  <strong>Prefix:</strong>
  <%= @session.prefix %>
</p>

<%= link_to 'Edit', edit_session_path(@session) %> |
<%= link_to 'Back', sessions_path %>

注意:请注意在editback操作中对链接URL的修改。

app / controllers / sessions_controller.rb

class SessionsController < ApplicationController
  before_action :set_session, only: [:show, :edit, :update, :destroy]

  # GET /sessions
  # GET /sessions.json
  def index
    @sessions = Session.all
  end

  # GET /sessions/1
  # GET /sessions/1.json
  def show
  end

  # GET /sessions/new
  def new
    @session = Session.new
  end

  # GET /sessions/1/edit
  def edit
  end

  # POST /sessions
  # POST /sessions.json
  def create
    @session = Session.new(session_params)

    respond_to do |format|
      if @session.save
        format.html { redirect_to sets_path(@sesion), notice: 'Session was successfully created.' }
        format.json { render :show, status: :created, location: sets_path(@sesion) }
      else
        format.html { render :new }
        format.json { render json: @session.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /sessions/1
  # PATCH/PUT /sessions/1.json
  def update
    respond_to do |format|
      if @session.update(session_params)
        format.html { redirect_to sets_path(@sesion), notice: 'Session was successfully updated.' }
        format.json { render :show, status: :ok, location: sets_path(@sesion) }
      else
        format.html { render :edit }
        format.json { render json: @session.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /sessions/1
  # DELETE /sessions/1.json
  def destroy
    @session.destroy
    respond_to do |format|
      format.html { redirect_to sets_url, notice: 'Session was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Only allow a list of trusted parameters through.
    def session_params
      params.require(:session).permit(:name, :prefix)
    end
end

注意:请注意在createupdate操作中对重定向URL的修改。

仅此而已。

我希望这会有所帮助