Rails: Ransack and will_paginate (undefined method 'total_pages' error) - ruby-on-rails

I'm attempting to use will_paginate for a search with Ransack for pagination, and I'm getting the following error: undefined method `total_pages' for #
I'm not sure what it is that I'm doing incorrectly here and have tried a number of possible solutions with no luck. Is it possible to use these together and what am I doing incorrectly here? Thanks.
coin_controller.rb
class CoinsController < ApplicationController
load_and_authorize_resource param_method: :question_params
before_action :find_coin, only: [:edit, :update, :destroy ]
before_action :authenticate_user!, except: [:index, :create, :show]
def index
#search = Coin.ransack(params[:q])
#coins = #search.result.paginate(page: params[:page], per_page: params[:per_page])
end
.
.
.
end
index.html.erb
<% provide(:title, "Coins") %>
<% #coins = #coins.sort_by &:currency_name %>
<%= will_paginate #coins %>
<div class="row">
<div class="col-md-8" id="pagination_table">
<% #coins.each do |coin| %>
<% if coin.accepted %>
<ul>
<%= image_tag coin.picture.thumb.url if coin.picture? %>
<%= link_to coin.currency_name, :action => 'show', :id => coin %>
</ul>
<% end %>
<% end %>
<% if can? :edit, Coin %>
<hr><b>Pending Approval</b><hr>
<% #coins.each do |coin| %>
<% if not coin.accepted %>
<ul>
<%= image_tag coin.picture.thumb.url if coin.picture? %>
<%= link_to coin.currency_name, :action => 'show', :id => coin %>
</ul>
<% end %>
<% end %>
<% end %>
</div>
<div class="col-md-3" id="side-bar">
<%= link_to "Submit a New Coin", new_coin_path, class: "btn btn-default" %>
<fieldset class="search-field">
<legend>Search All Coins</legend>
<%= search_form_for #search do |f| %>
<div class="field">
<%= f.label :currency_name_cont, "Search by Name" %>
<%= f.text_field :currency_name_cont %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<div class="field">
<%= f.label "Search by Genre" %><br />
<%= f.collection_check_boxes :genres_id_in_any, Genre.all, :id, :displayname do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
<% end %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
</fieldset>
</div>
</div>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
</script>
I also tried using Kaminari and got an identical error.

Try this solution...
Instead of this line
<%= will_paginate #coins %>
To
<%= will_paginate #coins if #coins.present? %>

Related

Missing Parameters in Ruby on Rails

The Seller Profile has one seller. I am trying to post the seller profile details and assigning the seller_id to the current_seller. I am however, running into this error. I don't understand why the error says missing parameter because it seems all the needed params have being provided.
Error is get is ActionController::ParameterMissing (param is missing or the value is empty: seller_profiles
def create
#seller_profile = SellerProfile.new(seller_profile_params)
#seller_profile.seller = current_seller
respond_to do |format|
if #seller_profile.save
format.html { redirect_to root_path, notice: 'Seller profile was successfully created.' }
def seller_profile_params
params.require(:seller_profile).permit(:first_name, :other_name, :last_name, :email)
end
<%= form_tag seller_seller_profiles_path do |form| %>
<% if seller_profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(seller_profile.errors.count, "error") %> prohibited this seller_profile from being saved:</h2>
<ul>
<% seller_profile.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label_tag :first_name %>
<%= text_field_tag :first_name %>
</div>
<div class="field">
<%= label_tag :other_name %>
<%= text_field_tag :other_name %>
</div>
<div class="field">
<%= label_tag :last_name %>
<%= text_field_tag :last_name %>
</div>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email %>
</div>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
resources :sellers, only: [:new, :create, :show, :index, :destroy] do
resources :seller_profiles
end
You should use the form_for or the form_with helpers instead of form_tag. Those helper methods will take care of adding the wrapping seller_profile key.
<%= form_for seller_profile do |form| %>
<% if seller_profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(seller_profile.errors.count, "error") %> prohibited this seller_profile from being saved:</h2>
<ul>
<% seller_profile.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :first_name %>
<%= form.text_field :first_name %>
</div>
<div class="field">
<%= form.label :other_name %>
<%= form.text_field :other_name %>
</div>
... replicate the same change for the rest of the fields ...
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If the error is (param is missing or the value is empty: seller_profiles, it's because you require :seller_profile, not :seller_profiles in your params.require

Submit button does not work unless I refresh the page form_for Rails

I have an app that has a problem model and when I go to create a record the submit button does nothing. No errors given it just simply doesnt execute, unless I refresh the page and attempt add it again. The same happens when I go to update a record.
Here is my controller
class ProblemsController < ApplicationController
include Concerns::Votes
def index
#problems = Problem.all
end
def show
#problem = find_problem
end
def new
#problem = Problem.new
end
def edit
#problem = find_problem
end
def create
#problem = current_user.problems.new(problem_params)
#problem.save
redirect_to #problem
end
def update
#problem = find_problem
if #problem.update_attributes(problem_params)
redirect_to #problem
else
redirect_to #problem
end
end
private
def find_problem
#problem = Problem.find(params[:id])
end
def problem_params
params.require(:problem).permit(:name, :description, :url)
end
end
Here is my _form.html.erb partial that I am rendering on new.html
<div class="row">
<div class="large-12 columns">
<%= form_for #problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
</div>
<div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
</div>
<div class="large-12 columns">
<%= f.text_area :description %>
</div>
<div class="large-12 columns">
<%= f.submit "Create" %>
</div>
</div>
<% end %>
I have resources :problems in my routes.
Here for good measure is my show.html.erb as well.
<%= div_for #problem do %>
<%= link_to 'Edit', edit_problem_path(#problem) %>
<h2><%= #problem.name %> (<%= #problem.cached_votes_score %>)</h2>
<a =href"<%= #problem.url %>"><%= #problem.url %></a>
<p><%= #problem.description %><p>
By <%= #problem.user.name %></br>
<a class="button"<%= link_to 'Up', {:controller => 'problems', :action => 'up_vote'}, {:method => :post } %></a>
<a class="button"<%= link_to 'Down', {:controller => 'problems', :action => 'down_vote'}, {:method => :post } %></a>
<%= link_to 'Edit', edit_problem_path(#problem) %> |
<%= link_to 'Back', problem_path %>
<% end %>
Here is my index.html.erb
<div class="row">
<div class="large-12 columns">
<% #problems.each do |problem| %>
<h1><small><%= problem.cached_votes_score %></small> <%= link_to problem.name, problem %></h1>
<% end %>
</div>
<%= link_to 'New Problem', new_problem_path %>
</div>
I really cant understand why it works if i refresh the page but otherwise it doesnt work at all.
Your HTML is invalid, the submit button is actually not nested under the form tag. Try changing your view code to this:
<div class="row">
<div class="large-12 columns">
<%= form_for #problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
<div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
</div>
<div class="large-12 columns">
<%= f.text_area :description %>
</div>
<div class="large-12 columns">
<%= f.submit "Create" %>
</div>
<% end %>
</div>
</div>
I had same issue
Before
<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.button :submit, class: 'pull-right' %>
<%end%>
</div>
</div>
After
<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.button :submit, class: 'pull-right' %>
</div>
</div>
<%end%>

Create a link from a div - Ruby on Rails

Currently I have a text link within the div that links correctly <%= link_to 'Show', profile %>. I have read that the below is the proper way to turn a div into a link
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
Though when I apply that to my block it gives me a undefined local variable or method 'profile'
<%= link_to profile do %>
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to 'Show', profile %>
</div>
</div>
<% end %>
</div>
<% end %>
My original block without the attempt at linking:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to 'Show', profile %>
</div>
</div>
<% end %>
</div>
My Controller:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy, :id]
# GET /profiles
# GET /profiles.json
def index
#profiles = Profile.all
end
# GET /profiles/1
# GET /profiles/1.json
def show
end
# GET /profiles/new
def new
#profile = Profile.new
end
#...
Your first example is problematic in many ways - you have links inside links. I'm not exactly sure what you're looking to output from this - but basically you need the
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
behavior to replace where you have <%= link_to 'Show', profile %>
Something along the lines of:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to profile do %>
<div>
fancy pants stuff here
</div>
<% end %>
</div>
</div>
<% end %>
</div>
If you can give me some guidance on what you want in the div I can post you some code to achieve that, I'm just not clear on what you are looking for exactly.
Another example could be (if you wanted more of the pieces to fall inside that <div></div>:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= link_to profile do %>
<div>
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
... more fancy pants stuff here ...
</div>
<% end %>
</div>
</div>
<% end %>
</div>

NoMethodError in Pages#contact

I got this error I Created pages model and for pages controller i created a contact, thank you and _form page. but I getting the error "NoMethodError in Pages#contact"
`Showing /home/rohit/Desktop/inertiiaproject/inertiiatwo/app/views/pages/_form.html.erb where line #1 raised:
undefined method model_name' for NilClass:Class
pages_controller.rb file
class PagesController < ApplicationController
def new
#pages = pages.new
end
def create
#pages = pages.new(params[:pages])
if #pages.deliver
render :thank_you
else
render :new
end
end
end
contact.html.erb
<h1>Want to get in touch?</h1>
<p>Please fill out the form below and we'll get back to you as soon as possible.</p>
<%= render 'form', :pages => #pages %>
_form.html.erb
<%= form_for pages do |f| %>
<% if pages.errors.any? %>
<ul>
<% pages.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div style="display: none;">
<%= f.label :nickname %><br/>
<%= f.text_field :nickname %>
</div>
<div>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div>
<%= f.label :message %><br/>
<%= f.text_area :message %>
</div>
<div>
<%= f.submit "Send" %>
</div>
<% end %>

In which controller should I place elements that persist throughout the application?

Everything after #posts should persist throughout the application (Like the left sidebar you find on Facebook).
def index
#title = "Posts"
default_order = "content_changed_at DESC"
params[:order_by] ||= default_order
#posts = current_user.subscribed_posts.paginate(:page => params[:page],
:per_page => 5,
:order => params[:order_by])
#subscribed_tags = current_user.subscribed_tags
#recent_posts = current_user.posts.limit(5).order("created_at DESC")
#tags= Tag.limit(20).order("ID asc")
#user = current_user
end
views/layout/_sidebar.html.erb:
<div class="user-profile">
<% avatar = image_tag(current_user.avatar.url(:thumb), :class => "authenticated-avatar") %>
<%= link_to avatar, "/users/#{current_user.id}" %>
<%= link_to "#{current_user.username}", "/users/#{current_user.id}", :class => "authenticated-username" %>
</div>
<%= form_for(#user, :remote => true) do |f| %>
<div class="field">
<%= f.label :subscribed_tag_names %><br />
<%= f.autocomplete_field :subscribed_tag_names, autocomplete_tag_name_posts_path, :"data-delimiter" => ' ', :class => "autocomplete_field" %>
</div>
<div class="actions">
<%= f.submit :class => "user_subscribed_tag_names_submit" %>
</div>
<% end %>
<div class="user-subscribed_tags">
<% #subscribed_tags.each do |subscribed_tag| %>
<%= link_to "#{subscribed_tag.name}(#{subscribed_tag.posts.count})", unsubscribe_tags_path(:unsubscribed_tag_name => subscribed_tag.name) %>
<% end %>
</div>
<div class="user-recent-posts">
<h4>Recent Posts</h4>
<ul>
<% #recent_posts.each do |recent_post| %>
<li><%= link_to recent_post.title, recent_post %></li>
<% end %>
</ul>
</div>
<div class="top-tags">
<% #tags.each do |tag| %>
<span class="tag-name"><%= tag.name %></span>
<span class="tag-count"><%= tag.posts.count %></span>
<% end %>
</div>
<% end %>
Where should I place the code in the controller if I want them to persist throughout the application? (I would like to see some example code if possible).
If it persists throughout the application, i.e. for every actions of all controllers, you might place it in a before_filter in the application controller.
Add a before_filter to the application controller as :
before_filter :find_recent_posts_and_tags
And define it (as private) :
private
def find_recent_posts_and_tags
# define your instance variables
end
More about filters here and here.

Resources