loop iterates over all items, for each item - ruby-on-rails

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.

Related

I have a NoMethodError in one of my controllers

Just added a new comment feature to my rails app and stumbled upon this issue. I'm getting a NoMethodError in Pics#show which comes from my _comment.html.haml partial.
I can see the comment thread on my show page but after I post the comment I'm getting this. Can go back to uncommented pics, but can't go to the ones I've already commented on.
I've been staring at the code for a while now and I need a fresh pair of eyes.
Started GET "/pics/14" for 127.0.0.1 at 2018-12-19 23:44:51 +0000
(0.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Users/alexfurtuna/.rvm/gems/ruby-2.4.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by PicsController#show as HTML
Parameters: {"id"=>"14"}
Pic Load (0.4ms) SELECT "pics".* FROM "pics" WHERE "pics"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
↳ app/controllers/pics_controller.rb:53
CACHE Pic Load (0.0ms) SELECT "pics".* FROM "pics" WHERE "pics"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
↳ app/controllers/pics_controller.rb:9
Rendering pics/show.html.haml within layouts/application
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/views/pics/show.html.haml:14
(0.7ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 [["votable_id", 14], ["votable_type", "Pic"], ["vote_flag", true]]
↳ app/views/pics/show.html.haml:20
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 14], ["commentable_type", "Pic"]]
↳ app/views/pics/show.html.haml:28
Rendered collection of comments/_comment.html.haml [1 times] (153.0ms)
Rendered pics/show.html.haml within layouts/application (220.1ms)
Completed 500 Internal Server Error in 267ms (ActiveRecord: 13.0ms)
ActionView::Template::Error (undefined method `comment_comments_path' for #<#. <Class:0x007fa535ee08b8>:0x007fa535ee9d50>
Did you mean? pic_comment_comments_path):
2: = comment.body
3: %small
4: Submitted #{time_ago_in_words(comment.created_at)} ago
5: = form_for [comment, Comment.new] do |f|
6: = f.text_area :body, placeholder: "Add a Reply"
7: %br/
8: = f.submit "Reply"
app/views/comments/_comment.html.haml:5:in `_app_views_comments__comment_html_haml___2866288612795974586_70173756400620'
app/views/pics/show.html.haml:28:in `_app_views_pics_show_html_haml___2738689311384334047_70173775563600'
And this is my _comment partial.
%li
= comment.body
%small
Submitted #{time_ago_in_words(comment.created_at)} ago
= form_for [comment, Comment.new] do |f|
= f.text_area :body, placeholder: "Add a Reply"
%br/
= f.submit "Reply"
%ul
= render partial: 'comments/comment', collection: comment.comments
Everything is fixed now and working properly. Had to pass the #pic variable. Thanks

Unending/non-terminating database query - Rails

I want to implement a way of clicking a button and running a method without having to switch to the view of the specifid item/object found by id. I'm using PostgrSQL.
In my PaymentsController I have a method called withdraw, which looks something like this:
def withdraw
#payment = Payment.find(params[:id])
#payment.withdrawn = true
#payment.amount_interest = withdraw.amount * 1.1
#payment.save
head :ok
end
And the route:
patch '/withdraw/:id', to: 'payments#withdraw', as: :withdraw
each statement with link_to:
<% #payments.each do |withdraw| %>
<tr>
<td><%= withdraw.amount %></td>
<td><%= link_to 'Withdraw', withdraw_path(withdraw), method: :patch, remote: true %></td>
</tr>
<% end %>
EDIT: Upon clicking "Withdraw" I get an unending database query that looks like this:
Started PATCH "/withdraw/1" for 127.0.0.1 at 2016-11-11 21:20:34 +0200
Processing by PaymentsController#withdraw as JS
Parameters: {"id"=>"1"}
Started PATCH "/withdraw/1" for 127.0.0.1 at 2016-11-11 21:20:34 +0200
Processing by PaymentsController#withdraw as JS
Member Load (40.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = $1 ORDER BY "members"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Parameters: {"id"=>"1"}
Member Load (1.4ms) SELECT "members".* FROM "members" WHERE "members"."id" = $1 ORDER BY "members"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Payment Load (0.0ms) SELECT "payments".* FROM "payments" WHERE "payments"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Payment Load (2.0ms) SELECT "payments".* FROM "payments" WHERE "payments"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Payment Load (1.1ms) SELECT "payments".* FROM "payments" WHERE "payments"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Payment Load (0.0ms) SELECT "payments".* FROM "payments" WHERE "payments"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
My Payments model has the following:
account:string amount:decimal paid:boolean withdrawn:boolean amount_interest:decimal
belongs_to :member
Any assistance will be greatly appreciated.
The issue was a bug in my withdraw method.
changed:
#payment.amount_interest = withdraw.amount * 1.1
to:
#payment.amount_interest = #payment.amount * 1.1

Using the cache count for acts_as_votable

I have a view that is iterating through a series of posts, and is running a count on the votes for each. I know how to do eager loading generally and how to use cache counters generally. But I can't figure out how to use the cache counter that comes with acts_as_votable (or I'm doing something else wrong.)
View:
<span class="votes">
<% if current_user.voted_for? link %>
<%= link_to like_link_path(link), class: "likes active", id: "link-#{link.id}", remote: true, method: :put do %>
<i class="fa fa-heart"></i> <%= link.cached_votes_total.to_s %>
<% end %>
<% else %>
<%= link_to like_link_path(link), class: "likes", id: "link-#{link.id}", remote: true, method: :put do %>
<i class="fa fa-heart-o"></i> <%= link.cached_votes_total.to_s %>
<% end %>
<% end %>
</span>
Terminal:
Link Load (0.4ms) SELECT "links".* FROM "links" ORDER BY "links"."score" DESC LIMIT 10 OFFSET 0
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.2ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."voter_id" = $1 AND "votes"."voter_type" = $2 AND "votes"."votable_id" = $3 AND "votes"."votable_type" = $4 AND "votes"."vote_scope" IS NULL [["voter_id", 1], ["voter_type", "User"], ["votable_id", 1], ["votable_type", "Link"]]
(0.2ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."voter_id" = $1 AND "votes"."voter_type" = $2 AND "votes"."votable_id" = $3 AND "votes"."votable_type" = $4 AND "votes"."vote_scope" IS NULL [["voter_id", 1], ["voter_type", "User"], ["votable_id", 2], ["votable_type", "Link"]]
Rendered home/index.html.erb within layouts/application (24.6ms)
Completed 200 OK in 60ms (Views: 56.9ms | ActiveRecord: 1.6ms)
Thanks!
It looked nothing abnormal to me. The voted_for? method is alias of voted_on?. Keep on reading the code you will find this method calls a votes.size > 0, which will trigger following queries to database. (I guess you have 2 links on your page, right?) So it has nothing to do with the cached counts fields.
(0.2ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."voter_id" = $1 AND "votes"."voter_type" = $2 AND "votes"."votable_id" = $3 AND "votes"."votable_type" = $4 AND "votes"."vote_scope" IS NULL [["voter_id", 1], ["voter_type", "User"], ["votable_id", 1], ["votable_type", "Link"]]
(0.2ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."voter_id" = $1 AND "votes"."voter_type" = $2 AND "votes"."votable_id" = $3 AND "votes"."votable_type" = $4 AND "votes"."vote_scope" IS NULL [["voter_id", 1], ["voter_type", "User"], ["votable_id", 2], ["votable_type", "Link"]]
P.S. The code is here: github: voter.rb.
May be that is not the best way, but it could help to preload user's votes for links in single database hit:
Somewhere in backend (e.g. User model) you may define method like
# votable - records, which were voted by user, links in the case
def preloaded_votes(votable)
#preloaded_votes ||= Vote.where(voter: self, votable: votable).group(:votable_id).count
end
this method will return hash like {votable_id1 => count1, votable_id2 => count2 }, where votable_idN means id of an element from votable argument for the method.
Change current call current_user.voted_for? link to preloaded counts current_user.preloaded_votes.key?(link.id)
This solution adds one more database hit, but allow to avoid N+1 queries. Also, additional SQL does not contain any tables joins. One possible way to improve here - add special index for this kind of queries.

Can't get the value stored when updating attributes

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)

No error is shown but return result is empty in index

i'm trying to create a Result which belongs to Generator. In order to create the Result, I did the code in Generator show.html.erb and render the form ( of Result ) in the show.html.erb of Generator. However, Result method is not called when i click the submt button. How can i solve this ? There are no errors shown either so i do not know where goes wrong. When i type Result.all in console, no records were in the db as well. I followed the way of associating 2nd object with the 1st but still can't get them right. Please help me .
Log ( no idea why it's so long. Definately there's something gone wrong)
Started GET "/users/1/generators/new" for 127.0.0.1 at 2013-12-13 23:36:51 +0800
Processing by GeneratorsController#new as HTML
Parameters: {"user_id"=>"1"}
[1m[35mUser Load (2.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered generators/_form.html.erb (77.0ms)
Rendered generators/new.html.erb within layouts/application (82.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 200 OK in 256ms (Views: 179.0ms | ActiveRecord: 15.0ms)
Started POST "/users/1/generators" for 127.0.0.1 at 2013-12-13 23:36:56 +0800
Processing by GeneratorsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SBzLTWzzkziEGPDL7BZFnxdKuVeBJXQXUpKta3k5erQ=", "choice"=>"Randomly", "generator"=>{"primer_length"=>"10"}, "no_A"=>"", "no_T"=>"", "no_G"=>"", "no_C"=>"", "user_seq"=>"", "result_choice"=>"Yes", "commit"=>"Generate", "user_id"=>"1"}
[1m[36mUser Load (2.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36m (2.0ms)[0m [1mBEGIN[0m
[1m[35mSQL (3.0ms)[0m INSERT INTO "generators" ("choice", "created_at", "f_primer", "melting_temp", "primer_length", "r_primer", "random_primer_generated", "result_choice", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["choice", "Randomly"], ["created_at", Fri, 13 Dec 2013 23:36:56 MYT +08:00], ["f_primer", "GTGCTAAAAT"], ["melting_temp", 26.0], ["primer_length", 10], ["r_primer", "ATTTTAGCAC"], ["random_primer_generated", "GTGCTAAAAT"], ["result_choice", "Yes"], ["updated_at", Fri, 13 Dec 2013 23:36:56 MYT +08:00], ["user_id", 1]]
[1m[36m (103.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/users/1/generators/5
Completed 302 Found in 136ms (ActiveRecord: 110.0ms)
Started GET "/users/1/generators/5" for 127.0.0.1 at 2013-12-13 23:36:56 +0800
Processing by GeneratorsController#show as HTML
Parameters: {"user_id"=>"1", "id"=>"5"}
[1m[35mUser Load (2.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mGenerator Load (2.0ms)[0m [1mSELECT "generators".* FROM "generators" WHERE "generators"."id" = $1 LIMIT 1[0m [["id", "5"]]
[1m[35mResult Load (2.0ms)[0m SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1 [["generator_id", 5]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered results/_form.html.erb (79.0ms)
Rendered generators/show.html.erb within layouts/application (85.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 200 OK in 140ms (Views: 119.0ms | ActiveRecord: 14.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/generators.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/identities.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/results.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/welcome.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/generators.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/identities.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/registrations.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/results.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started GET "/assets/welcome.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started PUT "/users/1/generators/5?generator_id=5" for 127.0.0.1 at 2013-12-13 23:37:08 +0800
Processing by GeneratorsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SBzLTWzzkziEGPDL7BZFnxdKuVeBJXQXUpKta3k5erQ=", "ncbi_ref_seq"=>"", "genome_seq"=>"TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG", "commit"=>"Analyze", "generator_id"=>"5", "user_id"=>"1", "id"=>"5"}
[1m[36mUser Load (2.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mGenerator Load (2.0ms)[0m SELECT "generators".* FROM "generators" WHERE "generators"."id" = $1 LIMIT 1 [["id", "5"]]
[1m[36m (2.0ms)[0m [1mBEGIN[0m
[1m[35m (2.0ms)[0m COMMIT
[1m[36m (1.0ms)[0m [1mBEGIN[0m
[1m[35m (2.0ms)[0m COMMIT
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Redirected to http://localhost:3000/users/1/generators.5?id=5
Completed 302 Found in 27ms (ActiveRecord: 11.0ms)
Started GET "/users/1/generators.5?id=5" for 127.0.0.1 at 2013-12-13 23:37:08 +0800
Processing by GeneratorsController#index as
Parameters: {"id"=>"5", "user_id"=>"1"}
[1m[35mUser Load (2.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mGenerator Load (2.0ms)[0m SELECT "generators".* FROM "generators" WHERE "generators"."user_id" = $1 [["user_id", 1]]
[1m[36mResult Load (1.0ms)[0m [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m [["generator_id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mResult Load (2.0ms)[0m SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1 [["generator_id", 2]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mResult Load (2.0ms)[0m [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m [["generator_id", 3]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mResult Load (2.0ms)[0m SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1 [["generator_id", 4]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mResult Load (2.0ms)[0m [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m [["generator_id", 5]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered generators/index.html.erb within layouts/application (61.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 200 OK in 128ms (Views: 111.0ms | ActiveRecord: 13.0ms)
Generator controller
def show
end
# GET /generators/new
def new
#generator=Generator.new(params[:generator])
end
def index
##generators = current_user.generators
##generators = #generators.order("created_at DESC")
end
def create
#generator = current_user.generators.build(generator_params)
#generator.choice = params[:choice]
if params[:choice] == 'Randomly'
#generator.random_generate(generator_params)
elsif params[:choice] == 'Specified ATGC'
#generator.specified_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
elsif params[:choice] == 'Seating'
#generator.seating(params[:user_seq])
end
#generator.result_choice=params[:result_choice]
#generator.save
respond_to do |format|
if #generator.result_choice == 'Yes'
format.html { redirect_to(:action =>"show",:id =>#generator.id )}
else
format.html { redirect_to(user_generators_path(#generator,:user_id => current_user.id,:generator_id=>#generator.id) ) }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(params[:generator])
#generator.save
format.html { redirect_to(user_generators_path(#generator,:user_id => current_user.id,:id=>#generator.id) ) }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
Result controller
def new
end
# GET /results/1/edit
# POST /results
# POST /results.json TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG
def create
#result = #generator.build_result(result_params)
#generator=#result.generate_result(result_params)
#generator.result.save
redirect_to user_generators_path
end
Result Form that was rendered in Generator show.html.erb
<%= form_for(#generator.build_result(params[:result]),:url =>user_generator_path(#generator,:user_id => current_user.id, :generator_id => #generator.id),:html =>{:method=>:put}) do |f| %>
<script>
$(document).ready(function(){
$("#accession_number").click(function(){
$("#FASTA").hide();
$("#highlight").hide();
})
$("#FASTA").click(function(){
$("#accession_number").hide();
$("#highlight").hide();
})
$("#reset").click(function(){
$("#accession_number").show();
$("#FASTA").show();
$("#highlight").show();
})
});
</script>
<font size ="6">*** <u>Choose ONE of the input methods</u> *** </font>
<p id="accession_number">
<label><strong><font size ="3">NCBI accession number:</font></strong></label><br />
<%= text_field_tag(:ncbi_ref_seq,nil,placeholder: 'Accession Number')%>
</p>
<p id="highlight"><font size ="5">
OR
</font></p>
<p id="FASTA">
<strong><font size ="3">Paste your sequence here: ( FASTA format ) :</font></strong>
<%= text_area_tag(:genome_seq,nil,size: "50x10",placeholder: 'Input your sequence here')%>
</p>
<input type="reset" value="Reset" id="reset">
<%= submit_tag 'Analyze' %>
<% end %>
Form for Generator ( i need to use tag else if i use f.button , i get empty result)
<html>
<script>
$(document).ready(function() {
var total = 0;
$.each($(':input'), function(){
total += parseInt($(this).val(), 10); //use radix 10 to ensure correct parseInt val
});
$("span").text(i);
});
</script>
<body >
<%= form_for(#generator, :url => user_generators_path(:user_id => current_user.id, :id => #generator.id)) do |f| %>
<% if #generator.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#generator.errors.count, "error") %> prohibited this generator from being saved:</h2>
<ul>
<% #generator.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% else %>
<fieldset class ="primer">
<legend><strong>Method Use :</strong></legend><br>
<h3 align="left"><font size ="5"><b>Step 1: <u>Choose only ONE of the methods</u></font></b></h3>
<table class="p_gen">
<thead>
<tr>
<th class="method1"><label>Randomly</label><br />
<%= radio_button_tag(:choice, 'Randomly',checked:true )%>
<p> ( Generate a primer randomly )</p></th>
<th class="method2"><label>Specified ATGC</label><br />
<%= radio_button_tag(:choice,'Specified ATGC')%>
<p> ( Generate a primer with number of A,T,G and C )</p></th>
<th class="method3"><label>Seating</label><br />
<%= radio_button_tag(:choice,'Seating')%>
<p> ( Generate a primer according to your preference )</p></th>
</tr>
</thead>
<tbody>
<tr>
<td class="method1">
<p> Input length of the primer you want : </p>
<label>Primer Length :</label>
<%= f.number_field(:primer_length , min: 6 , max: 35)%>
</td>
<td class="method2">
<p>Input the number of each base the primer should have</p>
<label>Number of A :</label>
<%= number_field_tag :no_A,nil, :min=>0, :max=>35 %><br />
<label>Number of T :</label>
<%= number_field_tag :no_T,nil, :min=>0, :max=>35 %><br />
<label>Number of G :</label>
<%= number_field_tag :no_G,nil, :min=>0, :max=>35 %><br />
<label>Number of C :</label>
<%= number_field_tag :no_C,nil, :min=>0, :max=>35 %><br />
Total bases:<span></span>
</td>
<td class="method3">
<p> Input your preference sequence (only IUPAC nucleotide).</p>
<p><b><u>IUPAC Nucleotide :</u></b></p>
<p>A,T,G,C,R,Y,S,W,K,M,B,D,H,V,N </p>
Example: <br />
Preference primer = TAGGCT<b>N</b>TTA<b>N</b>GAC<b>N</b> <br />
N = Any base ( A/ T / G / C) <br /><br />
<label>Desired sequence :</label><br>
<%= text_field_tag(:user_seq,nil,:maxlength => 35)%>
</td>
</tr>
</tbody>
</table>
</fieldset>
<br>
<fieldset class ="sample">
<h4><font size="5"><b>Step 2: <u>Choose 'Yes' if you want to input reference sequence for Binding-time analysis </font></u></b></h4>
<legend><strong>Do you have NCBI data to extract / FASTA file to input?</strong></legend><br>
<label>Yes</label>
<%= radio_button_tag(:result_choice,'Yes')%>
<label>No</label>
<%= radio_button_tag(:result_choice,'No')%>
<br>
</fieldset>
<br><div class = "button">
<%=f.submit("Generate", :class => "Gbutton_class") %>
</div>
<%end %>
<%end%>
</body>
</html>
routes.rb
Project::Application.routes.draw do
get "/signout" => "sessions#destroy", :as => :signout
root :to => 'welcome#index'
get '/auth/:provider/callback' => 'sessions#create'
post '/auth/identity/callback' => 'sessions#create'
resources :users do
resources :generators
resources :results
end
resources :identities
I'm not entirely clear why there are two Generator forms (perhaps I'm missing something), but in some places you're using the singular user: user_generators_path where in others you're using users_generators_path (note the plural users). Check which is correct via rake routes, and fix any incorrect entries.

Resources