1

我对 Ruby on Rails 很陌生,如果这是一个愚蠢的错误,请原谅。

我使用 rails generate scaffold 命令生成了一个带有标题:字符串和消息:文本的“板”脚手架。现在,我正在尝试访问 localhost:3000/boards/new,当我尝试访问 board.message 时出现“Boards#show 中的 NoMethodError”错误。当我尝试访问 board.title 时,我没有收到任何错误。

代码:

form.html.erb

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

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

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

我在第 20 行特别收到错误(<%= f.text_area :message %>)

板子.rb

class Board < ActiveRecord::Base
  attr_accessible :message, :title
  has_many :posts
end

*boards_controller.rb*

class BoardsController < ApplicationController
  # GET /boards
  # GET /boards.json
  def index
    @boards = Board.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @boards }
    end
  end

  # GET /boards/1
  # GET /boards/1.json
  def show
    @board = Board.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @board }
    end
  end

  # GET /boards/new
  # GET /boards/new.json
  def new
    @board = Board.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @board }
    end
  end

  # GET /boards/1/edit
  def edit
    @board = Board.find(params[:id])
  end

  # POST /boards
  # POST /boards.json
  def create
    @board = Board.new(params[:board])

    respond_to do |format|
      if @board.save
        format.html { redirect_to @board, notice: 'Board was successfully created.' }
        format.json { render json: @board, status: :created, location: @board }
      else
        format.html { render action: "new" }
        format.json { render json: @board.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /boards/1
  # PUT /boards/1.json
  def update
    @board = Board.find(params[:id])

    respond_to do |format|
      if @board.update_attributes(params[:board])
        format.html { redirect_to @board, notice: 'Board was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @board.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /boards/1
  # DELETE /boards/1.json
  def destroy
    @board = Board.find(params[:id])
    @board.destroy

    respond_to do |format|
      format.html { redirect_to boards_url }
      format.json { head :no_content }
    end
  end
end

路线.rb

Anonymous::Application.routes.draw do

  resources :boards


  resources :posts

  root :to => "boards#index"

end

谁能给我解释一下?

4

0 回答 0