如何保存ActionMailbox inbound HTML email和关于ActionText与ActiveStorage的附加
gi代码:
新建一个rails6
需要
gem 'image_processing'
gem 'whenever', require: false
rails action_text:install
(参考https://www.cnblogs.com/chentianwei/p/10515183.html ,
https://www.cnblogs.com/chentianwei/p/10655122.html )
rails webpacker:install:stimulus
yarn add tributejs
#app/javascript/packs/application.js #... +require("trix") +require("@rails/actiontext")
rails g scaffold Post title:string
rails db:migrate后
#app/models/post.rb # 这里给post记录挂上关联的actiontext和attachment。 class Post < ApplicationRecord has_rich_text :body has_many_attached :attachments end
rails action_mailbox:install
所有入境的邮件都让posts处理。
#app/mailboxes/application_mailbox.rb class ApplicationMailbox < ActionMailbox::Base # routing /something/i => :somewhere routing :all => :posts end
rails g mailbox Posts
..
app/views/posts/_form.html.erb 修改代码,添加richtext,并根据post是否存在,添加删除和返回按钮。
<div class="field"> <%= form.label :title %> <%= form.text_field :title, class: 'form-control' %> </div> <div class="form-group"> <%= form.label :body %> <%= form.rich_text_area :body, class: 'form-control' %> </div> <div class="form-group"> <% if post.persisted? %> <div class="float-right"> <%= link_to "Destroy", post, method: :delete, class:'text-danger', data: {confirm: "Are you sure?"}%> </div> <% end %> <%= form.submit class: 'btn btn-primary'%> <% if post.persisted? %> <%= link_to "Cancel", post, class: "btn btn-link"%> <% else %> <%= link_to "Cancel", posts_path, class: "btn btn-link"%> <% end %> </div> <% end %>
app/views/posts/index.html.erb 修改代码:
+ <td><%= post.body %></td>
app/views/posts/show.html.erb 修改代码:
<div class="page-header"> <%= link_to posts_path, class: 'btn btn-default' do %> All Posts <% end %> <%= link_to edit_post_path(@post), class: 'btn btn-primary' do %> Edit <% end %> <h1>Show post</h1> </div> <dl class="dl-horizontal"> <dt>Title:</dt> <dd><%= @post.title %></dd> <dt>Body:</dt> <dd><%= @post.body %></dd> </dl> <% @post.attachments.each do |attachment| %> <div><%= link_to attachment.filename, attachment %></div> <% end %>
配置: