Resolve SoundCloud track in controller - ruby-on-rails

First off, I'm pretty new to Ruby on Rails and the SoundCloud API so sorry if this is a stupid question.
My problem is, I'm trying to use the SoundCloud resolve track feature to get track information about a post.
Right now I have a scaffold set up that runs through each post using the
<% #posts.each do |post| %>
<%= post.soundcloud %>
<% end %>
.soundcloud is just the url of the song that I want to be resolved.
I've tried
<%= #client.get('/resolve', :url => <%= post.soundcloud %> ) %>
But then realized I can't have nested erb tags.
I've also tried passing various methods in the controller but I can't seem to figure it out.
Oh, here is what my controller looks like.
def index
require 'soundcloud'
#posts = Post.all
#client = Soundcloud.new(:client_id => 'MY_CLIENT_ID')
#tracks = #client.get('/resolve', :url => '') <-- Don't know what to do with this
end
Any suggestions?
Thanks in advance

I didnt check the documentation, but from looking at your code your erb file should be
<% #posts.each do |post| %>
<%= #client.get('/resolve', url: post.soundcloud ) %>
<% end %>

Related

link to last post - rails

Im trying to link_to the show action for a post.
In my controller I have:
#post = Post.all
In my view I'm trying to link it to the last post like this:
<%= link_to #post.last do %>
<%= #post.last.title %>
<% end %>
But it does not take me to the show page for that post?
Post.all loads all posts, but does not guarantee an order. You might want to use the id or the created_at value to order your list of posts:
# in your controller
#posts = Post.order(:id)
# in the view:
<%= link_to #posts.last.title, #posts.last %>
Or - if you don't need the other posts in the view - just load the lastest post:
# in the controller:
#latest_post = Post.order(:id).last
# in the view:
<%= link_to #latest_post.title, #latest_post %>
Try with below code,
<%= link_to post_path(#post.last) do %>
<%= #post.last.title %>
<% end %>
If this code not work then please find route with fire rake routes in your terminal and replace post_path with your routes
Hope this will work.

Rendering issue with <dc:creator> tag from Wordpress RSS2

I'm having issues with rendering the content of the XML tag from my Wordpress RSS feed. Here's my code to display the last 2 posts which I draw in from the controller:
<h3>Check out our latest blog posts:</h3>
<% unless #latest_blog_posts.nil? %>
<% #latest_blog_posts.each do |post| %>
<% if nil != post && post.respond_to?(:pubDate) %>
<h4><%= link_to post.title, post.link, :target => "_blank" %>
(by: <%= post.dc:creator %> - <%= time_ago_in_words(post.pubDate) %> ago)</h4>
<%= (post.description).slice!(0, 195).html_safe %>[...]
<% end %>
<% end %>
<% else %>
<p>Woops, looks like there's no posts to show. Sorry about that.</p>
<% end %>
The tag gives an error due to the ":" in the tag. I've tried using another variable and rendering the contains in a string:
article_author = '#{post.dc:creator}'
That renders "#{post.dc:creator}" in the view (I thought it would but I gave it a try anyway). Does anyone have a solution to this? Thanks.
Might as well answer my own question since it's the first result in a relative Google search. Hopefully it can help someone out in the future. The xml node was being parsed as:
dc_creator
<%= post.dc_creator %>
Thanks to everyone that checked out my question and tried to help.

Render in rails a view

Sorry for a simple question but i am a bit confused trying to follow ruby on rails tutorial book
I am at the chapter 10 and confused, yes i trick a bit my version for learning purpose
So I have a controller called customer
customers_controller.rb
def show
#customer = Customer.find(params[:id])
#posts = #customer.posts
end
I then have the following folder
_post.html.erb
Welcome to a post
Which his called from the show customer file has follow
/view/customer/show.html.erb
<% provide(:title, #customer.name) %>
<aside class="customer_show_nav">
<h1><%= #customer.name %></h1>
<%= #customer.email %>
</aside>
<div class="events">
<%= render #posts %>
</div>
But when loading nothing his appearing not even Welcome to a post. What i am doing wrong?
Thanks in advance. I am following the tutorial http://ruby.railstutorial.org/chapters/user-microposts#top 10.22
if _post.html.erb is in view/customers
you do this in view/customers/show.html.erb
<%= render 'controller_where_post_lives/post' %> which will look for customers/_post.html.erb
Sometimes, you also need = in <%= %> with rails 3
Also, show is used to show one item, and index is used to show all items.
So to render you will do
<%= render :partial => "post", :collection => #posts %>
Edit:
When you call render you give it the view, no objects here. Unless like above passing a collection.
http://guides.rubyonrails.org/layouts_and_rendering.html
Also if you just want to render text you can do render :text => "OK"
Look for partials explanation.

Problem with displaying content when using RJS

I'm quite sure this is a silly error but I'm unable to spot it. Please help me out on this.
I'm using a select tag to select a topic, which triggers the filter_by_content method. I'm using Rails 2.3.2.
This is my controller code
def filter_by_content
#articles = Article.find(:all)
end
My RJS (filter_by_content.rjs)
update_page do |page|
page.replace_html 'articles', :partial => 'main/filtered', :object => #articles
end
My Partial 'filtered'
<div id = "articles">
<% if #articles %>
<% #articles.each do |article| %>
<%= article.title %>
<% end %>
<% end %>
</div>
I checked my server, and the articles are sure getting fetched but the problem is with displaying them.
Ok turns out, if I remove the reference to JQuery and use only prototype, it works fine.
So I had to install JRails and now it works fine.

remote_form_for doesn't call rjs template

So here is my form
<% remote_form_for([:admin, Page.new]) do |f| %>
<ol>
<li>
<%= f.label(:title) %>
<%= f.text_field(:title) %>
</li>
<li>
<%= f.label(:parent_page) %>
<%= f.select(:parent_page_id, Page.roots.map { |p| [p.title, p.id] }.unshift(["none", nil])) %>
</li>
</ol>
<div class="modal-controls">
<%= submit_tag("Save") %> or <%= link_to_function("cancel", "R.Pages.hideAdd();") %>
</div>
<% end %>
And my action
def create
#page = Page.create(params[:page])
#languages = Language.all
#languages.each do |language|
#page.page_languages.create(:language_id => language.id)
end
For some reason the submitted for does not call the create.js.rjs template, but instead tries to call create.html.erb, do i need some sort of extra setting with the form?
btw i am using rails 2.3.5
I can't remember the exact default behavior in rails, but have you tried putting a respond_to at the end of your controller action:
respond_to(:html, :js)
Hope this helps.
EDIT
I went back to check on the default behavior for Rails in respect to rendering views. Rails favors convention over configuration in this instance. The default behavior is that Rails automatically renders views with names that correspond to actions. You don't need the respond_to any more if you stick to this convention. Here is the documentation.
Just wanted to update my post with the correct info... glad you figured your problem out.
I had named my template create.rjs.js instead of create.js.rjs, thats why i didnt work

Resources