2013-07-01 78 views
5

我剛剛嘗試了Ruby on Rails與Getting Started with Rails教程。我遵循了所有的步驟,但我一直在PostsController#create中收到錯誤TypeError。Rails入門教程中的TypeError教程

當我在步驟5.6將數據保存在控制器中時,會發生這種情況。

我PostsController.rb看起來是這樣的:

class PostsController < ApplicationController 

    def new 
    end 

    def create 
    @post = Post.new(post_params) 

    @post.save 
    redirect_to @post 
    end 

    private 
    def post_params 
    params.require(:post).permit(:title, :text) 
    end 
end 

和我在本地主機:3000 /職位/新。我請求POST,它失敗,出現以下:

不能轉換成符號串

app/controllers/posts_controller.rb:15:in `post_params' 
app/controllers/posts_controller.rb:7:in `create' 

這個錯誤發生在加載下列文件:發佈

你可以在我的my GitHub repo找到我的所有的代碼。

請幫助:(

+0

歡迎的StackOverflow!爲了獲得更快的響應,最好在帖子中發佈所有相關的代碼部分。 – claptimes

+0

Awh,特別爲Rails 3.2.xx提供了一個單獨的教程。遵循這些步驟解決了這個問題。 http://guides.rubyonrails.org/v3.2.13/getting_started.html –

回答

7

您正在使用的軌道版本3.2.xx,在軌道上的版本3.2.xx不包括strong_parameters寶石

注意,高清post_params是私人的。這種新的方法可以防止一個 從通過操縱傳遞到模型中的散列 設定模型的屬性攻擊。欲瞭解更多信息,請參閱這篇博客 約Strong Parameters.


  1. 添加gem "strong_parameters"到你的Gemfile,然後運行bundle install

  2. 包括ActiveModel::ForbiddenAttributesProtection你的模型或創建config/initializers/strong_parameters.rb,並把這個:

    ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection))

  3. config.active_record.whitelist_attributes = falseconfig/application.rb

https://github.com/rails/strong_parameters