will_paginate rendering correct html but not displaying links - html

I am creating a web app in Ruby on Rails which pulls images from instagram and puts them into an array.
I have a initializer/config/will_paginate_array.rb with the line:
require "will_paginate/array"
My controller looks like this:
def index
result = []
while result.length < 100
//my logic here which adds a bunch of images to the array
result.concat(data)
end
#results = result.paginate(:page => params[:page], :per_page => 13)
end
I am using this line in my html to render the links, but the links are not showing:
<%= will_paginate #results %>
To be clear, when I inspect element the markup is there(and all the pagination links are fully functional), it just isn't rendering/showing up on the page properly for some reason, and I am fairly sure it is not my css overriding anything. The pagination div generated by will_paginate has a height and width of 0. If I manually enter the next page for example localhost:3000?=2 it still advances the page correctly.
Perhaps it is some sort of compatibility issue with will_paginate and arrays?

Related

Editing comments on show.html.erb rather than redirecting to new edit.html.erb page with Ruby on Rails

Like the title says, I am created the blog application from the Rails guide and I want to be able to edit comments made on articles without redirecting to an edit.html.erb page. I want a div that was previously hidden to appear in the middle of the screen with the edit form on it and the text fields filled with the comment that is being edited. Here is an image of the div that is going to appear in the middle of the screen.
I know how to edit comments by going to edit.html.erb but not in the same page or if this is even possible.
I am not looking for help with the css or html, but if there is a way to do this with js or rails that would be awesome. Thanks in advance!
Edit: So the answer by Satendra gets the partial to show up, but the fields do not contain the original comment that I wanted to edit. How can I get the text-fields to be populated with the comment?
Follow these steps to make edit comment on same page.
Create a partial name _edit.html.erb put your edit.html.erb code there.
Create a div in middle of the screen where you want to render this partial.
<div id="edit_comment" >
</div>
put :remote => true to your edit button link.
# change your path accordingly
<%= link_to edit_comment_path(:comment_id => comment.id), :remote => true %>
The :remote => true is the most important part here, it allows the whole Ajax business in the first place.
In comment_controller.rb inside edit function change respond_to js
def edit
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
end
Create edit.js.erb and put below code there.
$("#edit_comment").html("<%= j render(:partial => 'edit', :locals =>{ :comment => #comment }) %>");
# use comment variable in your partial as it is passed as local.
Its Done.

will_paginate current page is always 1

Im using will_paginate and the problem Im having is when I click on a specific page number in the will_paginate selection. The page reloads with the page=2 if clicking 2 on the selection for example. However on the selection it is still selected as page 1. The selection never changes.
<%= will_paginate #reservations %>
I removed the css from the pagination and still having the same problem.
#reservations = user.reservations.order(updated_at: :desc).page(params[:page_pending]).per_page(10)
also tried:
#reservations = user.reservations.order(updated_at: :desc).paginate(page: params[:page_pending]).per_page(10)
Please change query in the Controller --> "index action" to this?
#reservations = user.reservations.order(updated_at: :desc).paginate(page: params[:page], per_page: 30)

How can I display the action of a database-linked form with sinatra?

I have been working on a project in Ruby's Sinatra with ActiveRecord and MySQL and have ran into an error: part of my site is adding a link to the database and I want to show all of the links on the next page but for some reason I can not seem to find out how to express this.
This is what I have so far:
get '/dashboard' do
#site = Site.find_by text: params[:text]
#sites= Site.all
#newsite = Site.new ({:text => :text})
#user = User.find_by_id session[:user_id]
erb :dashboard
end
This is what ERB page looks like:
<% #sites.each do |site| %>
<h4><%=site.text.to_s%></h4>
<% end %>
How would I solve this? Now my form works but it does not show anything after
Your ERB code references a #sites attribute which you don't set in the controller.
If you set #sites in your ruby code to be an array of the Site records you want to show, then you should see something.

Removing header rendering in specific rails .html.erb file

I'm rendering a header universally across my application in layouts/application.html.erb.
I'd like to make the header not appear in a specific foo.html.erb file.
What's the syntax for un-rendering a universal layout?
EDIT:
The controller for the layout is a devise controller, specifically Sessions Controller.
in your controller you can set the layout to false (or another layout), if false then you need all of the html,head,body tags in your view file
class BarController < ApplicationController
def foo
render :layout => false # render foo.html.erb with no layout
end
end
see section 2.2.11.2 from the rails guides: http://guides.rubyonrails.org/layouts_and_rendering.html
EDIT: including devise layout overrides
in config/initializers/devise.rb
Devise::SessionsController.layout "bar"
Devise::RegistrationsController.layout "foo"
Devise::ConfirmationsController.layout false # never tried this, guessing it would work
Devise::UnlocksController.layout "bar"
Devise::PasswordsController.layout "foo"
also see the wiki post - https://github.com/plataformatec/devise/wiki/How-To:-Create-custom-layouts has at least one other way
Let's say the controller and action that renders the template foo.html.erb is 'things#foo' and the path to this action is things_path. You can wrap the header in conditional tags as follows
<% unless request.path == things_path %>
<% end %>
. There are several ways to achieve this, but here is one.

Rendering a partial in an iframe in rails 3

Rookie coder here... I am currently building an ecommerce site in rails. I have brands and price lists on the left hand side of the results pages, these lists are often longer than the page itself, I would therefore like to put them into scroll boxes like here
I have tried putting then into iframes, using partials, but it keeps producing different errors each time, and seems more fiddly than it should be. I have only really been able to find relevant information from dated blogs etc. The code I have tried is variations of this...
View:
<iframe src="<%= url_for :action => 'brands' %>" scrollbars="auto" name="brands"></iframe>
Controller:
def brands
render :partial => 'brands', :layout => false
end
Partial:
<ul>
<% #brands.each do |prod| %>
<li><%= link_to(strip_tags(prod[0]).html_safe + " " + "(#{prod[1]})", params.merge(:brand => "#{prod[0]}")) %></li>
<% end %>
</ul>
This seems to throw up a "Couldn't find Product with id=brand" error.
I also read that iframes may be a little old fashioned, so I am fully open to alternatives. Any help would be much appreciated!
No need to use iframes for this. You should just be rendering the partial in the view itself. And if you want scroll bars you should use CSS to make the height/width fixed and then set the overflow to scroll. Here is a link to help with that