将更改保存到Rails应用程序中的tinymce内联编辑器吗?

时间:2018-09-03 15:07:10

标签: ruby-on-rails tinymce

我正在尝试在Rails应用程序中保存在嵌入式tinymce编辑器中所做的更新,但是我不知道如何完全按照自己的意愿进行。我希望视图为“ html_safe”时可以保存内联编辑器。如果表单区域是文本区域(即<div class="editor"><%= form.text_area :content %></div>),我可以保存,但是我想以内联编辑器格式显示视图,以便您可以即时查看所做的更改。

在日志中,邀请参数似乎已关闭,其中未包含所需的更改。我该如何做才能正确保存内联编辑器更改?

注意:除<%= @ invitation.content%>的更改未保存之外,其他所有操作均正常工作。

查看:

    <%= form_with(model: @invitation, local: true) do |form| %>

    <%= form.hidden_field :event_id, value: @event.id %>

    <div class="editor"><%= @invitation.content.html_safe %></div>
    <script type="text/javascript">
          tinyMCE.init({
            selector: '.editor',
            menubar: false,
            inline: true,
            plugins: "save",
            toolbar: "save"
          });
        </script>

<% end %>

我在日志中的输出是:

  Processing by InvitationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2ssewH/CSN7is0P2LVrMkEmwZXcgmQqjWTjgrE6gGqAEv4dmLXFjEuOFJ9TRia+JF59SB6kKu5Le1xvt/FoxPg==", "invitation"=>{"event_id"=>"29"}, "mce_0"=>"<h1>Second Test Event CHANGEHERE!</h1>\r\n<h2>09/02/18 @ 04:20</h2>\r\n<p>&nbsp;</p>\r\n<hr />\r\n<p>Bring Cheese</p>", "commit"=>"Update Invitation", "id"=>"7"}
  Invitation Load (2.6ms)  SELECT  "invitations".* FROM "invitations" WHERE "invitations"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
  ↳ app/controllers/invitations_controller.rb:66
   (0.3ms)  BEGIN
  ↳ app/controllers/invitations_controller.rb:43
  Event Load (1.8ms)  SELECT  "events".* FROM "events" WHERE "events"."id" = $1 LIMIT $2  [["id", 29], ["LIMIT", 1]]
  ↳ app/controllers/invitations_controller.rb:43
   (0.3ms)  COMMIT
  ↳ app/controllers/invitations_controller.rb:43
Redirected to http://localhost:3000/events/29

我的控制器是一个基本的对象控制器:

class InvitationsController < ApplicationController
  before_action :set_invitation, only: [:show, :edit, :update, :destroy]

  # GET /invitations
  # GET /invitations.json
  def index
    @invitations = Invitation.all
  end

  # GET /invitations/1
  # GET /invitations/1.json
  def show
  end

  # GET /invitations/new
  def new
    @invitation = Invitation.new
  end

  # GET /invitations/1/edit
  def edit
  end

  # POST /invitations
  # POST /invitations.json
  def create
    @invitation = Invitation.new(invitation_params)
    respond_to do |format|
      if @invitation.save
        format.html { redirect_to @invitation, notice: 'Invitation was successfully created.' }
        format.json { render :show, status: :created, location: @invitation }
      else
        format.html { render :new }
        format.json { render json: @invitation.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /invitations/1
  # PATCH/PUT /invitations/1.json
  def update
    respond_to do |format|
      if @invitation.update(invitation_params)
        format.html { redirect_to @invitation.event, notice: 'Invitation was successfully updated.' }
        format.json { render :show, status: :ok, location: @invitation }
      else
        format.html { render :edit }
        format.json { render json: @invitation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /invitations/1
  # DELETE /invitations/1.json
  def destroy
    @invitation.destroy
    respond_to do |format|
      format.html { redirect_to invitations_url, notice: 'Invitation was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_invitation
      @invitation = Invitation.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def invitation_params
      params.require(:invitation).permit(:name, :content, :event_id)
    end
end

0 个答案:

没有答案