5.10中的Ruby on Rails指南(博客)错误

时间:2015-12-18 08:17:16

标签: ruby ruby-on-rails-4.1

我正在学习Ruby on Rails并开始通过http://guides.rubyonrails.org/getting_started.html#showing-articles创建这个小博客应用程序。 现在我在5.10,我需要在表单中添加验证,以便用户添加一个长度小于5的tittle。

所以这是我的articles_controller.rb:

 public class MainActivity extends Activity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                "iPhone 4S", "Samsung Galaxy Note 800",
                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);   
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
    }    
}

在这个视图中我有一个错误(new.html.erb):

    class ArticlesController < ApplicationController
    def index
        @articles = Article.all
    end

    def show
        @article = Article.find(params[:id])
    end

    def new
    end

    def create
        #render plain: params[:article].inspect

        #@article = Article.new(params[:article])

        @article = Article.new(article_params)

        #@article.save
        if @article.save
            redirect_to @article
        else
            render 'new'
        end

        redirect_to @article
    end

    private
        def article_params
            params.require(:article).permit(:title, :text)
        end
end

这是我得到的错误:

enter image description here

我是Ruby和rails的新手,所以我希望能得到一些帮助。

1 个答案:

答案 0 :(得分:2)

您没有初始化@article实例变量,但您尝试在new视图中使用它。你应该:

def new
  @article = Article.new
end