2

私はRailsを学ぼうとしていますが、そのための言い訳としてブログを使用しています。

今、私はポストの足場で遊んでいます。私はMVCとその背後にあるアイデアを理解しているので、次のエラーに遭遇したときに、MVCを再作成しようとしていました。

次のようなコンテンツを入力すると

text
text
text

投稿フォームの「content」タグでは、すべてのテキストが1つのブロックとして表示されます。

text text text

こんなことやってみようかなと思った

<p>text</p>
<p>text</p>
<p>text</p>

しかし、それは示しています

<p>text</p><p>text</p><p>text</p>

Railsに実行してもらいたいのは、コンテンツ内のhtmlを実際に解析することです。それを実現するにはどうすればよいですか?

これが、コンテンツの送信に使用した新しいフォームパーシャルです。

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

これが投稿コントローラー全体です

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.paginate(page: params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end
4

2 に答える 2

2

XSS攻撃を防ぐために、Railsはデフォルトでhtmlをエスケープします。HTMLをエスケープしたくない場合は、エスケープ.html_safeしたくない文字列を使用する必要があります。でshow.html.erb

<%= @post.content.html_safe %>

<p>または、コンテンツフィールドに'sを入力せずsimple_format、次のように段落への書式設定を行うために使用するのがより良い方法です。

<%= simple_format(@post.content) %>

もちろん、両方を組み合わせて使用​​することもできます。たとえば、段落タグを省略たが、コンテンツにリンクがある場合:

<%= simple_format(@post.content.html_safe) %>

自分で入力したコンテンツには安全に使用できますが.html_safe、サイトがXSS攻撃にさらされる可能性があるため、サードパーティによって入力されたコンテンツ(コメントなど)には使用しないでください。

于 2012-11-08T03:39:32.227 に答える
0

ミシャの答えは私を検索のうさぎの穴に導きました、そして私はこの宝石も見つけました-https ://github.com/spohlenz/tinymce-rails

wysiwigエディターであるTinyMCEをテキストエリアに追加します

于 2012-11-08T03:58:18.390 に答える