ruby - Rails 应用程序 Controller 中的语法错误

标签 ruby ruby-on-rails-3 twitter-bootstrap-rails

我将 twitter bootstrap 库用于我新的简单 Rails 应用程序 并为产品模型使用了一些脚手架 我更新了 GemFile 以及 bundle 命令和 rails g bootstrap。 当我运行网页时出现以下错误:

SyntaxError in ProductsController#index

/Library/Ruby/Gems/1.8/gems/twitter-bootstrap-rails-2.1.4/app/helpers/glyph_helper.rb:9: syntax error, unexpected ':'
...   content_tag :i, nil, class: names.map{|name| "icon-#{name...

我不知道为什么会这样!

这是我的 GemFile:

source 'https://rubygems.org'

gem 'rails', '3.2.7'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

gem 'json'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
  gem 'twitter-bootstrap-rails'
end

gem 'jquery-rails'

Controller 代码:

class ProductsController < ApplicationController
  # GET /products
  # GET /products.json
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @products }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @product }
    end
  end

  # GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, :notice => 'Product was successfully created.' }  
        format.json { render :json => @product, :status => :created, :location => @product }
      else
        format.html { render :action => "new" }
        format.json { render :json => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.json
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to @product, :notice => 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @product.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end
end

最佳答案

解决方案似乎很简单。我猜你使用的一些 gem 的版本需要 ruby​​ 版本 > 1.9.0 因为符号特性所以你可以做 bar: foo 而不是 :bar => foo

因此,如果您更改为 ruby​​ 1.9.2 或 1.9.2,该问题就会消失!

关于ruby - Rails 应用程序 Controller 中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13096038/

相关文章:

jquery - Uncaught Error : no such method 'show' for tooltip widget instance

Ruby 代码混淆

ruby - gem date_validator 不工作 Ruby 1.9.3 Rails 3.2.1

Ruby:动态创建具有值(value)的类

ruby-on-rails - 给定代码的输出是什么

ruby-on-rails - ruby 强度和用途

css - bootstrap 词缀行高与导航不准确

ruby - 在 Rails 应用程序中生成 Flat UI Pro 时无法在任何源路径中找到 'flat-ui.css'

ruby-on-rails - 如何从 Ruby 数组创建平均值?

ruby - 如何 stub 在初始化方法中调用的方法