Problem with displaying content when using RJS - ruby-on-rails

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.

Related

Resolve SoundCloud track in controller

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 %>

My Kaminari links are not working with Rails, unless I reload the browser

EDIT:
Basically it is because I am making the pagination links appear in a bootstrap class - col-xs-5 so everything appeared to the right of the page. When I commented it all out, the links worked fine but everything is in the center of the page now like normal non-Bootstrap content..what to do?
EDIT:
Is this a browser thing? I'm on a Macbook Pro OSX Yosemite, using the Safari browser.
Basically, if I select page 2, that URL is correctly shown in the browser URL field: localhost:3000/users/showtasksforuser?page=2, but the entries for page 2 are not rendered in place of page 1 unless I reload the page2 URL manually by pressing enter.
(As a sidenote, users/showtasksforuser works and users/showtasksforuser/?page=1 is not needed for the first page).
In users/showtasksforuser.html.erb:
<% #tasks.each do |task| %>
</br></br>
<div style="text-align:center"><%= task.name %></div>
<% end %>
<%= paginate #tasks, :remote => true %>
In my Users controller:
def showtasksforuser
#tasks = Task.page(params[:page]).per(1)
end
In my Task model:
paginates_per 1
is the first line after the model declaration.
The problem is that you have not created a showtasksforuser.js.erb file.
<div id="taskList">
<%= render "task_list"%>
</div>
<%= paginate #tasks, :remote => true %>
_task_list.html.erb
<% #tasks.each do |task| %>
</br></br>
<div style="text-align:center"><%= task.name %></div>
<% end %>
users_controller.rb
def showtasksforuser
#tasks = Task.page(params[:page]).per(1)
respond_to do |format|
format.js
format.html
end
end
showtasksforuser.js.erb
$("#taskList").html('<%= escape_javascript(render "task_list")%>')
I recommend changing the name of the action and the files to show_tasks_for_user
I have a similar problem. In my case, this works perfectly well:
<%= paginate #tasks %>
I just remove the :remote => true from paginate, and this makes all the default buttons from Kaminari works.

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.

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

ruby-on-rails: rendering partials on each item in a list

I have the following in a view (.html.erb) :
<% #posts = GetAllPostsFunctions %> (removed for berivity)
<% #posts.each do |post| %>
<%= post.title %>
<%= render :partial => "posts/post_show" %>
<% end %>
the posts_show partial has the following:
....
<td><%=h #post.title %> </td>
But I get the following error
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.title
Any ideas?
You can also simply things by using the :collection for render :partial. Which pass each item in the value for :collection to a local variable sharing the name of your partial.
<% #posts = GetAllPostsFunctions %> (removed for berivity)
<%= render :partial => "posts/post_show", :collection => #posts %>
In this case, Rails will render post_show for each item in #posts with the local variable post_show set to the current item. It also provides handy counter methods.
Successfully using this approach would require renaming the app/views/posts/_post_show.html.erb partial to app/views/posts/_post.html.erb to or changing every occurance of post in your partial to post_show. If you renamed the partial to the conventional _post.html.erb which will then allow you to simply do:
<%= render :partial => #posts %>
Which will render the partial for every single post in the #posts variable.
Since the post variable in the each loop is a locale variable you have to make it available to the partial:
<%= render :partial => "posts/post_show", :locals => {:post => post} %>
You can then access the title through the local variable post:
<td><%=h post.title %> </td>
You could also simplify the whole thing by rendering the posts as collection. Take a look at the Rails documentation for more information:
http://api.rubyonrails.org/classes/ActionController/Base.html#M000658
It doesn't look like you're setting the #post of the partial, so when it goes to evaluate the partial it gets a null reference.
Alternately, ensure that your post fetching functions are actually returning something
I'm not positive, but I think in the partial you have to do post.title not #post.title
Sorry if I misunderstood you, I'm new to rails.

Resources