will_paginate current page is always 1 - html

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)

Related

Select options with new lines issue

I have the following rails code (it uses simple form):
<%= f.input :color, collection: custom_colors.split(/\n/).reject(&:empty?) %>
custom_colors comes from a text area where users are asked to enter entries seperated by new lines.
The issue: This displays correctly, however, when a user goes to edit, the color is not saved and the select is empty. I'm using a typical rails scaffold.
The issue (continued): The reason I believe this is not working, is that when saved, the color looks like blue\r\n. However, when I do:
<%= field.options.split(/\n/).reject(&:empty?) %> it returns blue\r, which means the input won't match, as blue\r\n clearly does not equal blue\r
How do I fix this?
Try this in your form.
<%= f.input :color, collection: custom_colors.split(/\n/).map(&:strip).reject(&:empty?) %>

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.

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.

Rails div not rendering?

I have a simple app I'm following from a textbook. It's a store app and on the oft side of the page a shopping cart is shown. However, when the cart is empty it is supposed to be hidden. So I wrote a helper function to hide the cart whenever it is empty. This would have been east enough just to do in the html but it's how the book shows it. It works mostly. If I empty the cart it stops being displayed, but when I first open the page the cart will be invisible even if its not empty! Adding an item to it will then reveal it.
Here's the code:
application.html.erb
<div id="cart">
<% if #cart %>
<% hidden_div_if( #cart.line_items.empty?, id:"cart" ) do %>
<%= render #cart %>
<% end %>
<%end%>
</div>
application_helper.rb
module ApplicationHelper
def hidden_div_if(condition, attributes = {}, &block)
debugger
if(condition)
attributes["style"] = "display: none"
end
content_tag("div", attributes, &block)
end
end
Where I put the debugger statement, I checked the condition and it says it's false. So, I thin it's an issue with the html file. The controller sets the cart before hand with this code at the top.
include CurrentCart
before_action :set_cart
So, I'm not sure why it would show after a button is pressed but not on the initial page load. Any ideas?
thanks
You need to start with <%= in erb if you want to output the return value of a helper method. Change the line in which you call your helper method to:
<%= hidden_div_if(#cart.line_items.empty?, id: "cart") do %>

will_paginate rendering correct html but not displaying links

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?