可能很简单。(Probably very simple. Ruby on rails error when using web interface)
我正在关注我学校的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
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 theBook
. 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
原文:https://stackoverflow.com/questions/24194840