Search code examples
ruby-on-railssyntaxlocalhostrenderpartial

Probably very simple. Ruby on rails error when using web interface


I am following my school's ruby on rails tutorial. I have followed the instructions very closely, but I have come across an error. My web interface works fine when I do localhost:3000/books (this results in me seeing my database of my book entries, which I added using the Firefox SQL manager). However, when I click "New Book" to make a new entry through the web interface, I get: SyntaxError in BooksController#new

(NOTE: when I do localhost:3000/publisher/new, it works fine. When I do localhost:3000/authors/new, it doesn't work. I'm hoping the issue with the Author will be resolved when I fix the issue with the Book. All of the following information is about the books part of my project.)

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

This is the app trace:

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'

Here are the parts of code that I think are relevant:

app/view/books/_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>

app/views/books/_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 %>

app/views/books/new.html.erb:

<h1>New book</h1>

<%= render 'form' %>

<%= link_to 'Back', books_path %>

EDIT: Here is my 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

Solution

  • The ruby compiler is exceptionally clear on this one, it did not expect your solitary : in author.rb on line 4. It is clearly not valid ruby and you should follow the docs for validates_presence_of. The line should simply say

    validates_presence_of :first_name, :last_name