Rails 3 JQuery Pagination on a nested model - ruby-on-rails-3

I have watched the railscasts on Pagination with AJAX. I am trying to duplicate his method using a nested model. I seem to be confused because i don't have a view for the child model or a fully integrated controller.
Assume the Product has_many Comments. How can I do the same pagination using will_paginate on comments within a product view?
UPDATE:
Here is most of my attempt (my attempt actually uses kaminari but it's basically the same as will_pagenate):
view/products/comments/_index.html.erb:
<div id="comments">
<% comments.each do |comment| %>
<%= f.fields_for :comments, comments do |comment_builder| %>
<%= render 'products/comments/field', f: comment_builder %>
<% end %>
<% end %>
<%= paginate comments %>
</div>
view/products/comments/_field.html.erb:
<fieldset>
<%= f.hidden_field :_id, :value => f.object._id %>
<%= f.hidden_field :_destroy %>
<%= f.object.text %>
<%= link_to "remove", '#', class: "remove_field" %>
</fieldset>
view/products/comments/_index.js.erb:
$("#comments").html("j render("products/comments/index")) %>");
assets/javascripts/product.js
$('.pagination a').live
click: ->
$(".pagination").html("Page is loading...")
$.getScript(this.href)
false
controllers/comments_controller.rb
class CommentsController < ApplicationController
before_filter :get_product
def index
#comments = #product.comments.desc(:created_at).page(params[:page]).per(10)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #comments }
end
end
private
def get_product
#product = Product.find(params[:product_id]) if params[:product_id]
redirect_to root_path unless defined?(#product)
end
end
views/products/show.html.erb (an excerpt)
<%= render "products/comments/index", f: f, comments: #comments%>
controllers/products_controller.rb
def show
#product = Product.find(params[:id])
#comments = #product.comments.desc(:created_at).page(params[:page]).per(1)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
view/products/comments/_index.js.erb never gets called. Also is there a more rails-like way of doing this?

Related

What are some possible reasons that you would get an undefined method 'TheModelName_path' error using a form_for in rails 3?

I can't figure out why I have this error from my knowledge everything is set up as it should to work.
this is the error i get:
NoMethodError at /short/new
undefined method `shorts_path' for #<#:0x007fbc441426e0>
my model is short not shortS
when I run rake routes there is no shorts_path so I'm not sure where this helper is coming from. I don't understand why the form_for is giving me this error when #short is defined in the def new section of my controller.
Can someone please explain this to me?
Thank you in advance
This is what my controller looks like
class ShortController < ApplicationController
def show
#short = Short.find(params[:id].to_i(36))
respond_to do |format|
#redirect directly to the url stored as long in the database
format.html { redirect_to #short.long}
end
end
def new
#short = Short.new
respond_to do |format|
format.html # new.html.erb
end
end
def create
#short = Short.new(params[:short])
respond_to do |format|
if #short.save
format.html { render action: "show" }
else
format.html { render action: "new" }
end
end
end
end
routes
Trainingproject::Application.routes.draw do
root :to => 'short#welcome'
resources :short, :only => [:new, :create, :show]
end
form partial which is rendered in the view for new
<%= form_for(#short) do |f| %>
<% if #short.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#short.errors.count,"error") %> prohibited this url from being saved:</h2>
<ul>
<% #short.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-field">
<%= f.label "Enter your URL" %><br />
<%= f.text_field :long %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You're getting this error because form_for(#short), where #short is a new Short record, will attempt to use the path helper called shorts_path to route to where the form needs to go. You're missing this route definition in your config/routes.rb file:
resources :shorts
Please read the Getting Started and Routing guides, which should explain the things that you're missing.

Rails Submit Form Remote issues

I am trying to submit a form via remote: true in rails 3 and for some reason when I look at the response in the browser I only see the raw response instead of the JavaScript being interpreted.
Form:
<%= form_for #entry, url: contest_entries_path, remote: true, html: {id: "contest_form"} do |f| %>
Controller:
class ContestEntriesController < ApplicationController
respond_to :html, :js
def index
#entry = ContestEntry.new
#entry.build_school
respond_with #entry
end
def create
#entry = ContestEntry.new(params[:contest_entry])
respond_with #entry
end
end
Create.js.erb:
<% unless #entry.errors.any? %>
<% if #entry.parent? %>
$('body').find('#parents_message').show();
<% else %>
$('body').find('#falculty_message').show();
<% end %>
<% end %>
The response in the browser is the raw JavaScript response

Create a new perfil inside a persons _controller

I'm in rails 3.2.2
I'm trying to create a new perfil inside a persons_controller
Models symplify:
class Person < ActiveRecord::Base
belongs_to :perfil, :foreign_key=>'perfil_id'
class Perfil < ActiveRecord::Base
has_one :person
I created ad_perfil inside persons_controller
def ad_perfil
#person = Person.find(params[:id])
#perfil = Perfil.new
end
and I supused 2 ways:
1) using f.submit
ad_perfil.html.erb:
<%= #person.nombre %>
<%= form_for(#perfil) do |f| %>
<div class="field">
<%= f.label :nombre %><br />
<%= f.text_field :nombre %>
</div>
<%= f.submit %>
<% end %>
this redirect me to create to perfils_controller and there I can't get #persona:
def create
#perfil = Perfil.new(params[:perfil])
#person = Person.find(params[:id])
respond_to do |format|
if #perfil.save
format.html { redirect_to #perfil, notice: 'Perfil was successfully created.' }
format.json { render json: #perfil, status: :created, location: #perfil }
#person.perfil_id = #perfil.id
#person.save
2) I created a new def in persons_controller:
def new_perf
#perfil = Perfil.new(params[:perfil])
#person = Person.find(params[:id])
if #perfil.save
#person.perfil = #perfil
#person.update_attributes(params[:person])
redirect_to #person
end
end
and I tryed change f.submit for:
<td><%= link_to 'New Perfil', :action => "new_perf", :id=> #person.id %></td>
and it created a new perfil without any field and of course don save perfil.id inside #person.perfil_id.
Is possible that I'm trying? Is there any other methods? I guess that will be more easy puting perfil.person_id instead person.perfil_id but I want access to person.perfil later.
I hope you understand me, and maybe I don't understand "submit" very well. any suggestions? thanks!

Why am I getting "no route matches [POST]" in my nested resources?

I have a project that contains projects that have todos that have tasks. When I try to create a new task, I get this error when I submit:
No route matches [POST] "/projects/1/todos/19/tasks/new"
Here is my form:
<%= form_for [#todo, #todo.tasks.new], :url => new_project_todo_task_path(#project, #todo) do |f| %>
<div class="field">
<%= f.label :description, "Description" %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %> or <%= link_to "Cancel", "#", :id => "cancel_new_task_link" %>
</div>
<% end %>
Here is my controller:
class TasksController < ApplicationController
before_filter :authenticated?
before_filter :get_project_and_todo
respond_to :html, :xml, :json, :js
def new
#task = #todo.tasks.new
end
def create
#task = #todo.tasks.new(params[:task])
if #task.save
respond_with #todo, :location => project_todo_path(#project, #todo)
else
render "new"
end
end
private
def get_project_and_todo
#project = Project.find(params[:project_id])
#todo = #project.todos.find(params[:todo_id])
end
end
Here are my routes:
resources :projects do
resources :todos do
resources :tasks
end
end
Thanks
Your URL should not be new_project_todo_task_path(#project, #todo). You don't need to specify the URL here as Rails will imply it from the parameters passed in to form_for.
If the final object is a new object and not persisted in the database then it will make a POST request to, in this case, /projects/:project_id/todos. You're declaring in your example that you want to make a POST request to /projects/:project_id/todos/new, for which there is no POST route and that is why it's failing.

Create and edit multiple records of the same model

I'm quite new to Rails and to this Q&A website! So hello everyone, I'm a French Rails tester. I have cross posted this question to Railsforum too, to have a better chance on getting an answer :) (http://railsforum.com/viewtopic.php?id=44238)
I need help on something. It seems simple, but in my opinion, not well documented (I browsed the web for 3 days without finding a clean answer. Some partial solutions here, but not a complete and clean one for a newbie!)
I want to add some actions to a default scaffolded controller to handle creation and modification of multiple records in the same form.
rails generate scaffold Item title:string
This is the sample scaffolded code:
app/controllers/items_controller.rb
class ItemsController < ApplicationController
# GET /items
# GET /items.xml
def index
#items = Item.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #items }
end
end
# GET /items/1
# GET /items/1.xml
def show
#item = Item.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #item }
end
end
# GET /items/new
# GET /items/new.xml
def new
#item = Item.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #item }
end
end
# GET /items/1/edit
def edit
#item = Item.find(params[:id])
end
# POST /items
# POST /items.xml
def create
#item = Item.new(params[:item])
respond_to do |format|
if #item.save
format.html { redirect_to(#item, :notice => 'Item was successfully created.') }
format.xml { render :xml => #item, :status => :created, :location => #item }
else
format.html { render :action => "new" }
format.xml { render :xml => #item.errors, :status => :unprocessable_entity }
end
end
end
# PUT /items/1
# PUT /items/1.xml
def update
#item = Item.find(params[:id])
respond_to do |format|
if #item.update_attributes(params[:item])
format.html { redirect_to(#item, :notice => 'Item was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #item.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.xml
def destroy
#item = Item.find(params[:id])
#item.destroy
respond_to do |format|
format.html { redirect_to(items_url) }
format.xml { head :ok }
end
end
end
app/views/items/new.html.erb
<h1>New item</h1>
<%= render 'form' %>
<%= link_to 'Back', items_path %>
app/views/items/edit.html.erb
<h1>Editing item</h1>
<%= render 'form' %>
<%= link_to 'Show', #item %> |
<%= link_to 'Back', items_path %>
app/views/items/_form.html.erb
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In my opinion, I need to add 4 methods to my Item controller:
new_multiple : to initialize the new
multiple form view
create_multiple :
to handle results from the new
multiple form view
edit_mulitple : to
initialize the edit multiple form
view
update_mulitple : to handle
results from the edit multiple form
view
Then, I need to create 2 new views:
new_multiple.html.erb with code for a multiple record (5 in the same time) creation form
edit_mulitple.html.erb with code for a multiple record (5 in the same time) edition form
and maybe _partials to avoid DRY stuff...
Anyone want to help me write these 4 actions and 2 views? I would really appreciate that.
Thanks! :)
Note : In my application items are children of parentItems but I DON'T WANT to use nested_form to create items in parentItems forms like in railscast 196 and 197.