1
我有一個非常簡單的Rails應用程序。基本上有評論的文章。我想讓文章頁面顯示文章,下面的評論,然後是文本框來撰寫評論和提交按鈕來提交它。表單在頁面加載時提交
除了一個(大)問題以外,我知道了所有的工作。當頁面加載.. example.com/article/1 空白評論提交給數據庫。
我修正了在評論模型中包含「validates_presence_of:body」的問題。但是,這會導致下面的圖像中加載網頁時:
這是我的代碼的方式:
def show
@place = Article.find(params[:id])
@comment = Article.find(params[:id]).comments.create(params[:comment])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @article }
end
end
和
<% form_for([@article, @comment]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>