parameters are not passing to the controller Rails3 + coffeescript + ajax - ruby-on-rails-3

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.

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.

Rails ERB -- get the generated HTML content to a variable I can access in a controller

I am doing an online contract agreement which is generated dynamically based on the current state of our contract language (stored in an ERB partial).
When the user clicks "I Agree" we save the variable data, but I also want to store the contents of the rendered partial (html fragment) in the database.
I tried using ActionView::Helpers::CaptureHelper.capture -- wrapping the whole partial in a block and rendering at the end, like
<% #saved_output = capture do %>
...
The rest of the erb with instance variable <%= #my_class.name %> and so on
...
<% end %>
<%= #saved_output %>
<% logger.debug "Rendered output is: #{#saved_output}" %>
And this produced the same result and also sent the correct text to the log. But it appears to go out of scope -- even if I declare #saved_output = nil prior to render it's nil when I end the block.
I tried using content_for as well ... which makes some sense to me, but ... huh, just not getting it. Any helpful pointers appreciated.
Have you tried render_to_string in your controller when you're creating the record? That may allow you to just go ahead and "snapshot" the page.

Rails 3 No Method Error undefined method 'current_page'

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.

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.

How to use to_sentence with an AR collection

I have User.all, which returns 3 results.
How can I make it so I can render each result to something like:
Foo, Bar, and Foobar
Which when rendered in the browser, will display as:
Foo, Bar, and Foobar
I know about the to_sentence helper. But not very sure how to execute this, since User.all returns 3 hash objects. I can use .map(&:first_name), but how will I be able to provide the route path in the link_to method.
Looking for an approach that works.
I think you're looking for something like this. (answer updated)
In a helper:
module ApplicationHelper
...
include ActionController::UrlWriter
def generate_user_links_sentence
links = User.all.collect do |user|
link_to user.first_name, user_path(user)
end
links.to_sentence
end
...
end
# Example: <%= generate_user_links_sentence %>
You can separate out the generation logic into your controller if you so wish, but it's difficult enough accessing route paths from a helper, let alone the controller. There may be a better way to do this in a view, but this is all I can really think of right now.
Update: Just in a view:
<%= User.all.collect{|u| link_to u.first_name, user_path(u)}.to_sentence %>