RJS动态更新ActionView :: Template ::错误(未定义的方法`每个'对于nil:NilClass)

时间:2016-06-22 02:00:54

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

我正在使用Rails 4.2.6 / PostgreSQL并尝试在具有表单并通过scaffold生成的索引中进行动态更新。我认为我做得对,但是通过rjs渲染部分问题。 Bellow是一个错误和代码。任何的想法?或者RJS仍然可用于Rails 4 ??

服务器

Started POST "/users" for 127.0.0.1 at 2016-06-21 18:50:59 -0700
Processing by UsersController#create as JS
  Parameters: {"utf8"=>"✓", "user"=>{"name"=>"Tim"}, "commit"=>"Create User"}
   (0.3ms)  BEGIN
  SQL (0.9ms)  INSERT INTO "users" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING     "id"  [["name", "Tim"], ["created_at", "2016-06-22 01:50:59.194072"], ["updated_at", "2016-06-22     01:50:59.194072"]]
   (63.8ms)  COMMIT
  Rendered users/_user.html.erb (37.6ms)
  Rendered users/create.js.erb (42.8ms)
Completed 500 Internal Server Error in 145ms (ActiveRecord: 65.4ms)

ActionView::Template::Error (undefined method `each' for nil:NilClass):
    1:     <% @users.each do |user| %>
    2:       <tr>
    3:         <td><%= user.name %></td>
    4:         <td><%= link_to 'Show', user %></td>
  app/views/users/_user.html.erb:1:in     `_app_views_users__user_html_erb___2381841639716957386_70119033868960'
  app/views/users/create.js.erb:1:in `_app_views_users_create_js_erb__1865303298608397742_70119034321000'
  app/controllers/users_controller.rb:30:in `create'

create.js.erb

$("#users").append("<%= escape_javascript(render @user) %>");

users_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
    @user = User.new
  end

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

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
        format.js
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name)
    end
end

应用/视图/用户/ index.html中

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

<h1>Listing Users</h1>

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

  <tbody id="users">
  <%= render @user %>
  </tbody>
</table>

<br>

<%= render 'form' %>

应用/视图/用户/ _form.html.erb

<%= form_for(@user, remote: true) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

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

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

如果我在创建操作中添加@users = User.all之类的内容,它会将所有用户呈现两次,如下图所示。 enter image description here

1 个答案:

答案 0 :(得分:0)

经过尝试和错误,我已经确认了Rajesh的评论中有一个答案。在尝试的过程中有点不清楚,但这是我如何解决这个问题。

<强> create.js.erb

之前

$("#users").append("<%= escape_javascript(render @user) %>");

$("#users").html("<%= escape_javascript(render @user) %>");

<强> users_controller.rb

之前     def创建       @user = User.new(user_params)

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
      format.js
    else
    .
    .

    def创建       @user = User.new(user_params)

  respond_to do |format|
    if @user.save
    @users = User.all
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
      format.js
    else
    .
    .

然后一切正常。 这些对于两者都非常有意义,每个都有零错误和附加的渲染行为。谢谢你回答评论Rajesh。下次,请尝试在答题部分发布。