0

だから、私はユーザーがYouTubeのリンクを送信し、親指の宝石を使って曲/リンクをランク付けできるレールアプリを持っています。

ただし、リンクがアップロードされた後に曲を聴くには、リンクを物理的にコピーしてブラウザーに貼り付ける必要があります。UX を向上させるために、ユーザーがリンクをアップロードしたら、この gemを使用して YouTube ビデオをアプリに動的に埋め込みます。

しかし、どうすればこれができるのかよくわかりませんか?

私はインデックスでこのようなことをすることを考えていました:

 <%= YouTubeAddy.extract_video_id('song.url)' %> <%= YouTubeAddy.youtube_embed_url('song.url') %> ? 

または、コントローラーにある必要がありますか?もしそうなら、どのように?

song#new.html.erb:

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

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

    <div class="row">
        <div class="large-6 columns">
    <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
    </div>

    <div class="field">
    <%= f.label 'website url' %>
    <%= f.text_area :url %>
    </div>
    <div class="field">
      <%= f.label :tag_list, "Genres (separated by commas)" %><br />
      <%= f.text_field :tag_list %>
    </div>
    <p>
    <%= f.file_field :track%>
    </p>

    <div class="actions">
        <%= f.submit value: "Upload" %>
    </div>

    <% end %>
        </div>

song_controller.rb

class SongsController < ApplicationController
  before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
  before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]

  def vote_for
      @song = Song.find(params[:id])
      current_user.vote_for(@song)
      @song.plusminus = @song.votes_for
      @song.save
      respond_to do |format|
        format.js { render 'update_votes' }
      end
  end

  def vote_against
    @song = Song.find(params[:id])
    current_user.vote_against(@song)
    respond_to do |format|
      format.js { render 'update_votes' }
    end
  end

  def new_songs
    @songs = Song.order "id DESC"
  end


  # GET /Songs
  # GET /Songs.json
  def index
    if params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
    else
      @songs = Song.order('plusminus').paginate(:page => params[:page], :per_page => 15)
    end
  end

  # GET /Songs/1
  # GET /Songs/1.json
  def show
   @comment = Comment.new(song: @song) 
  end

  # GET /Songs/new
  def new
    @song = Song.new
  end

  # GET /Songs/1/edit
  def edit
  end

  # POST /Songs
  # POST /Songs.json
  def create
    @song = Song.new(song_params)

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

  # PATCH/PUT /Songs/1
  # PATCH/PUT /Songs/1.json
  def update
    respond_to do |format|
      if @song.update(song_params)
        format.html { redirect_to @song, notice: 'Song was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end

  # Song /Songs/1
  # Song /Songs/1.json
  def destroy
    @song.destroy
    respond_to do |format|
      format.html { redirect_to songs_url }
      format.json { head :no_content }
    end
  end

  private

    def set_song
       @song = Song.find(params[:id])
     end

     def song_params
       params.require(:song).permit(:title, :artist, :url, :track, :user_id, :tag_list)
     end
  end

index.html.erb

<h6>Top ranked songs</h6>
<hr>
<ol><% @songs.each do |song| %>
<span class="title">
<li><%=link_to image_tag('thumbs.png', size: '16x50'), vote_for_song_path(song), :remote => true, :method => :put %> <%= link_to song.title, song %><span class="subtext"> (<%= song.url %>)<br></li></span>


 <%#=link_to '&#9660'.html_safe, vote_against_song_path(song), :remote => true, :method => :put %> 

<span class="subtext"><span class="votes_<%= song.id %>"><%= pluralize(song.votes.count, 'like') %>,</span>

    posted <%= time_ago_in_words(song.created_at) + " ago" %>
    <small><span class="comments"></small> | <%= pluralize(song.comments.size, 'comment') %></span></small><br /></span>


<%#= link_to 'Show', song, class: "button small secondary" %>
<%= link_to('Edit', edit_song_path(song), class: "button small secondary") if can? :update, song %>
<%= link_to('Destroy', song, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary") if can? :destroy, song %>

<% end %>

</ol>
<div class="pagination-centered">
  <ul class="pagination">
<%#= will_paginate @songs %>
<!-- or custom pagination -->
<% if @songs.previous_page %>
  <%= link_to "Back", params.merge(page: @songs.previous_page) %>
<% end %>
<% if @songs.next_page %>
  <%= link_to "More", params.merge(page: @songs.next_page) %>
<% end %>
</ul></div>
4

1 に答える 1

0

YouTubeAddy.youtube_embed_url('song.url')は iframe タグの文字列を生成するため、埋め込みビデオを配置したいビューで正確に使用する必要があります。ただし、html セーフ文字列を返す必要があることに注意してください。したがって、あなたの見解では次のようになります。

<%=raw YouTubeAddy.youtube_embed_url('song.url') %>

htmlのエスケープ解除の詳細。

于 2013-09-03T19:05:52.823 に答える