使用 Ruby on Rails 6,在用户保存他们的故事后(使用动作文本键入他们的故事),他们将被重定向到show
可以阅读故事的视图。但是,什么都没有出现,并且在检查数据库时,似乎什么都没有保存。
在浏览了文档并观看了一些视频后,我body
从表中删除了该列Story
以由 ActionText 存储。
该模型:
class Story < ApplicationRecord
# declare that body has a rich text field
has_rich_text :body
end
控制器:
def new
@story = Story.new
end
def create
@story = Story.new(params[:story_params])
if @story.save
redirect_to @story
else
render 'new'
end
end
def show
@story = Story.find(params[:id])
end
private
def story_params
params.require(:story).permit(:heading, :summary, :body)
end
来自 new.html.erb 的表格
<%= form_with(model: @story) do |form| %>
<%= form.label :heading %> <br>
<%= form.text_area :heading %>
<br>
<%= form.label :summary %> <br>
<%= form.text_area :summary %>
<br>
<%= form.label :body %>
<%= form.rich_text_area :body %>
<br>
<%= form.submit %>
<% end %>
我希望heading
andsummary
存储在 Story 表中,而 ActionText 处理body
. 以下是创建和保存故事时发生的情况。如此处所示,Story 表中没有保存任何内容。