使用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
和summary
被存储在Story表中,而ActionText处理body
。
以下是创建和保存故事时的happens。
如here所示,故事表中未保存任何内容。
答案 0 :(得分:0)
您实际上应该使用story_params
方法:
@story = Story.new(story_params)