Embed youtube video in rails - ruby-on-rails

I'm trying to embed video in pages. I tried two different ways but no luck
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #video.name %>
</p>
<p>
<b>Url:</b>
<%= #video.url %>
</p>
<p>
<% #video_tag( #video.url , :size => "560x315", :controls => true, :autobuffer => true ) %>
<%= youtube_video #video.url%>
</p>
<%= link_to 'Edit', edit_video_path(#video) %> |
<%= link_to 'Back', videos_path %>
The video does not show neither does the links below it.
Any tips would be appreciated. Thank you
this is the debug result
ActionView::MissingTemplate in Videos#show
Showing
/Users/atbyrd/Documents/sites/city/app/views/videos/show.html.erb
where line #15 raised:
Missing partial shared/video with {:handlers=>[:erb, :builder,
:coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: *
"/Users/atbyrd/Documents/sites/city/app/views" *
"/Users/atbyrd/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.1/app/views"
Extracted source (around line #15):
12: 13: 14: <% #video_tag( #video.url , :size => "560x315",
:controls => true, :autobuffer => true ) %> 15: <%= youtube_video
#video.url%> 16: 17: 18: <%= link_to 'Edit',
edit_video_path(#video) %> |
Rails.root: /Users/atbyrd/Documents/sites/city Application Trace |
Framework Trace | Full Trace
app/helpers/application_helper.rb:4:in youtube_video'
app/views/videos/show.html.erb:15:in
_app_views_videos_show_html_erb__1532956397246631491_70281299458880'
app/controllers/videos_controller.rb:18:in `show'

you have forgotten to close a bracket
<%= video_tag( #video.url %>
should be
<%= video_tag( #video.url ) %>
UPDATE:
Try using video_path instead of video_tag.
2nd UPDATE:
here's how I do this myself on my own site:
1 - create a partial called _video.html.erb (I actually use haml, but erb will do if you prefer it) and put it in a folder like views/shared or something ad put the following code in it:
<iframe width="490" height="275" src="<%= url %>" frameborder="0" allowfullscreen></iframe>
2 add the following method to application_helper.rb
module ApplicationHelper
# this method will embed the code from the partial
def youtube_video(url)
render :partial => 'shared/video', :locals => { :url => url }
end
end
3 - now just call this in your views with:
<%= youtube_video #video.url %>
This works ok for me

I got it, you forgot to use erb syntax. Try:
<iframe width="560" height="315" src= "<%= #{#video.url} %>" frameborder="0" allowfullscreen></iframe>

#miaout17 is right, you can't use string interpolation in HTML, so you have to wrap it in Erb (e.g. <%= %>).
Also you're missing an ending ')' in your links below which is probably why they aren't working.

Related

Two Similar Partials, One Giving Error, Other Is Fine

I have two partials, that are almost exactly the same, except one is a full view, and the other is a compact. They are both under 'projects' view, but one works correctly, and the other gives errors:
projects/_project.html.erb
<div class="pure-u-1-3">
<%= link_to project do %>
<div class="project">
<h4 class="red marginless"><%= truncate( project.title, length: 22, separator: ' ') %></h4>
<p class="left marginless"><strong>By:</strong> <%= project.user.username %><br>
<strong>Genres:</strong> <%= truncate( project.genre2, length: 25, separator: ' ') %><br><br>
<strong>Description:</strong><br>
<%= truncate( project.description, length: 60, separator: ' ') %><br>
<strong>Needs:</strong><br>
<%= truncate( project.looking_for, length: 60, separator: ' ') %></p>
</div>
<% end %>
<% if #projects.size == 0 %>
<em> no projects found with that criteria </em>
<% end %>
</div>
projects/_short.html.erb
<div class="pure-u-1-3">
<%= link_to project do %>
<div class="project">
<h4 class="red marginless"><%= truncate( project.title, length: 22, separator: ' ') %></h4>
<p class="left marginless"><strong>By:</strong> <%= project.user.username %></p>
</div>
<% end %>
</div>
Now, this is the part of the view using these partials:
dashboards/index.html.erb
<% if #projects.length > 0 %>
<div class="pure-u-1" id="projects"><h3 class="red"><%= current_user.username %>'s Owned Projects</h3>
<%= render :partial => 'projects/short', :collection => #projects %>
</div>
<% end %>
When I have 'projects/project' it works perfectly, no issues.
When I put 'projects/short' it gives me this error:
undefined local variable or method `project'
This error is given everywhere the word project is used on _short, unless I use #project, but then it gives me this error:
undefined method `title' for nil:NilClass (same for user)
I do not understand how the partials can be in the exact same spot, and used the exact same way, in the exact same spot, but one works and the other does not. Is there code I may have done in the past, that I am missing that makes this work?
You need to supply the as: :project option to the render partial call as:
<% if #projects.length > 0 %>
<div class="pure-u-1" id="projects"><h3 class="red"><%= current_user.username %>'s Owned Projects</h3>
<%= render :partial => 'projects/short', :collection => #projects, as: :project %>
</div>
<% end %>
This will make project local variable available within the partial projects/short.
The reason you are getting the error is because (from the documentation on rendering collections within Using render):
When a partial is called with a pluralized collection, then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial.
Okay, so before I actually saw the above answer, I was still sorting through documentation, and from my best understanding, I actually just changed everything that said project to compact now like:
<h4 class="red marginless"><%= truncate( compact.title, length: 22, separator: ' ') %></h4>
<p class="left marginless"><strong>By:</strong> <%= compact.user.username %></p>
Is this the 'wrong' way to do it? It seems to be working perfectly now...

Rails: 'Load More' button with Ajax & Kaminari

I would like to create a "load more" ajax pagination, with Kaminari.
I'm using this code :
class BienvenueController < ApplicationController
def index
#articles = Admin::Article.page(1).per(2)
respond_to do |format|
format.html
format.js
end
end
end
# Bienvenue#index
<div class="container" style="min-width:<%= #width %>px">
<%= render "shared/articles" %>
<%= link_to_next_page #articles, 'Load More', :remote => true, :id=>"load_more_link" %>
# Shared/articles
<% #articles.each do |a| %>
<article class="<%= a.rubrique.color %>">
<div class="sharing">
<%= image_tag "facebook-32.png" %>
</div>
<p class="color<%= a.rubrique.color %>"><i>Le <%= I18n.localize(a.created_at, :format => :long) %> par David Perrotin</i></p>
<h1><%= a.titre %></h1>
<div class="excerpt">
<%= a.chapo.html_safe %>
</div>
<div class="image">
<%= image_tag a.mainphoto.url(:medium), :width=>"100%" %>
</div>
<div class="contenu">
<%= a.contenu.html_safe %>
</div>
<div class="readmore">
<%= link_to "Continuer la lecture", article_path(a) %>
</div>
</article>
<% end %>
# index.js.erb
$('.container').append("<%= escape_javascript(render 'shared/articles')%>");
$('#load_more_link').replaceWith("<%= escape_javascript(link_to_next_page(#articles, 'Load More', :remote => true, :id=>'load_more_link'))%>");
But the problem is that when I click on "Load More", it always shows the two same articles, the partial is never refreshed with two more articles, like I would like.
I just ran into an issue with this that might help others. Depending on your version of jQuery, don't use replaceWith on the #load_more_link in index.js.erb.
There is a regression (http://bugs.jquery.com/ticket/13401) that an empty replaceWith does nothing, so on the very last page of your set, the link_to_next will be empty, making the line: $('#load_more_link').replaceWith(''); and thus will not replace the last "more" button, so you'll continually load the last page of your data set.
Fixed by updating jQuery version or use empty().html('...') instead of replaceWith.

Trying to put rel and class inside rails link_to

I'm trying to put in a rel and class attribute inside a anchor tag but it keeps going into the img tag. I'm fairly new to Rails so I'm hoping can see what i'm doing wrong.
<% #series.uploads.each do |upload| %>
<%= link_to image_tag upload.upload.url(:thumb), :class => 'lightbox', :rel => 'lightbox'%>
<% end %>
Here is the output.
<a href="/project-gallery">
<img alt="alt" class="lightbox" rel="lightbox" src="img-path" />
</a>
This?
<%= link_to(image_tag(upload.upload.url(:thumb)), :class => 'lightbox', :rel => 'lightbox') %>
This is what I ended up doing to get it to work but it just doesn't look right?
<% #series.uploads.each do |upload| %>
<%= link_to(image_tag(upload.upload.url(:thumb)), '', {:rel => "lightbox", :class => "lightbox"})%>
<% end %>

Rails 3 tagging problems, acts_as_taggable_on

I'm using acts_as_taggable_on to add tags to posts, other tagging plugins/gems don't work with rails 3. I can edit/display tags on the post model and the tags controller displays the posts tagged by name i.e /tags/post-tag-name/.
The functionality I want is to turn the tags on the posts pages into links to display the other posts with the same tag.
I followed the tutorial in sitepoints 'simply rails 2' which uses acts_as_taggable_on_steroids but I'm stuck with the following error;
ActionView::MissingTemplate in Posts#show
Missing partial acts_as_taggable_on/tags/tag with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "../app/views"
Extracted source (around line #28):
25: <div id="tags">
26: <% unless #post.tag_list.empty? %>
27: <p class="tags">
28: <%= render :partial => #post.tags %></p>
29: <% end %>
...
class Post < ActiveRecord::Base
...
acts_as_taggable_on :tags
end
class TagsController < ApplicationController
def show
#post = Post.tagged_with(params[:id])
end
end
_tag.html.erb
<%= link_to, tag_path(:id => tag.name) %>
posts/show.html.erb
<div id="tags">
<% unless #post.tag_list.empty? %>
<p class="tags">
<%= render :partial => #post.tags %></p>
<% end %>
</div>
Also trying to add a tag cloud at tags/index.html as described here http://github.com/mbleigh/acts-as-taggable-on gives me a routing error of;
No route matches {:action=>"tag", :id=>"news", :controller=>"tags"}
Looks like you want to use :collection, which will render the whole list with the template:
<div id="tags">
<% unless #post.tag_list.empty? %>
<p class="tags">
<%= render :partial => 'tag', :collection => #post.tags %>
</p>
<% end %>
</div>

How do I wrap link_to around some html ruby code?

How do I wrap a link around view code? I can't figure out how to pass multiple lines with ruby code to a single link_to method. The result I am looking for is that you click the column and get the show page:
<div class="subcolumns">
<div class="c25l">
<div class="subcl">
<%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil %>
</div>
</div>
<div class="c75r">
<div class="subcr">
<p><%= album.created_at %></p>
<%= link_to h(album.title), album %>
<p><%= album.created_at %></p>
<p><%= album.photo_count %></p>
</div>
</div>
</div>
link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag.
So, you do
<%= link_to(#album) do %>
html-code-here
<% end %>
But I'm quite sure that to nest a div inside a a tag is not valid HTML.
EDIT: Added = character per Amin Ariana's comment below.
Also, this may be an issue for some:
Make sure to write <%= if you are doing a simple link with code in it instead of <%.
e.g.
<%= link_to 'some_controller_name/some_get_request' do %>
Hello World
<% end %>
For older Rails versions, you can use
<% content_tag(:a, :href => foo_path) do %>
<span>Foo</span>
<% end %>
You can use link_to with a block:
<% link_to(#album) do %>
<!-- insert html etc here -->
<% end %>
A bit of a lag on this reply I know -- but I was directed here today, and didn't find a good answer. The following should work:
<% link_to raw(html here), #album %>

Resources