首页 \ 问答 \ 可能很简单。(Probably very simple. Ruby on rails error when using web interface)

可能很简单。(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 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

原文:https://stackoverflow.com/questions/24194840
更新时间:2022-02-05 16:02

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)