2016-09-14 68 views
0

我想用simple_form生成一個表單。我在Gemfile中的寶石,跑bundle installgenerate simple_form:install用simple_form視圖生成表單erb

這是new.html.erb:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <% simple_form_for @post do |f| %> 
    <%= f.input :image %> 
    <%= f.input :caption %> 
    <%= f.button :submit %> 
    <% end %> 
</body> 
</html> 

我post_controller.rb

class PostsController < ApplicationController 
    def index 
    end 

    def new 
     @post = Post.new 
    end 
end 

我的模型post.rb:

class Post < ActiveRecord::Base 
    validates :image, presence: true 

    has_attached_file :image, styles: { :medium => '640x'} 
    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/ 
    validates :image, presence: true 

end 

And my routes.rb:

Rails.application.routes.draw do 
    resources :posts 
    root 'posts#index' 
end 
  • 我沒有得到任何錯誤,但是當我加載視圖與本地主機,我只看到一個簡潔的白色網頁
  • 當我檢查與瀏覽器的工具生成的觀點,我看到所有的HTML標籤,但沒有erb。
  • 當我刪除了該局及喜歡寫東西Test,我可以看到測試在瀏覽器

我想不出有什麼錯我的代碼,任何想法?在此先感謝

回答

0

我想你錯過了=在這段代碼:<% simple_form_for @post do |f| %>。 你可以做到這一點,如下所示:

<body> 
<%= simple_form_for @post do |f| %> 
    <%= f.input :image %> 
    <%= f.input :caption %> 
    <%= f.button :submit %> 
<% end %> 
</body> 
+0

感謝回答。這與我已有的有何不同? – cerealCode

+0

我更新了我的答案,請再次檢查。 –

+0

非常感謝,我認爲第一行代碼從來沒有'='。 – cerealCode