0

我在 OneMonth Rails 中工作,我错误地删除了控制台内 id 为 6 的用户密码。现在我无法在本地主机上创建新的 pin。如何修复我收到的“PinsController#index 中的 SystemStackError”错误?

index.html.erb:

    <div class="panel panel-default">
  <div class="panel-heading">
    <div class="panel-title">

<h1>Listing Pins</h1>

<table>
  <thead>
    <tr>
      <th>Image</th>
      <th>Description</th>
      <th>User</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @pins.each do |pin| %>
      <tr>
        <td><%= image_tag pin.image.url(:medium) %></td>
        <td><%= pin.description %></td>
        <td><%= pin.user.email if pin.user %></td>
        <td><%= link_to 'Show', pin %></td>
        <% if pin.user == current_user %>
          <td><%= link_to 'Edit', edit_pin_path(pin) %></td>
          <td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>

<br>
<% if user_signed_in? %>
  <%= link_to 'New Pin', new_pin_path %>
<% end %>

这是我在 pins_controller.rb 文件中的新操作中编写的代码。

def new
  @pin = current_user.pins.build
end

这是服务器日志:

    Started GET "/pins" for ::1 at 2016-04-25 14:49:20 -0500
Processing by PinsController#index as HTML
  [1m[36mPin Load (0.0ms)[0m  [1mSELECT "pins".* FROM "pins"[0m
  Rendered pins/index.html.erb within layouts/application (4.0ms)
Completed 500 Internal Server Error in 17ms (ActiveRecord: 0.0ms)

SystemStackError (stack level too deep):
  actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:79


  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (79.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (1.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (1.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (84.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (1.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (1.0ms)
  Rendered c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (173.0ms)

这是我从您的上一条评论中收到的错误图片

下面是我的 pins_controller.rb 文件:

class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  def index
    @pins = Pin.all
  end

  def show
  end

  def new
    @pin = current_user.pins.build
  end

  def edit
  end

  def create
    @pin = current_user.pins.build(pin_params)
    if @pin.save
      redirect_to @pin, notice: 'Pin was successfully created.'
    else
      render :new
    end
  end

  def update
    if @pin.update(pin_params)
      redirect_to @pin, notice: 'Pin was successfully updated.'
    else
      render :'edit'
    end
  end

  def destroy
    @pin.destroy
    redirect_to pins_url
  end

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

    def current_user
       @pins = current_user.pins.find_by(params[:id])
       redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil? 
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pin_params
      params.require(:pin).permit(:description, :image)
    end
  end
4

1 回答 1

0

Update your new method, Its supposed to be like this.

def new
  @pin = Pin.new
end

def create
  @pin = current_user.pins.build(YOUR PARAMS HERE)
end
于 2016-04-24T06:28:18.527 回答