Pagination of a partial with Kaminari in Rails 3 - ruby-on-rails-3

Here is another Newbie question on Rails 3: I have a model called "Activity". On the show.html.erb page of an activity, I show the comments that belong to this activity as a partial:
<%= render :partial => #activity.comments %>
How the comments look like is in the _comment.html.erb file.
My question: How can I paginate the comments for an activity with Kaminari?
Thanks very much in advance!

Lets assume params[:page] is the page number for your comments. You would do something like #activity.comments.page(params[:page]). I suggest you read https://github.com/amatsuda/kaminari, the README contains pretty much everything you need.

Related

gmaps4rails map display

I am new to Ruby on Rails and am trying to use the gmaps4rails gem. The longitude and latitude show up automatically but the map is not appearing. If I look at the page source info it seems like the map is being called and created so I'm not sure why it's not showing up.
This can be seen here:
<script src="//maps.google.com/maps/api/js?v=3.8&sensor=false&client=& amp;key=& amp;libraries=geometry&language=&hl=&region=" type="text/javascript"></script>
<script src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js" type="text/javascript"></script>
<script type="text/javascript">
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.auto_adjust = true;
Gmaps.map.initialize();
Gmaps.map.markers = [{"lat":40.8217303,"lng":-73.9551369}, {"lat":41.4925374,"lng":-99.9018131},{"lat":50.3429668,"lng":18.5540869}];
Gmaps.map.markers_conf.do_clustering = true;
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
Do I need to include some other files in my document to show the map? And where would those documents be? I am using Ruby 1.9.3 and Rails 3.2.12.
I see this type of question has been asked and answered many times, but I haven't been able to get a solution from those answers. Thanks for any help.
UPDATE:
Here is my view:
<br />
<%= link_to 'New Bathroom', new_bathroom_path %>
<%= gmaps4rails(#json) %>
Model:
acts_as_gmappable
def gmaps4rails_address
address
Controller:
def index
#bathrooms = Bathroom.all
#json = Bathroom.all.to_gmaps4rails
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #bathrooms }
end
end
and application.html
<%= yield %>
<%= yield :scripts %>
<%= stylesheet_link_tag 'gmaps4rails' %>
</body>
</html>
One thing I'm wondering is if I need to copy some assets to the app. It says on the github site.
"""""""""""""""""""""""""""""""""""""""""
gmaps4rails.css will be copied to your app after you run the Rails generator. Be sure to require this file in your view for your first steps.
For Rails 3.0.x or without assets pipeline:
<%= stylesheet_link_tag 'gmaps4rails' %>
"""""""""""""""""""""""""""""""""""""""""""
Since I don't see the gmaps4rails.css file I wonder if that's the problem. Thanks for helping.
Post Debugger Details:
Yeah that's true. I have almost no experience with this stuff. I got Firebug as you recommended and discovered that when I glide over the page the google scripts in the debugger never get highlighted. I see that it did try to make a call to googleapis.com But the time for the different actions are zero, except for blocking and waiting. This makes me think nothing was sent from the google server. Under Get gmaps4rails.css it looks in my app/asset folder and has a 404 error. I'm learning how to understand all this info, and if I'm correct it seems like the server request to google was blocked for some reason and it thinks my css file is in the asset folder. If I am correct I really have no idea where to go from here. Please tell me what info would help make this problem clearer.
You need to run 'rails generate gmaps4rails:install'. That will supply the missing bits (and the css will be sourced automatically in rails 3.2).
After that, you should be good to go (based on the original question, and barring any subsequent changes).

recaptcha_tags rendering escaped HTML in erb

I'm migrating my app to Rails 3.2.8 from 2.3.5. One form uses reCAPTCHA (in an erb file). Right now, all the HTML tags that the recaptcha_tags puts out are in escaped HTML. (i.e. <...> instead of <...>). So I see the tags themselves in the HTML page, instead of the reCAPTCHA box.
Here's what my erb looks like. I've verified that it doesn't matter where in the erb I put the recaptcha tags (inside the form_for or outside):
register.html.erb
...
<%= recaptcha_tags :public_key => RECAPTCHA_PUBLIC_KEY %>
...
Other things, like form_for don't have this problem. They output straight HTML.
Gemfile
gem "recaptcha", :require => 'recaptcha/rails'
Thanks in advance!
Turns out I needed to prefix the recaptcha_tags call with "raw":
register.html.erb
...
<%= raw recaptcha_tags :public_key => RECAPTCHA_PUBLIC_KEY %>
...
One of the major changes between Rails 2.3.5 and 3.X was some changes having to do with cross site scripting. You may want to start your upgrade by upgrading to 2.3.14 and adding the rails_xss gem. The rails_xss gem (https://github.com/rails/rails_xss) will switch the HTML safety default to escape, so it will cause the issue you're seeing with recaptcha_tags. This will allow you to see what strings in your app need to be marked as html_safe.
Or, if recaptcha_tags is the only issue, you may be able to fix it by telling rails not to escape the recaptcha_tags.
Something like:
<%= raw recaptcha_tags :public_key => "_________" %>
Thanks for doing the research. :)

how to post :create from the default update url, rails 3

I'm sure this is pretty basic, but I'm somewhat new to rails and struggling to find a solution via search.
I'm implementing a message model to enable private messaging on a forum. I have the models resource nested within a users resource.
Currently the model works, but I want to enable a user to reply to a private message directly on the message show page. I.e users/1/messages/16 instead of users/1/messages/new. Currently this is the default route for 'update' within the MessagesController. Is there anyway to make the form on this page hit the 'create' action within the controller, instead of the 'update'?
Thanks.
Sure, I would try something like this:
On your show page just add a new form.
<%= form_for :message, :url => new_user_message_path do |f| %>
...
<% end %>
You can check the routes of your application using this command:
bundle exec rake routes
I suggest you to read the rails guide: http://guides.rubyonrails.org/

Google suggest and auto complete with jquery/ajax in Rails app

I would like to get some ref to use Jquery in Rails app.
At the moment, I am stuck to find the things to enable google suggest in text box and autocomplete in Rails app. Any help/suggestions/ref would be of great help.
I did a short search on auto-complete with Rails earlier today and these are the best links I found:
http://github.com/crowdint/rails3-jquery-autocomplete and http://github.com/crowdint/rails3-jquery-autocomplete-app
http://blog.jackhq.com/posts/3-how-to-add-jquery-autocomplete-to-your-rails-forms-in-a-restful-way
http://code.google.com/p/jquery-autocomplete/
Edit: I just implemented the first link in my app, works flawless so far. I had minor problems with the documentation though, this works for me (on typing AutoComplete starts after the second character):
Model named Category, has an attribute name:
controller:
class UsersController < ApplicationController
autocomplete :category, :name
...
routes:
get 'users/autocomplete_category_name'
view:
<%= form_tag 'users/index' do %>
<%= autocomplete_field_tag 'address', '', users_autocomplete_category_name_path, :size => 75 %>
<% end %>

Rails login partial in different controller

Hey,
I'm pretty new to rails and for learning effect, I try to implement my own authorization system.
Right now I'm having a Page Controller to control some static pages and nothing more, and a Session Controller where I plan to implement most of the authorization process.
My problem is, I have no clue how to get my partial to use the sessions-controller, when I add it to one of the static pages controlled by the pages controller.
It stated out with this http://ruby.railstutorial.org/chapters/sign-in-sign-out#top but i don't want it on an extra page.
so I tried setting the routes and I got an exception "no path found for '/'" as soon as I deleted "resources :sessions" it worked fine again.
my partial looks like this:
<%= form_for(User.new) do |f| %>
<%= f.submit "Login" %>
<% end %>
there's also a div class="action" block around the submit but can't find out how to escape it
this is included into my home via
<%= render 'sessions/new' %>
Thanks for your help
edit my solution:
I added to routes.rb:
resources :sessions
Furthermore I changed form_for(#user) to
<%= form_for(:session, url => sessions_path)
so this works.
I Highly recommed that you look at the railscast http://railscasts.com/episodes/250-authentication-from-scratch , it will give you an idea how to create authentication without forgetting some important steps.
Then you can use the gem devise which is an excellent authentication gem.
Have you tried putting your functions and everything for authentication within a Session Helpers file? Then, in your Application Controller if you add "include SessionsHelper" this should give you access to all the helper functions from Session that you should need