Rails 3 No Method Error undefined method 'current_page' - ruby-on-rails-3

Rails -v 3.2.3
im using kaminari to do pagination but i keep getting a NO METHOD ERROR, undefined method 'current_page' for nil:NilClass
in views/layouts/application.html.erb where line #7 raised:
<%= paginate #links, :remote => true %>
is this happening because i have it on the application view instead of my link submission view? does it matter where you place the paginate link?
in /controllers/links_controllers.rb (in a method called submissions)
#links = Link.page(params[:page]).per(20)

I figured it out... basically I had to move my <%= paginate #links %> code to my submissions.html.erb page (which is a part of my links views) I then ran bundle to make sure will_paginate was gone from my gemsfile and voilĂ , it worked.

Related

Middleman sitemap.where doesn't exist

I'm trying to automatically generate a list of links to pages that have certain frontmatter in them, but every time I try to use sitemap.where(), I get a NoMethodError. For example, the following line:
<%= sitemap.where(:title=>"about") %>
produces this output:
NoMethodError at /
undefined method `where' for #<Middleman::Sitemap::Store:0x007f9b95c7d890>
Ruby layouts/layout.erb: in block in singleton class, line 20
Web GET localhost/
I was wondering if I accidentally messed something up in my project, so I generated a new Middleman project, but I had the same problem when I tried to use sitemap.where. Is there a solution to this or another way that I can query all of the pages?
The where method is part of ActiveRecord and might not work in Middleman.
To get only those pages in the sitemap which have a particular property, you can use Ruby's select:
<% sitemap.resources.select{|p| p.data.title == 'about'}.each do |page| %>
<%= page.url %>
<% end %>
This code will print a (very basic) list of the URLs of pages that match your criteria.

parameters are not passing to the controller Rails3 + coffeescript + ajax

I'm trying to implement a sorting list with Rails3 and coffeescript. I had a routing issue earlier and was able to solve it with your generous help, so now I have a different issue, My data parameter is not passing to the server,
Following is my view
#index.html.erb
<h1>Listing books</h1>
<ul id="books"> <% #books.each do |book| %>
<li class="book<%= book.id %>"><span class="handle">[drag]</span><%= book.name %></li>
<% end %></ul>
<%= link_to 'New book', new_book_path %>
following is my books.js.coffee file
jQuery ->
$('#books').sortable
axis: 'y'
handle: '.handle'
update: ->
$.post('/books/sort', $(this).data('#books'), $(this).sortable('serialize'))
Following is my controller
#books_controller.rb
def sort
#books = Book.all
#books.each do |book|
book.position = params['book'].index(book.id.to_s) + 1
book.save
end
render :nothing => true
end
this is the error I'm getting
Served asset /jquery.js - 304 Not Modified (0ms)
Started POST "/books/sort" for 127.0.0.1 at 2012-11-28 17:03:25 +0530
Processing by BooksController#sort as */*
Book Load (0.2ms) SELECT "books".* FROM "books"
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `index' for nil:NilClass):
app/controllers/books_controller.rb:14:in `block in sort'
app/controllers/books_controller.rb:13:in `each'
app/controllers/books_controller.rb:13:in `sort'
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (40.8ms)
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (47.7ms)
So when i say p params, I get {"action"=>"sort", "controller"=>"books"}, So it doesnt have a param called 'book' and hence gives a nil error, can someone help me out,
any help will be greatly appreciated
thanks in advance
You have a couple things going wrong here. First of all, your $.post arguments are a bit confused:
$.post('/books/sort', $(this).data('#books'), $(this).sortable('serialize'))
$.post wants the data in the second argument so just this:
$.post('/books/sort', $(this).sortable('serialize'))
makes more sense.
Sortable's serialize method wants DOM id attributes to be in a specific form:
It works by default by looking at the id of each item in the format "setname_number", and it spits out a hash like "setname[]=number&setname[]=number".
In your Rails controller, you want an array of book IDs in params[:book] so you want book[]=id&book[]=id&... going to your server. That means that you want your <li>s to look more like this:
<li id="book_<%= book.id %>">
Or you could keep use class attributes by including an underscore:
<li class="book_<%= book.id %>">
and then telling serialize to look at class:
$(this).sortable('serialize', attribute: 'class')
You should also use the expression option and leave the HTML alone:
$(this).sortable('serialize', attribute: class, expression: /(book)(.+)/)
There are a few other ways to arrange things, see the sortable documentation for further details.

Correct way to share a view in the index page

I'm a Ruby-on-Rails newbie, just starting out.
I have an MVC called "account_types", generated via scaffold to produce:
controllers/account_types_controller.rb
helpers/account_types_helper.rb
models/account_type.rb
views/account_types/_form, edit, index etc...
Going to localhost:3000/account_types gives me the index view.
What I'd like to do is display the same data as selected from the account_types index method in the application index page as a list.
I wrote a new view called account_types/_list.html_erb as follows:
<ul>
<% #account_types.each do |account| %>
<li><% account.label %></li>
<% end %>
</ul>
I then edited home/index.html.erb (This is based on examples given in other questions on SO):
<%= render :partial => 'account_types/list', :module_types => #module_types %>
However I get
undefined method `each' for nil:NilClass
and the error displays the code from account_types/_list.html.erb where I've written
<% #account_types.each do |account| %>
The scaffolded views work fine, why aren't mine?
Are partials the right thing to use here?
Thanks in advance.
What is the correct way to define an application-wide partial and its variables in rails says to use a before_filter on ApplicationController.
You pass :module_types to partial, but use account_types. As I can see you just need to change your index.html.erb to:
<%= render :partial => 'account_types/list', :account_types => #module_types %>
You can use partials for this if you want, though it would be unnecessary in this case as far as I can tell (they are for sharing chunks of code between several views). In order to get this code to work you'll need to define #account_types in your controller with something like
#account_types = AccountType.all
You can see exact line in your account_types_controller.rb under index action. :module_types => #module_types is not necessary here, since I doubt you defined #module_types either and you don't use module_types in your partial at all.
It's obvious, that you don't understand how Rails works, so I suggest reading through a good tutorial (like this one) before you proceed with whatever you have in mind.

Memberships engine not working properly

I've installed RefineryCMS and a couple of its engines (like Blog). Everything was working fine until I installed Memberships engine.
After struggling a couple of days, I could make it "work". By "work" I mean that I could create a user, but since I have it installed, each time I access the home page I get the following error:
undefined method `refinery_user?'
Extracted source (around line #1):
1: <% if refinery_user? %>
2: <% unless admin? # all required JS included by backend. %>
3: <% content_for :stylesheets, stylesheet_link_tag('refinery/site_bar') unless !!local_assigns[:exclude_css] %>
4: <%= yield(:stylesheets) unless local_assigns[:head] or local_assigns[:exclude_css] %>
I've "ctrl+click" on that method and it does exist!! It has the following code:
def refinery_user?
user_signed_in? && current_user.has_role?(:refinery)
end
The weird thing is that I've put a breakpoint on that line but the app didn't stop there...
Does anybody know what's going on?
Make sure your /config/initializers/devise.rb file exists and that it includes the following (probably at the bottom):
config.router_name = :refinery

Using render(:action => "action") in a js.erb file

I try to assign to result of rendering an action to an element on page. That should be an easy task but the following statement:
$(".box:first").after("<%= escape_javascript(render :action => "new" ) %>");
will result in the following error message:
ActionView::Template::Error (undefined method `formats' for nil:NilClass):
1: $(".box:first").after("<%= escape_javascript(render(:action => "new") ) %>");
The same code works if i try to render a partial using :partial => "new" (given that the partial with the given name exists)
The error message will be the same if I change ':action' f.e. to ':foobar'.
Any ideas what i am doing wrong or is this simple a bug in rails? (3.0.7)
It is a bug in your application. Actually it is a bug in your new action in controller
Result of rendering of an action? That's probably not the very best idea. As you said, the code works when you use partial, and that's what you should use.
This is from official Rails guides:
Using render with :action is a
frequent source of confusion for Rails
newcomers. The specified action is
used to determine which view to
render, but Rails does not run any of
the code for that action in the
controller. Any instance variables
that you require in the view must be
set up in the current action before
calling render.
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render