可能很简单。使用Web界面时Ruby on rails错误

时间:2014-06-12 22:24:40

标签: ruby-on-rails syntax localhost renderpartial

我正在关注我学校的ruby on rails教程。我非常仔细地按照说明操作,但是我遇到了一个错误。当我执行localhost:3000 / books时,我的Web界面工作正常(这导致我看到我的书库数据库,我使用Firefox SQL管理器添加)。但是,当我点击" New Book"通过Web界面创建一个新条目,我得到: BooksController中的SyntaxError #new

(注意:当我执行localhost:3000 / publisher / new时,它运行正常。当我执行localhost:3000 / authors / new时,它不起作用。我希望这个问题与当我使用Author解决问题时,Book将得到解决。以下所有信息都与我项目的书籍部分有关。)

c:/Sites/Summer_2014/BookManager/app/models/author.rb:4: syntax error, unexpected ':', expecting keyword_end validates_presence_of : :first_name, :last_name ^

这是应用程序跟踪:

app/views/books/_authors_checkboxes.html.erb:2:in `_app_views_books__authors_checkboxes_html_erb__770955296_43280532'
app/views/books/_form.html.erb:26:in `block in _app_views_books__form_html_erb___935905997_43301772'
app/views/books/_form.html.erb:1:in `_app_views_books__form_html_erb___935905997_43301772'
app/views/books/new.html.erb:3:in `_app_views_books_new_html_erb___378444391_43261320'

以下是我认为相关的代码部分:

应用程序/视图/书籍/ _authors_checkboxes.html.erb:

<p>
    <% for author in Author.alphabetical %>
        <%= check_box_tag "book[author_ids][]", author.id, @book.authors.include?(author) %>
        <%= author.name %>
        <br />
    <% end %>
</p>

应用程序/视图/书籍/ _form.html.erb:

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

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

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :year_published %><br>
    <%= f.number_field :year_published %>
  </div>
  <div class="field">
    <%= f.label :publisher_id %><br>
    <%= f.collection_select :publisher_id, Publisher.alphabetical, :id, :name, :prompt => 'Please select a publisher'  %>
  </div>
  <%= render partial: 'authors_checkboxes' %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

应用程序/视图/书籍/ new.html.erb:

<h1>New book</h1>

<%= render 'form' %>

<%= link_to 'Back', books_path %>

编辑:这是我的apps / models / author.rb:

class Author < ActiveRecord::Base
    has_many :book_authors
    has_many :books, through: :book_authors
    validates_presence_of : :first_name, :last_name
    scope :alphabetical, -> {order('last_name, first_name')}

    def name
        "#{last_name}, #{first_name}"
    end
end

1 个答案:

答案 0 :(得分:0)

ruby编译器对此非常清楚,它不会指望第4行:中的单author.rb。它显然不是有效的ruby,你应该遵循{ {3}}。这条线应该简单地说

validates_presence_of :first_name, :last_name