我无法在本地服务器上打开pin页面

时间:2016-03-15 03:43:42

标签: html ruby-on-rails scaffolding

我试图打开本地服务器,但没有工作。成功打开第一页但是当我打开localhost:3000 / pins页面时失败。 pins_controller.rb

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 action: 'new'
    end
  end

  def update
    if @pin.update(pin_params)
      redirect_to @pin, notice: 'Pin was successfully updated.'
    else
      render action: '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(params[:id])
    end

    def correct_user
      @pin = current_user.pins.find_by(id: 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)
    end
end

index.home.erb

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

<h1>Listing Pins</h1>

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

  <tbody>
    <% @pins.each do |pin| %>
      <tr>
        <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>
      </tr>
    <% end %>
  </tbody>
</table>

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

我不知道为什么我可以在本地:3000 / pin页面。错误说PinsController #index中的SyntaxError。有谁知道如何解决这一问题?需要你的帮助!

2 个答案:

答案 0 :(得分:0)

你有index.home.erb,但你没有控制器的方法。

尝试添加:

def index
  @pins = Pin.all
end

此外,您的错误为PinsController#index,因此这意味着您未在index上定义PinsContoller方法。

更新:

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 action: 'new'
    end
  end

  def update
    if @pin.update(pin_params)
      redirect_to @pin, notice: 'Pin was successfully updated.'
    else
      render action: '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(params[:id])
    end

    def correct_user
      @pin = current_user.pins.find_by(id: 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)
    end
  end

更新2: index.html.erb:

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

<h1>Listing Pins</h1>

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

  <tbody>
  <% @pins.each do |pin| %>
    <tr>
      <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>
      </tr>
      <% end %>
  </tbody>
</table>

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

答案 1 :(得分:0)

正确设置Pins控制器动作。

def index
  @pins = Pin.all
end

def new    
  @pin = Pin.new
end

index.html.erb

中有错误
<% if @pin.user == current_user %> //no closing <% end %>
// @pin should be "pin" only.

您正在致电:

<% @pins.each do |pin| %> // used if pin.user not @pin.user