2017-05-18 82 views
2

我正在學習一個教程,我必須創建一個目錄網站。由於需要有用戶可以撰寫評論的頁面,因此我會爲評論創建視圖和表單。我也做了步驟是:Rails應用程序視圖中的NoMethodError

1 - 創建的app /意見/審查的評論文件夾:

2 - 內創建該文件夾的部分稱爲_forms.html.erb的評論

<%= simple_form_for(@review) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :content, required: true %> 
    <%= f.hidden_field :place_id, required: true, value: @place_id %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

3 - 要渲染的地方審查/ _form /節目我補充一下:

<div class="col-md-9"> 
    <h3>Reviews by People</h3> 
    <%= render 'reviews/form' %> 
    </div> 

得到試圖呈現形式讓我去PlacesController當一個錯誤,我把下面的代碼行展示方法:

# GET /places/1 
    # GET /places/1.json 
    def show 
    @review = Review.new 
    end 

我的routes.rb文件已獲得評論的路由。我得到的錯誤是:

NoMethodError in Places#show 
Showing C:/workinrails/myyelpapp/app/views/reviews/_form.html.erb where line #5 raised: 
undefined method `content' for #<Review:0x9200a30> 
Trace of template inclusion: app/views/places/show.html.erb 

app/views/reviews/_form.html.erb:5:in `block in _app_views_reviews__form_html_erb___988880813_76383660' 
app/views/reviews/_form.html.erb:1:in `_app_views_reviews__form_html_erb___988880813_76383660' 
app/views/places/show.html.erb:13:in `_app_views_places_show_html_erb__530556826_71913768' 

我可以更好地瞭解

+0

你的名字在這裏有點混亂。頁面與地點。你可能同時擁有PagesController和PlacesController,並忘記在正確的PlacesController中添加顯示操作 – rohan

+0

我更正了關於該問題的信息,謝謝。我有一個PagesController和一個PlacesController。在我的PagesController上只有def home方法,裏面沒有任何東西。在我的PlacesController的show動作中,我有這個:@review = Review.new – samureira

+0

Review表中的列是什麼。 – rohan

回答

1

添加上的評論表稱爲內容的新列提供任何代碼片段

bundle exec rails g migration add_content_to_reviews content:string 

bundle exec rake db:migrate 
0

您需要添加一個指定的列名稱

rails g migration AddColumnNameToTableName column_name:datatype

然後

軌分貝:遷移

注: - 你應該知道的B/W的form_for &的form_tag,

的form_for

的區別

你在這裏使用的form_for,所以你應該只指定在已創建數據庫列名

的form_tag

的form_tag你不需要sapecify所需的列名,你可以使用任何名字,你會在你的控制器中得到這個值的參數