我正在遵循一个教程,我必须制作一种目录网站。由于需要有一个用户可以撰写评论的页面,我会为评论创建视图和表单。我所做的步骤是:
1 - 在app / views / reviews:
创建了一个评论文件夹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,并在show方法中放了以下代码:
# 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 :(得分:1)
在评论表
上添加一个名为content的新列bundle exec rails g migration add_content_to_reviews content:string
bundle exec rake db:migrate
答案 1 :(得分:0)
您需要在表格中添加指定的列名称
rails g migration AddColumnNameToTableName column_name:datatype
然后
rails db:migrate
注意: - 强> 你应该知道b / w form_for&amp;的差异。的form_tag,
的form_for
您在此处使用form_for,因此您应仅在已创建数据库列名
时指定的form_tag
form_tag您不需要指定所需的列名,您可以使用任何名称,您将在控制器中获得该值的参数