Rails ajax form for products - ruby-on-rails

I've been working with app for pizzeria where customers could order pizzas through their website. I currently working with the product page where I try to submit products to shopping cart through ajax, but I'm really stuck. I haven't been able to build a shoppingcart which would accept product-id, product-size-id, extra-toppings as an array and quantity. I decided to try to go with session-store where all the order-row ids are stored and on menu page every product has a form where user could add product, size and quantity to shoppingcart but I keep getting this error in server logs:
Started POST "/order_row" for ::1 at 2015-08-03 11:18:21 +0300
Processing by OrderRowsController#create as JS
Parameters: {"utf8"=>"✓", "order_row"=>{"product"=>"1", "size"=>"0", "quantity"=>"2"}, "commit"=>"Tilaa"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200)):
app/controllers/order_rows_controller.rb:4:in `create'
I have models Product, ProductCategory, Order, OrderRow and my session stores order-row-ids as mentioned. My menu page is actually product_categories#show -view where products belonging to that category are listed.
#order_rows_controller.rb
class OrderRowsController < ApplicationController
respond_to :html, :js
def create
#orow = OrderRow.new(order_rows_params)
if #orow.save
session[:order_row_ids] << #orow.id
flash[:notice] = "Lisättiin ostoskoriin!"
else
flash[:error] = "Tuotteen lisääminen ostoskoriin epäonnistui."
redirect :back
end
end
def update
#orow = OrderRow.find(params[:id])
if #orow.update_attributes(params[:order_row])
flash[:notice] = "Ostoskori päivitetty."
else
flash[:error] = "Ostoskorin päivitys epäonnistui."
end
end
def destroy
#orow.find(params[:id]).destroy
flash[:notice] = "Tuote poistettu onnistuneesti"
end
private
def order_rows_params
params.require(:order_row).permit(:product, :size, :quantity) #, :extras => []
end
end
ProductCategories-controller
class ProductCategoriesController < ApplicationController
before_action :set_product_category, only: [:edit, :update, :destroy]
respond_to :html, :js
def index
#product_categories = ProductCategory.all
end
def show
#product_category = ProductCategory.friendly.find(params[:id])
#product_categories = ProductCategory.all
#products = #product_category.products
#order_row = OrderRow.new(order: nil, product: nil, size: nil, extras: nil, quantity: nil)
end
And menu-page in product_categories/show.html.erb
#product_categories#show -view
<!--- category descriptions -->
<div class="container">
<% #products.each do |product| %>
<div class="col-sm-6 col-md-4">
<div class="product well">
<h3><%= product.name %></h3>
<span><%= product.description %></span>
<p class="prices">
<%= price(product.normal_price) %> | <%= price(product.plus_size_price) %> | <%= price(product.lunch_price) %>
</p>
<br>
<div id="form-<%= product.id %>">
<%= simple_form_for #order_row, :url => url_for(:controller => 'order_rows', :action => 'create'), remote: true do |f| %>
<%= f.hidden_field :product, :value => product.id %>
<h5>Koko</h5>
<div style="padding-left: 13px">
<%= f.input :size, collection: OrderRow.sizes, as: :radio_buttons, label: false, item_label_class: "radio-inline", item_wrapper_tag: false %>
</div>
<h5>Määrä</h5>
<div style="width: 8%; padding-left: 13px;">
<%= f.input :quantity, as: :string, label: false %>
</div>
<p>
<%= f.submit "Tilaa", class: "btn btn-success btn-lg" %>
</p>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
Create.js.erb in order_rows#create action
#create.js.erb
$("#form-<%= params[:product] %>").load(document.URL + "#form-<%= params[:product]");
Associations:
#order_row
belongs_to :order
belongs_to :product
#product
belongs_to :product_category
has_one :campaign_producte
belongs_to :dish_type
#product_categories
has_many :products
has_many :campaign_products
has_many :product_extras
has_many :dish_types, through: :products
#product_extra
belongs_to :product_category
Link to github-repo: https://github.com/casualCodeAndDesign/ravintolamammamia
What's the reason for this server error and why it doesn't store my order_row to the database?

ActiveRecord::AssociationTypeMismatch (Product(#70158072501800)
expected, got String(#70158039566200))
You need to change
<%= f.hidden_field :product, :value => product.id %>
to
<%= f.hidden_field :product_id, :value => product.id %>
and product to product_id in create.js.erb and order_rows_params

Related

Rails error messages not appearing on website but showing in Chrome DevTools Network Preview

I'm trying to show error messages when creating a new instance of a model in my Rails app when a field doesn't pass validation. For some reason, the errors never actually show up on the website next to the fields like they're supposed to. However, the errors appear in the 'Preview' section of the Network tab of Chrome DevTools. So the errors are generating properly. In the terminal it says that new.html.erb is rendered but I don't think it actually does? Any help would be greatly appreciated - I haven't found much about this online. I'm using Tailwind CSS for styling the front end if that's helpful.
Here's my code:
occasion.rb
class Occasion < ApplicationRecord
belongs_to :user
has_many :registries
has_many :charities, :through => :registries
validates :occasion_name, presence: true
validates :occasion_date, presence: true
validates :url, presence: true, format: { without: /\s/ }, uniqueness: true
validates_uniqueness_of :user_id
end
occasions_controller.rb
class OccasionsController < ApplicationController
load_and_authorize_resource only: [:new, :create, :edit, :update, :destroy]
def index
#occasions = Occasion.all
end
def show
#occasion = Occasion.where(url: params[:url]).first
end
def new
#occasion = Occasion.new
end
def create
#occasion = Occasion.new(occasion_params)
#occasion.user = current_user
if #occasion.save
respond_to do |format|
format.html { redirect_to new_registry_path }
format.js { render :js => "window.location='#{ new_registry_path }'" }
end
else
render :new
end
end
def edit
#occasion = Occasion.find(params[:id])
end
def update
#occasion = Occasion.find(params[:id])
if #occasion.update(occasion_params)
redirect_to #occasion
return
else
render :edit
end
end
def destroy
#occasion = Occasion.find(params[:id])
#occasion.destroy
redirect_to occasions_path
end
private
def occasion_params
params.require(:occasion).permit(:user_id, :occasion_name, :occasion_date, :url)
end
# user authentication is not required for show
skip_before_action :authenticate_user!, :only => [:show]
end
new.html.erb
<%= form_with model: #occasion do |form| %>
<div class="text-center">
<%= form.label :occasion_name, "Occasion Name", class: "text-red-400 font-semibold px-8" %><br>
<%= form.text_field :occasion_name, class: "rounded w-2/5" %>
<% #occasion.errors.full_messages_for(:occasion_name).each do |message| %>
<div><%= message %></div>
<% end %>
</div>
<div class="text-center py-2">
<%= form.label :occasion_date, "Occasion Date", class: "text-red-400 font-semibold px-8" %><br>
<%= form.date_field :occasion_date, type: "date", class: "rounded" %>
<% #occasion.errors.full_messages_for(:occasion_date).each do |message| %>
<div><%= message %></div>
<% end %>
</div>
<div class="text-center py-2">
<%= form.label :url, 'URL', class: "text-red-400 font-semibold px-8" %><br>
<%= form.text_field :url, class: "rounded" %>
<% #occasion.errors.full_messages_for(:url).each do |message| %>
<div><%= message %></div>
<% end %>
<em><div class="text-sm">domainname.com/yourURLhere</div></em>
</div>
<div class="text-center py-2">
<%= form.submit occasion.persisted? ? 'Update' : 'Save', class: "rounded-full bg-red-400 text-white px-3" %>
</div>
<% end %>
From the provided information, it looks like the form gets submitted as an AJAX request. Since you're not passing local: false to the form_with call, there must be a configuration set to use AJAX form submissions by default.
From the docs,
:local - By default form submits via typical HTTP requests. Enable remote and unobtrusive XHRs submits with local: false. Remote forms may be enabled by default by setting config.action_view.form_with_generates_remote_forms = true.
Pass local: true to submit the request via a normal HTTP request.
<%= form_with model: #occasion, local: true do |form| %>
<%#= ... %>
<% end %>

Weird Routing Behavior in Rails

I'm using rails 4.2. I have created a form that submits to a particular action in my controller. Here is the beginning of the form code and the controller definition:
view.html.erb
<div id="account-booking" class="tab-pane">
<%= form_for #booking_info, url: { action: 'book' } do |b| %>
<fieldset class="group column-1">
<legend>Booking Preference for <%= Rails.configuration.x.app_settings.year %></legend>
<div class="group column-full radio-list">
<%= label_tag('Select Room Type') %>
<% #available_rooms.each do |rt| %>
<div class="radio-item">
<!--
<%= b.radio_button :room_type_id, rt.id, :class => 'rb_room_type inline', :onclick => fetch_room_info_path(:id => rt.id), :remote => true %>
<%= b.radio_button :room_type_id, rt.id, :class => 'rb_room_type inline', :onclick => 'render_room_info('+ rt.id.to_s + ');' %>
-->
<%= b.radio_button :room_type_id, rt.id, :class => 'rb_room_type inline' %>
<%= content_tag :span, rt.name %>
<a data-toggle="tooltip" data-placement="right" title="<%= rt.description %>">
<%= image_tag "tooltip.png", :class=>"tooltip-icon" %>
</a>
</div>
<% end %>
<%= b.label :roommate_preference, 'Roommate Preference' %>
<%= b.text_area :roommate_preference, :class => 'form-control' %>
<div class="account-checkbox-options">
<%= b.label :is_flexible, class: 'checkbox inline' do %>
<%= b.check_box :is_flexible %>
I am flexible with regards to my room choice.
<% end %>
</div>
</div>
<!--
<div id="estimated-due" class="group column-2">
</div>
-->
</fieldset>
<fieldset class="group column-2 account-preferences">
<legend>Your Room Information for <%= Rails.configuration.x.app_settings.year %></legend>
<div class="group column-1">
<div class="group column-full add-tabbing">
<%= label_tag('Selected Room:') %>
<span><%= #booking_info.room_type.blank? ? '<No Room Selected>' : #booking_info.room_type.name %></span>
</div>
<div class="group column-full add-tabbing">
<%= label_tag('Assigned Room:') %>
<span><%= #booking_info.assigned_type.blank? ? '<No Room Assigned>' : #booking_info.assigned_type.name %></span>
</div>
</div>
<div class="group column-2">
<div class="group column-full add-tabbing">
<%= label_tag('Total Due:') %>
<span ><%= number_to_currency(#booking_info.total_due.blank? ? 0.00 : #booking_info.total_due) %></span>
</div>
<div class="group column-full add-tabbing">
<%= label_tag('Current Balance:') %>
<span><%= number_to_currency(#booking_info.outstanding_balance.blank? ? 0.00 : #booking_info.outstanding_balance) %></span>
</div>
</div>
<% unless #booking_info.assigned_type.blank? %>
<div class="group column-full">
<h2>Assigned Room Information</h2>
</div>
<% end %>
</fieldset>
<div class="account-buttons">
<%= b.submit 'Submit', class: 'btn btn-danger' %>
<%= link_to 'Cancel', '/pages/home', class: 'link-button-cancel' %>
</div>
<% end %>
</div>
account_controller.rb
def book
#booking = PersonRoom.new(booking_params)
#requested_room = RoomType.find(params[:person_room][:room_type_id])
#booking.room_type = #requested_room
if update_booking #booking
redirect_to :controller => 'account', :action => 'view'
else
render('view')
end
end
All of this works beautifully when there is no PersonRoom record (e.g. if I'm doing an insert). However, if I try to update the record, using the same exact form / action (because both the view and the action are exactly the same regardless of whether i'm inserting or updating), I get an error when I click "Submit":
No route matches [PATCH] "/account/book"
This makes no sense. I'm on the exact same page. I just used all of this code to create the record, so the route clearly exists (because it calls /account/book). Now I want to update but suddenly the route doesn't match? And it never even breaks into the code because it doesn't call the controller action. This makes zero sense to me. Hope someone can help.
I winced when looking over your code, here's how I'd have done it:
#config/routes.rb
resources :rooms do
resources :bookings #-> url.com/rooms/:room_id/bookings/new
end
#app/controllers/bookings_controller.rb
class BookingsController < ApplicationController
before_action :set_room
def new
#booking = room.bookings.new
end
def create
#booking = room.bookings.new booking_params
#booking.save
end
def update
#booking = room.bookings.update booking_params
end
private
def set_room
#room = Room.find params[:room_id]
end
def booking_params
params.require(:booking).permit(:x, :y, :z)
end
end
This setup is pretty standard with Rails (IE resourceful routes):
Browsers request pages from Rails by making a request for a URL using
a specific HTTP method, such as GET, POST, PATCH, PUT and DELETE. Each
method is a request to perform an operation on the resource. A
resource route maps a number of related requests to actions in a
single controller.
Observing this principle would likely resolve your "weird" routing errors due to their cohesion with the other aspects of the Rails system.
For example:
#app/controllers/accounts_controller.rb
class AccountsController < ApplicationController
#-> this shouldn't have a "book" action unless necessary. Your case is not necessary
end
--
The problem you have is that you're not adhering to the Rails' convention of keeping your system object-orientated.
The importance of this is huge; Rails is designed to make it as simple as possible to CRUD (create read update destroy) data objects. These objects are built from database data in the "models":
I use this picture a lot - it shows how Rails should work. You build objects in your Models which you manipulate in your controllers. The views show it all to the user.
Thus, when you're looking at your code, I would keep it as simple as possible:
#app/models/room.rb
class Room < ActiveRecord::Base
has_many :bookings
has_many :users, through: :bookings
belongs_to :type
end
#app/models/booking.rb
class Booking < ActiveRecord::Base
belongs_to :room
belongs_to :user
end
#app/models/user.rb
class User < ActiveRecord::Base
has_many :bookings
has_many :rooms, through: :bookings
end
This will allow you to call the following:
#room = Room.find 1
#room.bookings #-> collection of bookings per room
You could also do something like the following (simple) for the views:
#app/views/bookings/new.html.erb
<%= form_for #booking do |f| %>
<%= f.text_field :x %>
<%= f.text_field :y %>
<%= f.text_field :z %>
<%= f.submit %>
<% end %>
I found the problem. It was something really stupid. I had the following line in my route.config:
match ':controller(/:action(/:id))', :via => [:get, :post]
I just needed to change this to:
match ':controller(/:action(/:id))', :via => [:get, :post, :patch, :put, :delete]
Now it handles updates (i.e. patch / put)

Using jQuery Tokeninput within a nested form partial

I'm using jQuery Tokeninput as shown in this Railscast. I'd like to combine this functionality in a nested form but get the error
undefined method `artists' for #<SimpleForm::FormBuilder:0x007febe0883988>
For some reason its not recognizing the track parameter in my form builder which is stopping me to get a hold of albums I have on record.
<div class="input">
<%= f.input :artist_tokens, label: 'Featured Artists', input_html: {"data-pre" => f.artists.map(&:attributes).to_json} %>
</div>
Keep in mind this works in my track form but just not in my album form since its nested. What should I do to get this to work?
class ArtistsController < ApplicationController
def index
#artists = Artist.order(:name)
respond_to do |format|
format.html
format.json {render json: #artists.tokens(params[:q])}
end
end
end
Models
class Artist < ActiveRecord::Base
has_many :album_ownerships
has_many :albums, through: :album_ownerships
has_many :featured_artists
has_many :tracks, through: :featured_artists
def self.tokens(query)
artists = where("name like ?", "%#{query}%")
if artists.empty?
[{id: "<<<#{query}>>>", name: "Add New Artist: \"#{query}\""}]
else
artists
end
end
def self.ids_from_tokens(tokens)
tokens.gsub!(/<<<(.+?)>>>/) {create!(name: $1).id}
tokens.split(',')
end
end
class Albums < ActiveRecord::Base
attr_reader :artist_tokens
accepts_nested_attributes_for :tracks, :reject_if => :all_blank, :allow_destroy => true
has_many :albums_ownerships
has_many :artists, through: :albums_ownerships
def artist_tokens=(ids)
self.artist_ids = Artist.ids_from_tokens(ids)
end
end
class Track < ActiveRecord::Base
attr_reader :artist_tokens
belongs_to :album
has_many :featured_artists
has_many :artists, through: :featured_artists
def artist_tokens=(ids)
self.artist_ids = Artist.ids_from_tokens(ids)
end
end
class AlbumOwnership < ActiveRecord::Base
belongs_to :artist
belongs_to :album
end
class FeaturedArtist < ActiveRecord::Base
belongs_to :artist
belongs_to :track
end
Album Form
<%= simple_form_for(#album) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<h1>Tracks</h1>
<%= f.simple_fields_for :tracks do |track| %>
<%= render 'track_fields', :f => track %>
<% end %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :tracks %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Track Partial
<div class="field">
<%= f.input :name %><br>
</div>
<div class="input">
<%= f.input :artist_tokens, label: 'Featured Artists', input_html: {"data-pre" => f.artists.map(&:attributes).to_json} %>
</div>
JS
$(function() {
$('#track_artist_tokens').tokenInput('/artists.json', {
prePopulate: $("#track_artist_tokens").data("pre"),
theme: 'facebook',
resultsLimit: 5
});
});
UPDATE
As mentioned by nathanvda, I needed to use f.object in order for the artists to be recognized. So in my partial I now have:
<%= f.input :artist_tokens, label: 'Featured Artists', input_html: {"data-pre" => f.object.artists.map(&:attributes).to_json, class: 'test_class'} %>
In my js I also needed to call the token input method before/after insertion:
$(function() {
$('.test_class').tokenInput('/artists.json', {
prePopulate: $(".test_class").data("pre"),
theme: 'facebook',
resultsLimit: 5
});
$('form').bind('cocoon:after-insert', function(e, inserted_item) {
inserted_item.find('.test_class').tokenInput('/artists.json', {
prePopulate: $(".test_class").data("pre"),
theme: 'facebook',
resultsLimit: 5
});
});
});
The only remaining issue I have is the the tracks_attributes not being saved. I ran into an issue similar to this in the past in this post but the two main difference is the second level of nesting involved and that I used a join table within my nested form. I'm not entirely sure if or how any of that code would translate over but I believe this is most likely problem. As far as the permitted params of my albums_controller here's what they looks like.
def album_params
params.require(:album).permit(:name, :artist_tokens, tracks_attributes: [:id, :name, :_destroy, :track_id])
end
If you need to acces the object of a form, you need to write f.object, so I think you should just write f.object.artists.
Your "data-pre" => f.artists... is calling the artists method on f which is the form builder and doesn't have an #artists method.
Try this instead:
In the album form, change the render partial line to this:
<%= render 'track_fields', :f => track, :artists => #artists %>
And then use this in the track partial:
<%= f.input :artist_tokens, label: 'Featured Artists', input_html: {"data-pre" => artists.map(&:attributes).to_json} %>
UPDATED
Let's back up a step. From your code it looks like you need to populate a data-pre attribute with the attributes of a collection of artists.
The problem is you're calling f.artists where f is the FormBuilder and doesn't know anything about artists. This is why you're getting undefined method 'artists'...
The solution is make a collection of artists available to the view and its partials. One way to do this:
class AlbumsController < ApplicationController
...
def new
#album = Album.new
#artists = Artist.order(:name) # or some other subset of artists
end
...
def edit
#album = Album.find params[:id]
#artists = Artist.order(:name) # or perhaps "#artists = #album.artists", or some other subset of artists
end
end
and then in new.html.erb and edit.html.erb, pass #artists to the form partial:
... # other view code
<%= render 'form', album: #album %>
... # other view code
and then in your form partial:
... # other view code
<%= f.simple_fields_for :tracks do |track_form| %>
<%= render 'track_fields', :f => track_form %>
<% end %>
... # other view code
finally, in your track partial:
... # other view code
<div class="input">
<%= f.input :artist_tokens, label: 'Featured Artists', input_html: {"data-pre" => #artists.map(&:attributes).to_json} %>
</div>
... # other view code
Does that make sense?

Nested resource parameters not being set properly in form_for

I'm getting a curious error after submitting my form. Been trying to solve this for several hours..
No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]
The parameters are:
{"utf8"=>"✓",
"authenticity_token"=>"w0D7XmX2X2/ZMU19T6RlMvWCEClXnCFFOR+4EdIFvWg=",
"comment_item"=>{"item_id"=>"",
"name"=>"kaljdf",
"body"=>"yet another comment test"},
"commit"=>"Post Comment",
"matter_id"=>"3",
"item_id"=>"141"}
I have the following models:
class Matter < ActiveRecord::Base
has many :discoveries
delegate :items, to: :discoveries
end
class Discovery < ActiveRecord::Base
belongs_to :matter
scope :items, -> { where(type: 'Item') }
end
class Item < Discovery
has_many :comment_items
end
class CommentItem < ActiveRecord::Base
belongs_to :item
end
Controllers:
class ItemsController < DiscoveriesController
def show
#item = Item.find(params[:id])
#comment_item = CommentItem.new
end
def edit
#item = Item.find(params[:id])
end
def new
#item = Item.new
end
end
class CommentItemsController < ApplicationController
before_action :set_comment_item, only: [:show, :edit, :update, :destroy]
def new
#item = Item.find(params[:item_id])
#comment_item = #item.comment_item.new
end
def create
#item = Item.find(params[:item_id])
#comment_item = #item.comment_items.new(comment_item_params)
if #comment_item.save
flash[:notice] = 'Comment was successfully created'
redirect_to matter_item_url(matter_id: params[:matter_id])
else
flash[:notice] = "Error creating comment: #{#comment.errors}"
redirect_to matter_item_url(#matter, #item)
end
end
def destroy
#comment_item = CommentItem.find(params[:id])
#comment_item.destroy
redirect_to(#comment_item.item)
end
private
def set_comment_item
#comment_item = CommentItem.find(params[:id])
end
def comment_item_params
params.require(:comment_item).permit(:name, :body, :item_id, :matter_id)
end
end
The show action for the item resource:
<p>
<strong>Matter:</strong>
<%= #item.matter_id %>
</p>
<p>
<strong>Content:</strong>
<%= #item.content %>
</p>
<hr />
<%= form_for #comment_item, url: matter_item_comment_items_path(matter_id: #item.matter, item_id: #item.id) do |f| %>
<% if #comment_item.errors.any? %>
<ul>
<% #comment_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.hidden_field :item_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Post Comment" %>
</p>
<% end %>
<%= render :partial => 'comment_item', :collection => #item.comment_items %>
<%= link_to 'Edit', edit_matter_item_path(id: #item.id) %> |
<%= link_to 'Back', matter_items_path %>
Routes
resources :items do
resources :comment_items
end
resources :matters do
resources :items do
resources :comment_items
end
end
When looking at CommentItems in the console, I see that the comments are in fact being added to the model with their correct ID's, but they don't seem to be passed as parameters.. What am I missing?
I've reviewed Rails 4 form_for double nested comments and Rails 3.2 - Nested Resource Passing ID but I didn't have much luck..
I'd really appreciate your help!
No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]
your request is going to ItemsController instead of CommentItemsController
see :controller => "items"

Updating multiple records massing assignment error

I'm working on a shows when a store was last visited. I want to be able to update multiple stores at once if they were all visited on the same day.
I think I have most of the code but I can't figure out how to get rid of the mass assignment error
Can't mass-assign protected attributes: date_visited(1i), date_visited(2i), date_visited(3i)
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"/yr8kLOyrTRGPfG1f/I5ilY/QB6GUx9IhQj6WiBaibM=",
"store_ids"=>["4",
"5"],
"visit"=>{"date_visited(1i)"=>"2012",
"date_visited(2i)"=>"11",
"date_visited(3i)"=>"14"},
"commit"=>"Save Visit"}
Model
class Visit < ActiveRecord::Base
attr_accessible :date_visited, :spent, :store_id
belongs_to :
end
Controller
def update_multiple
#visits = Store.find(params[:store_ids])
#visits.each do |visit|
visit.update_attributes(params[:visit])
end
flash[:notice] = "Updated products!"
redirect_to stores_path
end
View
<%= form_for :visit, :url => update_multiple_visits_path, :html => { :method => :put } do |f| %>
<ul>
<% #visits.each do |visit| %>
<%= hidden_field_tag "store_ids[]", visit.id %>
<% end %>
</ul>
<div class="field">
<%= f.label :date_visited %><br />
<%= f.date_select :date_visited %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<ol id="route">
<% #visits.each do |store| %>
<%= content_tag_for :li, store do %>
<%= "#{store.store} - #{store.address}" %>
<% end %>
<% end %>
</ol>
Most likely, you are missing the attr_accessible :your_model_attributes is this case, :visits_attributes on your activerecord model definition.
Also, your params should look like
{ visits =>
{ id_1 =>
{ :store_id
:attributes_for_visit_1 }
}
{ id_2 =>
{ :store_id
:attributes_for_visit_2 }
}
} # and so on....
# visits_controller.rb
def update_nultiple_visits
#visits = Visits.find(params[:visits].keys).each{|visit|visit.update_attributes!}
end
Add this to your Store model
attr_accessible :visits_attributes
accepts_nested_attributes_for :visits
And I'd suggest changing your controller to this:
def update_multiple
#stores = Store.find(params[:store_ids])
#stores.each do |store|
store.update_attributes(params[:visit])
end
flash[:notice] = "Updated products!"
redirect_to stores_path
end
Helper date_select generate three a select tags (for year, month and day).
You can concatenate its before updating attributes.
For example:
Date.civil(params[:visit][:date_visited(1i)].to_i, params[:visit][:date_visited(2i)].to_i, params[:visit][:date_visited(3i)].to_i)

Resources