我在这个Ruby代码中没有看到的SyntaxError

时间:2014-02-13 06:27:32

标签: ruby-on-rails ruby

当试图访问我的练习webapp的页面时(通过localhost),我收到错误

syntax error, unexpected ':', expecting ';' or '\n'

它应该在这个文件的第4行。代码中没有“:”冒号。第4行是:@contact = Contact.new

我不理解什么?这是在Controller.rb文件

class ContactsController < ApplicationController 

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(secure_params)
    if @contact.valid?

      flash[:notice] = "Message sent from #{@contact.name}." 
      redirect_to root_path
    else
      render :new
    end 
  end

  private
  def secure_params
    params.require(:contact).permit(:name, :email, :content)
  end 
end

据我所知,这是完整的错误:

SyntaxError at /contacts/new

syntax error, unexpected ':', expecting ';' or '\n'

1 个答案:

答案 0 :(得分:0)

在Models目录中的contact.rb文件中实际上是一个错误。弹出的Better Errors页面对初学者来说真的没有帮助。当我记得查看apps logs目录时,我看到了这个:SyntaxError - /Users/NormalUse/ror/learn-rails/app/models/contact.rb:1: syntax error, unexpected ':', expecting ';' or '\n'我最终在其中一行代码中有一个额外的空白空格。谢谢你的回复。作为一个初学者,像我一样被投票放弃是令人沮丧的,但与此同时,我可以看到为什么原始问题很难让其他人帮忙。