使用操作文本时,数据是否发送到错误的表?

时间:2019-09-09 06:24:51

标签: ruby-on-rails ruby ruby-on-rails-6 actiontext

使用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 %>

我希望headingsummary被存储在Story表中,而ActionText处理body。 以下是创建和保存故事时的happens。 如here所示,故事表中未保存任何内容。

1 个答案:

答案 0 :(得分:0)

您实际上应该使用story_params方法:

@story = Story.new(story_params)