Create and edit multiple records of the same model - ruby-on-rails-3

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.

Related

Syntax error, but correct syntax? unexpected ':', expecting ')'

I have a nested form (see below)
Model Artist (artist is a user)
has_many :art_works
has_many :canvases
accepts_nested_attributes_for :art_works //artworks is what im currently working on
accepts_nested_attributes_for :canvases
Controller art_works
def new
#artist = Artist.find(params[:artist_id])
#artwork = #artist.art_works.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #artwork }
end
end
def create
#artwork = ArtWork.new(params[:artwork])
respond_to do |format|
if #artwork.save
format.html { redirect_to #artwork, notice: 'artwork was successfully created.' }
format.json { render json: #artwork, status: :created, location: #artwork }
else
format.html { render action: "new" }
format.json { render json: #artwork.errors, status: :unprocessable_entity }
end
end
end
artworks views _form
<%= form_for(#artwork, :url => artist_art_works_path(current_artist) :multipart => true) do |f| %>
<p>
<%= f.file_field :art %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I was pretty positive that this would work, but i'm assuming my :url is incorrect? I'm not really sure what else it would be though. below are my routes for artworks the reason why I am nesting these things is because an artist can upload art into an artwork model the idea is to have several pieces of art in one artwork (like an album has many images inside of it)
artist_art_works GET /artists/:artist_id/art_works(.:format) art_works#index
POST /artists/:artist_id/art_works(.:format) art_works#create
new_artist_art_work GET /artists/:artist_id/art_works/new(.:format) art_works#new
edit_artist_art_work GET /artists/:artist_id/art_works/:id/edit(.:format) art_works#edit
artist_art_work GET /artists/:artist_id/art_works/:id(.:format) art_works#show
PUT /artists/:artist_id/art_works/:id(.:format) art_works#update
DELETE /artists/:artist_id/art_works/:id(.:format) art_works#destroy
Thank you very much in advance for your help. (sorry for the noobness)
You're missing a comma. Yeah the error message isn't that helpful.
#artwork, :url => artist_art_works_path(current_artist) :multipart => true
vs
#artwork, :url => artist_art_works_path(current_artist), :multipart => true

Rails 3 destroy multiple record through check boxes

I have issue with multiple delete using checkboxes. when i m deleting multiple records it get s ids for checkboxes but it is passing a method name as parameter and shows me error.
here is my code,
**In my Controller method :**
def destroy
#ticket = current_user.tickets.find(params[:ticket_ids])
#ticket.destroy
respond_to do |format|
format.html { redirect_to tickets_url }
format.json { head :no_content }
end
end
def destroy_multiple
Ticket.destroy(params[:tickets])
respond_to do |format|
format.html { redirect_to tickets_path }
format.json { head :no_content }
end
end
**In my index.html.erb**
<%= form_tag destroy_multiple_tickets_path, method: :delete do %>
.
.
<td class="table-icon">
<%= check_box_tag "ticket_ids[]", ticket.id %>
</td>
.
.
<%= submit_tag "Delete selected" %>
**In routes.rb**
resources :tickets do
collection do
delete 'destroy_multiple'
end
end
it shows me this error ::::
Couldn't find Ticket with id=destroy_multiple [WHERE "tickets"."user_id" = 1]
passes arguement ::::
{"utf8"=>"✓",
"_method"=>"delete",
"authenticity_token"=>"yHeRR49ApB/xGq1jzMTdzvix/TJt6Ysz88nuBEotHec=",
"ticket_ids"=>["11",
"12"],
"commit"=>"Delete selected",
"id"=>"destroy_multiple"}
Step:1 In routes.rb
resources :tickets do
collection do
delete 'destroy_multiple'
end
end
Step:2 In _form.html.erb
<%= form_tag destroy_multiple_tickets_path, method: :delete do %>
<td class="table-icon">
<%= check_box_tag "ticket_ids[]", ticket.id %>
</td>
<%= submit_tag "Delete selected" %>
<%end%>
Stpe:3 In Controller
def destroy_multiple
Ticket.destroy(params[:tickets])
respond_to do |format|
format.html { redirect_to tickets_path }
format.json { head :no_content }
end
end
do
Ticket.destroy(array_of_ids)
Try this
Ticket.where(:id => params[:ticket_ids]).destroy_all
Hi, Update your Controller code likewise..
def destroy_multiple
#tickets = Ticket.find(params[:ticket_ids])
#tickets.each do |ticket|
ticket.destroy
end
end

Rails 3 JQuery Pagination on a nested model

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?

Passing an object through a link_to command to be referenced in the model#new page

I'm creating a basic loyalty card application, with model Merchant and Loyaltycard. In the merchant#show view I have this line
<%=link_to 'New Loyalty card', new_loyaltycard_path(:merchant_id=>1) %>
I'm trying to pass the merchant ID to the loyaltycard#new view so it will be automatically selected as the merchant for that card. In loyaltycard#_form (which gets called by loyaltycard#new) I have the lines
<%if #merchant%>
<%= f.hidden_field :merchant_id, :value => #merchant.id %>
<%else%>
<div class="field">
<%= f.label :merchant_id %><br />
<%= f.text_field :merchant_id %>
</div>
<%end%>
But I keep getting and error that says can't call id for class Nil. Is there a better way of doing this?
Here is the controller code for loyaltycard
def new
#loyaltycard = Loyaltycard.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #loyaltycard }
end
end
# GET /loyaltycards/1/edit
def edit
#loyaltycard = Loyaltycard.find(params[:id])
end
# POST /loyaltycards
# POST /loyaltycards.json
def create
#loyaltycard = Loyaltycard.new(params[:loyaltycard])
respond_to do |format|
if #loyaltycard.save
format.html { redirect_to #loyaltycard, notice: 'Loyaltycard was successfully created.' }
format.json { render json: #loyaltycard, status: :created, location: #loyaltycard }
else
format.html { render action: "new" }
format.json { render json: #loyaltycard.errors, status: :unprocessable_entity }
end
end
end
The error is
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
What you need to do in your new action is using the params[:merchant_id] to look up and set #merchant
#merchant = Merchant.find(params[:merchant_id])
Then your code should work, without that, #merchant is nil, and you can't call the method :id on nil
You're not setting variable #merchant anywhere in your controller, but you use it the view.

Rendering a like/unlike button in rails with jQuery

I'm using jquery to render a like button on a story. I have two models story and like
Here's the likes controller code:
def create
#like = Like.new(params[:like])
#story = Story.find(params[:story])
#like.story = #story
if #like.save
respond_to do |format|
format.html
format.js
end
end
end
def destroy
#like = Like.find(params[:id])
##story = #like.story
#like.destroy
respond_to do |format|
format.html { redirect_to stories_url }
format.js
format.json { head :ok }
end
end
This is the button partial (stories/like_button):
<% unless user_likes_story?(#story, current_user) %>
<%= button_to 'like', "/likes?story=#{#story.id}", :id => 'like_button', :remote => true %>
<% else %>
<%= button_to 'liked', #liked, :class => 'like_button unlike', :id => 'unlike_button', :remote => true, method: :delete %>
<% end %>
The problem is that my create.js.erb, when rendering the 'unlike' button, doesn't properly load the #liked instance variable, because it's being set in the #show action of the stories controller before the user has liked the story, so I can't figure out how or where to set it so the JS will render the unlike properly. I'm probably making this harder than it has to be, but...
create.js.erb
$('.button_to').replaceWith('<%=j render 'stories/like_button' %>');
$('#story_likes_count').replaceWith('<%=j render 'stories/likes_count' %>');
Here's how I tried setting #liked, in stories#show
def show
#like = Like.new
#story = Story.find(params[:id])
if current_user
#liked = Like.find_by_user_id_and_story_id(current_user,#story)
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: #story }
end
end
Any idea how to do this properly? Should I just move the partial out of the stories folder?
The solution was pretty simple. In the controller, doing this:
format.js {#liked = #like}
Passes the variable and makes it available to the partial when it's rendered.