Can't get the value stored when updating attributes - ruby-on-rails

I am trying to do a marking system for moderators, in which they can select each item as "editor pick".
I've added a :editor_pick column to photos table(boolean)
and added a form in the partial for each item to have a checkbox in it:
<% if controller_name == "photos" %>
<%= simple_form_for [#user, #photo] do |f| %>
<%= f.input :editor_pick, label: "Portada" %>
<%= f.submit "Pick" %>
<% end %>
<% end %>
after that, I required the new param in strong parameters in photos controller
It's redirecting me correctly after the update, but it's not saving the new value.
When I reload the page, the boolean is still false.
My controller actions:
def by_zone
#photo = Photo.find_by(params[:id])
#user = #photo.user
render :index
end
def update
#photo = Photo.friendly.find(params[:id])
if #photo.update_attributes(photo_params)
redirect_to [current_user, #photo], notice: 'El spot se ha actualizado.'
else
render 'edit'
end
end
Edit
def photo_params
params.require(:photo).permit(:editor_pick,:url,:remote_photo_url,:thumbnail_cache ,:order,:string_tags,:tag_list, :sponsored, :photo, :terms, :title,:description,:category_id,:zone_id, :crop_x, :crop_y, :crop_w, :crop_h, sponsors_attributes: [:name, :description, :web, :facebook, :twitter, :sponsored_avatar])
end
Params Edit
Started PATCH "/users/enrique-isasi-12/photos/rustic-plastic-table" for 127.0.0.1 at 2015-01-14 16:43:55 +0100
Processing by PhotosController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wB2SmOT0zR0dwIIguRhzFdctY51LX333CzGYelxS4Hs=", "photo"=>{"editor_pick"=>"1"}, "commit"=>"Pick", "user_id"=>"enrique-isasi-12", "id"=>"rustic-plastic-table"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 ORDER BY "users"."id" ASC LIMIT 1
Notification Load (0.3ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 AND "notifications"."viewed_at" IS NULL ORDER BY "notifications"."created_at" DESC LIMIT 30 [["user_id", 5]]
Notification Load (0.2ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 AND (viewed_at IS NOT NULL) ORDER BY "notifications"."created_at" DESC LIMIT 30 [["user_id", 5]]
Photo Load (0.4ms) SELECT "photos".* FROM "photos" WHERE "photos"."slug" = 'rustic-plastic-table' ORDER BY "photos"."id" ASC LIMIT 1
(0.4ms) BEGIN
Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["id", 2]]
Zone Load (0.3ms) SELECT "zones".* FROM "zones" WHERE "zones"."id" = $1 ORDER BY "zones"."id" ASC LIMIT 1 [["id", 6]]
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 26], ["taggable_type", "Photo"]]
SQL (0.6ms) UPDATE "photos" SET "editor_pick" = $1, "updated_at" = $2 WHERE "photos"."id" = 26 [["editor_pick", true], ["updated_at", Wed, 14 Jan 2015 15:43:55 UTC +00:00]]
false ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 26], ["taggable_type", "Photo"]]
(6.1ms) COMMIT
Redirected to http://localhost:3000/users/enrique-isasi-4/photos/rustic-plastic-table
Completed 302 Found in 31ms (ActiveRecord: 9.7ms)

Related

Acts-as-Taggable-On showing hash object instead of tag names

Acts-as-Taggable-on is failing to correctly print out only the name column, and not the entire hash of information on the object itself. How can I setup the show page to print out each tag separately for the model? The code that I have so far is below.
show.html.erb
<%= #user.user_profile.tags.each do |tag| %>
<%= tag.name %>
<% end %>
results shown on show page
#<ActsAsTaggableOn::Tag::ActiveRecord_Associations_CollectionProxy:0x2b7534d0> games [#<ActsAsTaggableOn::Tag id: 1, name: "games", created_at: "2017-08-25 15:54:30", updated_at: "2017-08-25 15:54:30", taggings_count: 1>] 1
console output
Rendering users/show.html.erb within layouts/application
UserProfile Load (8.0ms) SELECT "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = $1 LIMIT $2 [["user_id", 5], ["LIMIT", 1]]
ActsAsTaggableOn::Tag Load (15.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."context" = $3 [["taggable_id", 2], ["taggable_type", "UserProfile"], ["context", "tags"]]
ActsAsTaggableOn::Tagging Load (2.0ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 [["taggable_id", 2], ["taggable_type", "UserProfile"]]
ActsAsTaggableOn::Tag Load (25.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 2], ["taggable_type", "UserProfile"]]
user_profile.rb
class UserProfile < ApplicationRecord
acts_as_taggable
acts_as_taggable_on :abilities
end
Try
<% #user.user_profile.tags.each do |tag| %>
<%= tag.name %>
<% end %>
(without '=' after the '<%' in the first line)

My rails controller didn't work

My rails application is used acts-as-taggable-on. I would like to add function which articles can be searched by tags.
In my articles_controller.rb
def index
#articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all
#articles = #articles.includes(:tags)
#articles = Article.page(params[:page])
end
In index.html.erb
<%= render partial:'tags',locals:{tags: article.tags}%>
In _tags.html.erb
<% tags.each do |tag| %>
<%= link_to "#{tag.name} (#{tag.taggings_count})", articles_path(tag: tag.name) %>
<%end%>
However, article wasn't scoped by tags. I confirmed that acts-as-taggable-on is working. Only tagged_with method doesn't work.
If you know any solution, please tell me.
In Active record
irb(main):001:0> Article.tagged_with("アップルパイ")
ActsAsTaggableOn::Tag Load (25.9ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('アップルパイ'))
Article Load (2.6ms) SELECT "articles".* FROM "articles" JOIN taggings articles_taggings_2902983 ON articles_taggings_2902983.taggable_id = articles.id AND articles_taggings_2902983.taggable_type = 'Article' AND articles_taggings_2902983.tag_id = 2
=> #<ActiveRecord::Relation [#<Article id: 6, title: "横浜のアップルパイ専門店", content: "横浜の赤レンガ倉庫にオープンしたアップルパイ専門のお店。写真のラズベリーソースは人気の味", created_at: "2017ge: "ras.jpg">, #<Article id: 18, title: "tagfie", content: "タグの機能を修理するためのテスト記事です。", created_at: "2017-04-05 15:41:23", updated_at: "2017-04-05 16:48:39", image: nil>, #<Article pie", content: "Apple pieの記事です", created_at: "2017-04-09 10:36:38", updated_at: "2017-04-09 10:36:38", image: nil>]>
irb(main):002:0>
However, by rails s
ActiveRecord::SchemaMigration Load (2.7ms) SELECT "schema_migrations".* FROM "schema_migrations"
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from <class:ApplicationController> at /Users/futamidaiki/Desktop/lablab/choco/app/controllers/application_controller.rb:2)
Processing by ArticlesController#index as HTML
Parameters: {"tag"=>"アップルパイ"}
ActsAsTaggableOn::Tag Load (5.8ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('アップルパイ'))
Rendering articles/index.html.erb within layouts/application
Article Load (2.7ms) SELECT "articles".* FROM "articles" LIMIT ? OFFSET ? [["LIMIT", 10], ["OFFSET", 0]]
ActsAsTaggableOn::Tag Load (3.7ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 5], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (7.8ms)
User Load (3.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 6], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (2.4ms)
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 7], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (2.8ms)
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 8], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (2.3ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 10], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.7ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 11], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.4ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 18], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.8ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 19], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.7ms)
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 20], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.8ms)
(0.2ms) SELECT COUNT(*) FROM "articles"
Rendered articles/index.html.erb within layouts/application (1827.4ms)
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('渋谷'))
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('表参道'))
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('青山'))
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('横浜'))
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('その他'))
Completed 200 OK in 3143ms (Views: 2865.4ms | ActiveRecord: 21.7ms)
It looks like you are reassigning #articles in your controller to use pagination, which is overwriting .includes from the previous line. Try:
def index
articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all
articles = articles.includes(:tags)
#articles = articles.page(params[:page])
end

Ruby on rails - Kaminari - Paginate array of search results

I have a search form with pagination. Both things work OK separately, but if I search and then load the next page, the search parameters are forgotten and kaminari shows results regardless of what search options I selected.
I have read through the docs, and other stackoverflow posts but I'm not having any luck.
My index page has
<%= search_form_for #q, html: { id: "gig_search_form" }, remote: true do |f| %>
bla bla bla...
<% end %>
<%= link_to_next_page #gigs, 'Next Page', :remote => true %>
index.js.erb
<% if params[:page] %>
$('.gig-search-list').append("<%= j render(partial: 'gigs') %>");
<% else %>
$('.gig-search-list').html("<%= j render(partial: 'gigs') %>");
<% end %>
gigs partial:
<% #gigs.each do |gig| %>
bla bla bla...
<% end %>
Now, I have tried a few tings in my controller but I'm always gettings the same results. Originally I had;
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
#q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
#allgigs = #q.result(distinct: true)
#gigs = #q.result(distinct: true).page(params[:page]).per(5)
respond_to do |format|
format.js
format.html
end
end
Then;
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
#q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
#allgigs = #q.result(distinct: true)
#gigs = Kaminari.paginate_array(#allgigs).page(params[:page]).per(5)
respond_to do |format|
format.js
format.html
end
end
Then I tried to separate them;
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
#q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
#allgigs = #q.result(distinct: true)
if #allgigs.present?
unless #allgigs.kind_of?(Array)
#gigs = #allgigs.page(params[:page]).per(5)
else
#gigs = Kaminari.paginate_array(#allgigs).page(params[:page]).per(5)
end
end
respond_to do |format|
format.js
format.html
end
end
But I get the same result each time. I can see on the console that when I load more pages, the search parameters are not included.
How can I link the two things together and paginate the search results correctly?
UPDATE:
Server log after loading 'next page' using #chaitanya's answer. I have previously added search params.
Started GET "/gigs?locale=en&page=2" for 127.0.0.1 at 2016-06-29 14:45:53 +0200
Processing by GigsController#index as JS
Parameters: {"locale"=>"en", "page"=>"2"}
Gig Load (10.3ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 [["filled", "f"]]
Gig Load (1.2ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 LIMIT 5 OFFSET 5 [["filled", "f"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 8]]
Rendered gigs/_gigs.html.erb (13.4ms)
(0.5ms) SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 [["filled", "f"]]
Rendered gigs/index.js.erb (17.2ms)
Completed 200 OK in 54ms (Views: 21.4ms | ActiveRecord: 13.0ms)
Server log when filtering with search form:
Started GET "/gigs?locale=en&utf8=%E2%9C%93&search=&q%5Bdate_gteq%5D=&q%5Bdate_lteq%5D=&q%5Bgenres_id_in%5D%5B%5D=&q%5Bsalary_cents_gteq_euros%5D=0.00&q%5Bsalary_cents_lteq_euros%5D=210.00" for 127.0.0.1 at 2016-06-29 14:45:47 +0200
Processing by GigsController#index as JS
Parameters: {"locale"=>"en", "utf8"=>"✓", "search"=>"", "q"=>{"date_gteq"=>"", "date_lteq"=>"", "genres_id_in"=>[""], "salary_cents_gteq_euros"=>"0.00", "salary_cents_lteq_euros"=>"210.00"}}
Gig Load (0.6ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) [["filled", "f"]]
Gig Load (0.5ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) LIMIT 5 OFFSET 0 [["filled", "f"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 8]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]]
Rendered gigs/_gigs.html.erb (12.2ms)
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) [["filled", "f"]]
Rendered gigs/index.js.erb (15.6ms)
Completed 200 OK in 33ms (Views: 19.9ms | ActiveRecord: 5.7ms)
try following code to carry your params to next page
= link_to_next_page #gigs, "Next Page", :remote => true, :params => params
You should add the #page and #per methods to the query itself. Kaminari.paginate_array actually takes a slice from the whole array which is not very efficient.
Here's a snippet
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).page(params[:page]).per(5).near(params[:search], 500, :order => 'distance' ).ransack(params[:q]).results
else
#q = Gig.notexpired.where(:filled => false).page(params[:page]).per(5).ransack(params[:q]).results
end
#...
Then use the Kaminari paginate helper in your view
<%= paginate #q %>

loop iterates over all items, for each item

I'm running into an issue where I rander a partial using AJAX and for some reason the line_items renders to the amount of products in it. So if there's 2 products, it loops through all line_items 2 times, causing it to be 4 elements listed instead of 2. So the loop keeps going by the amount of products in the line_items.
In my create.js.erb I have:
$('#shopping-cart').html("<%= escape_javascript render(#cart) %>");
and my _cart:
<div id="shopping-cart">
<div id="shopping-cart-header">
</div>
<div id="shopping-cart-items">
<table>
<%= render cart.line_items %>
<tr class="total-line">
<td colspan="2">Total</td>
<td class="total-cell"><%= number_to_currency(cart.total_price, unit: '') %></td>
</tr>
</table>
<%= button_to 'Empty cart', cart, method: :delete %>
</div>
<div id="shopping-cart-footer">
<%= link_to 'New order', new_order_path, class: 'btn btn-main btn-lg', data: { no_turbolink: true } %>
</div>
</div>
and the issues is with render cart.line_items I guess:
<% #cart.line_items.each do |item| %>
<% if line_item == #current_item %>
<tr id="current-item">
<% else %>
<tr>
<% end %>
<td><%= item.quantity %> ×</td>
<td>
<p><%= item.product.name %></p>
<% if item.line_item_attributes.exists? %>
<% item.line_item_attributes.each do |attribute| %>
<i><%= attribute.product_attribute.name %></i>
<% end %>
<% end %>
</td>
<td class="item-price"><%= number_to_currency(item.total_price, unit: '') %></td>
</tr>
<% end %>
if I've now added 2 products they're shown twice like this:
1 × red shirt 490.00
1 × blue shirt 89.00
1 × red shirt 490.00
1 × blue shirt 89.00
Total 579.00
Any one know what's going on? My logs are:
Started POST "/line_items" for ::1 at 2016-01-25 08:53:41 +0100
Processing by LineItemsController#create as JS
Parameters: {"utf8"=>"✓", "line_item"=>{"product_id"=>"5", "instruction"=>""}, "commit"=>"Legg til"}
Cart Load (0.4ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2 [["id", 92], ["LIMIT", 1]]
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]]
(0.3ms) SELECT COUNT(*) FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."product_id" = $2 [["cart_id", 92], ["product_id", 5]]
LineItem Load (0.4ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."product_id" = $2 [["cart_id", 92], ["product_id", 5]]
LineItemAttribute Load (0.4ms) SELECT "line_item_attributes".* FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 [["line_item_id", 132]]
LineItem Load (0.4ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."id" = $2 LIMIT $3 [["cart_id", 92], ["id", 132], ["LIMIT", 1]]
(0.2ms) BEGIN
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]]
SQL (11.6ms) UPDATE "line_items" SET "quantity" = $1, "updated_at" = $2 WHERE "line_items"."id" = $3 [["quantity", 2], ["updated_at", 2016-01-25 07:53:41 UTC], ["id", 132]]
(0.8ms) COMMIT
LineItem Exists (1.0ms) SELECT 1 AS one FROM "line_items" WHERE "line_items"."cart_id" = $1 LIMIT $2 [["cart_id", 92], ["LIMIT", 1]]
LineItem Load (0.7ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 [["cart_id", 92]]
Product Load (0.5ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]]
LineItemAttribute Exists (1.0ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]]
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]]
LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]]
LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]]
LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]]
LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]]
LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]]
LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]]
Rendered line_items/_line_item.html.erb (22.9ms)
LineItemAttribute Exists (0.5ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]]
LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]]
Rendered carts/_cart.html.erb (74.4ms)
Rendered line_items/create.js.erb (78.1ms)
It seems that _line_items are rendered, and then _cart (which again contains line_items) are rendered - maybe thats an issue?
You're correct. When you call render cart.line_items, you're rendering a collection. To quote the Rails documentation,
When you pass a collection to a partial via the :collection option, the partial will be inserted once for each member in the collection:
<h1>Products</h1>
<%= render partial: "product", collection: #products %>
(this is the same as your render cart.line_items)
And then in the partial you will have access to a product (or line_item in your case) local variable, like this:
<p>Product Name: <%= product.name %></p>
So: Rewrite your partial view to represent a single line_item with a corresponding line_item variable instead of a collection.

Rails 4 object not saving when rendering form in another model

I have a Call model nested under Contact, a Contact has many calls. In the Edit#Contact view, I'm rendering the form to create a new call. When I submit the form, params are getting submitted, but the new Call object is not saved.
Note that if I just create a new Call from the New#Call view (form not rendered in Edit#Contact view), then the Call object is saved, so it's only a problem when rendering the form in the Edit Contact view.
console output:
Started PATCH "/contacts/54" for 127.0.0.1 at 2014-12-30 15:29:05 -0500
Processing by ContactsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wyo0S3G5NHdfFr3e2DFw/ArhA7ouKdljCP5XaHMQ5Gc=", "contact"=>{"name"=>"Sample User 3", "entity"=>"Company 3", "phone"=>"888-888-8888", "alt_phone"=>"", "dead_phone"=>"", "email"=>"sample3#example.com", "alt_email"=>"", "dead_email"=>"", "body"=>""}, "call"=>{"dial_id"=>"1", "conversation_id"=>"1", "investing_id"=>"1", "timing_id"=>"1", "motivator_id"=>"1"}, "commit"=>"Save", "id"=>"54"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", 54]]
(0.1ms) begin transaction
(0.1ms) commit transaction
Contact Store (725.1ms) {"id":54,"exception":["Elasticsearch::Transport::Transport::Errors::NotFound","[404] {\"error\":\"IndexMissingException[[contacts_development] missing]\",\"status\":404}"]}
[404] {"error":"IndexMissingException[[contacts_development] missing]","status":404}
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? AND (id < 54) ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
Property Load (0.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."contact_id" = ? [["contact_id", 54]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", "54"]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", "54"]]
Dial Load (0.2ms) SELECT "dials".* FROM "dials"
Conversation Load (0.2ms) SELECT "conversations".* FROM "conversations"
Investing Load (0.2ms) SELECT "investings".* FROM "investings"
Timing Load (0.1ms) SELECT "timings".* FROM "timings"
Motivator Load (0.1ms) SELECT "motivators".* FROM "motivators"
Rendered calls/_form.html.haml (11.9ms)
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" ORDER BY updated_at DESC LIMIT 1
Contact Load (0.4ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at ASC LIMIT 1 [["user_id", 1]]
Rendered contacts/edit.html.haml within layouts/application (29.7ms)
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
Completed 200 OK in 1039ms (Views: 304.1ms | Searchkick: 725.1ms | ActiveRecord: 3.6ms)
Here is the updated form partial, rendered in contact#edit:
= form_for [Contact.find(params[:id]), Contact.find(params[:id]).calls.new], :url => url_for(controller: "Call", action: "create") do |f|
.form-group
= f.label :dial
= f.select :dial_id, options_from_collection_for_select(Dial.all, "id", "result"), class: 'form-control'
.form-group
= f.label :conversation
= f.select :conversation_id, options_from_collection_for_select(Conversation.all, "id", "result")
.form-group
= f.label :investing
= f.select :investing_id, options_from_collection_for_select(Investing.all, "id", "result")
.form-group
= f.label :timing
= f.select :timing_id, options_from_collection_for_select(Timing.all, "id", "result")
.form-group
= f.label :motivator
= f.select :motivator_id, options_from_collection_for_select(Motivator.all, "id", "result")
.actions
= f.submit 'Save', class: 'btn btn-success btn-sm'
Rendering the new call form in contact#edit view like this:
= render 'calls/form'
As a side question, I wasn't able to figure out the form_for path for this. So to tackle one problem at a time, I just have the very wordy path as a placeholder.
Here is the edit method in the Contacts Controller:
def edit
#contact = Contact.find(params[:id])
#user = #contact.user
#new_call = #contact.calls.build
authorize #contact
end
create method in the Call Controller:
def create
#contact = Contact.find(params[:contact_id])
#call = Call.new(call_params)
#call.contact_id = #contact.id
#call.user_id = current_user.id
if #call.save
flash[:notice] = "Your Call was successfully posted."
redirect_to edit_contact_path(#contact)
else
flash[:error] = "Your Call was not posted. Please try again."
redirect_to edit_contact_path(#contact)
end
end
routes.rb:
resources :contacts do
resources :properties
resources :calls
collection do
post :import
get :database
end
end

Resources