will_paginate giving blank pages - ruby-on-rails

I'm use will_paginate gem in my Rails application.
controller code:
def index
#posts = Guestbook.order('id DESC').page(params[:page]).per_page(10)
end
view code (used foundation gem):
<%= foundation_paginate #posts %>
Everything works fine, but if you type in the address bar room for a page, a blank page is displayed (no error messages or redirect to the first page). Tell me how to fix it, please.
P.S. I'm sorry for my bad english

you have to set will_paginate to use an array.
In config/initializers/
create a new file called will_paginate_extensions.rb
and add this:
require 'will_paginate/array'
I also would recommend not to use that foundation gem you are using for will_paginate, It can't receive parameters. I ran into that problem with that gem so i made one that can receive parameters
https://github.com/acrogenesis/will_paginate-foundation

Related

Mailboxer: modifying how inbox renders - rails

I'm using a gem called mailboxer to allow users to send messages between each other.
In my controller:
def mailbox
#mailbox ||= current_user.mailbox
end
In my view I have:
<%= render mailbox.inbox %>
Which renders each conversation subject under a bullet list with a link to trash the conversation. How do I style this or modify how it renders? I can't seem to find the code anywhere that let's me modify how this renders.
mailbox.inbox is just an array of conversation objects. The magic here comes from the way that Rails automatically renders a partial for each object in a collection when an array is passed to render. You can modify how the collection is rendered by creating your own partial that does whatever you want to do with that array of conversations. This describes it further:
https://github.com/RKushnir/mailboxer-app/issues/2
And this part of the Rails guide describes this behavior (might help when you are figuring out how to customize - see section 3.4.5):
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

RESTful API in rails

I am very new to rails and following a tutorial for RESTful API so let me excuse if it is of not very good quality as I am equally a starter for these kind of terminologies as well.
I created a controller kitten with a command rails g controller kitten index
and in the index method I posted this code -
class KittenController < ApplicationController
def index
require 'open-uri'
kittens = open('http://placekitten.com/')
response_status = kittens.status
response_body = kittens.read[559, 441]
puts response_status
puts response_body
end
end
and un commented match ':controller(/:action(/:id))(.:format)' in routes.rb
When i navigate through this - http://localhost:3000/kitten
this is what i am getting in my browser -
Kitten#index
Find me in app/views/kitten/index.html.erb
and this in my command line -->
Now my question why it so although i am expecting it in my browser but the cat is shown in command prompt instead of browser ..i am new to rest resource so please excuse if it is a bad one :(
I don't know what tutorial you're following, but doing this seems like a very odd thing to do for Rails in general and learning RESTful APIs in particular.
Anyway, the puts in your controller outputs text to Ruby's standard out, which is going to be the terminal where the server started. That's why this is appearing in the console rather than in your browser: puts is putting it there.
If you want this to appear in a web page, you'll need to make a view for that controller action. Perhaps following further along your tutorial will get you there: if not, you might want to find a better one.
You should read the Model-View-Controller rails guide.
Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
Define your variable in the controller and display it in the view:
class KittenController < ApplicationController
def index
#variable = 'Hello World'
end
end
In your view (app/views/kitten/index.html.erb):
<%= #variable %>
Rails controllers setup responses with a render call.
When the call is not performed it instantiates the appropriate view and renders that view. In your case that is index.html.erb
Try this:
render :text => kittens.read[559, 441], :status => kittens.status

kaminari - redirect to last page

So I've got pagination with Kaminiari set up on my app, which is a forum. When someone replies to the thread, I want to direct them to the last page of the thread. It seems like it'd be easy enough to hard code the logic to get the last page based on what I'm using for record numbers, but is there a built in method to get the last page?
In my current version of kaminari (v0.12.4) the following works:
users = User.page(params[:page])
last_page = users.num_pages
num_pages is defined in https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/page_scope_methods.rb.
If you want to add a last_page method, I suggest the following:
module Kaminari
module PageScopeMethods
def last_page
num_pages
end
end
end
It seems that this thread has an answer.
You can write a method to achieve this if not already present in Kaminari . This should be present since Kaminari also renders the page numbers for navigation.
Say, #records is list of db records where you performed #records.page(1) to show the get the current set of records,
The last page number is defined by (#records.total_count / per_page.to_f).ceil .
For other people's sake, I'll share what worked for me.
Associations
Conversation has_many Messages
Message belongs_to Conversation
In Conversation show page, I want to paginate every 10 messages
messages = conversation.messages.page(params[:page]).per(10)
last_page = messages.total_pages
Then I want to create a link to this show page which will show me the last page. I just made a helper method
def create_link_to_last_page(conversation)
content_tag :div do
link_to("Show", url_for(controller: 'conversations', action: 'show', id: conversation.id, page: last_page))
end
end

Need help integrating youtube_it into my rails app

I am pretty new to rails so this may be an easy question, but I was looking to create a Rails app that would use youtube in it. I have found that youtube_it seems to be the gem of choice for a task like this but I am having trouble using it. Basically, I want to use the gem so that I can get videos from specific users, i.e. Stanford University and create a list of those videos with links to another page that has the video information and player. To test this out I tried the follow code:
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :yt_client
private
def yt_client
#yt_client ||= YouTubeIt::Client.new(:dev_key => dev_key)
end
end
home_controller.rb
class HomeController < ApplicationController
def index
#playlists = yt_client.playlists('stanforduniversity')
end
end
index.html.erb
<h3>Home</h3>
<% #playlists.each do |playlist| %>
<p>Playlist: <%= playlist %></p>
<% end %>
The problem with this is all I get for output on my home page is a list of something like this: #
My questions then are: is there a way to change this output into an actual title? Am I doing something wrong/forgetting a step? Or should I just use python code to use the Google API and put all the videos into my database(I already have some code for this) and just access it using my rails app?
Hope this is clear enough.
it looks like what you want to print out is the name of the playlist - but that is an attribute of the playlist object, so you'll need something like:
<% #playlists.each do |playlist| %>
<p>Playlist: <%= playlist.title %></p>
<% end %>
otherwise ruby is trying to "print" the playlist object - which just doesn't work the way you expect.
Note: I've not used this gem either, I'm gathering this info from the gem docs here: https://github.com/kylejginavan/youtube_it
https://github.com/kylejginavan/youtube_it or youtube_it is the new version of youtube_g. This was forked and enhanced. If you need any enhancements please reach out to me.
you have a complete demo that I made here
http://youtube-it.heroku.com/
included source!

Rails jQuery with Ajax?

I'm a rails newbie and I cant seem to get jquery with ajax to work. I have installed the jquery-rails and done rails g jquery:install. Then I did a sample test by calling an alert in the application.js file and it worked. Then I created a controller action 'index' and made a link to it on one of my other pages. I thought to get ajax to work with it, I would simply create a index.js.erb file. However I then got an error saying template missing, so in my controllers' index action, I put the following code:
respond_to do |format|
format.html
format.js
end
However, I am not getting anything displayed on my page. In my index.js.erb file I have a simple alert message to test out if its working, and I cannot get that to even come up. Any suggestions? I don't know if it matters but I am using rails 3.0.7. Thanks in advance
RJS files didn't work with jQuery Plugin, so, you have to use *.js.erb extension in order to use javascript template, and these files are a kind of erb + javascript combination (like *html.erb files).
Just wrote this using rails 3.0.7, jquery plugin should be compatible with rjs syntax
routes.rb
get "main/index"
get "main/hello"
main_controller.br
class MainController < ApplicationController
def index
end
def hello
end
end
hello.js.rjs
page.alert("test");
index.html.erb
<%= link_to 'test', main_hello_path, :remote => true %>

Resources