I'm trying to render the following partial:
<% content_for :admin_content do %>
<h3>Listing All Accounts</h3>
<%= paginate (#accounts) %>
<table id="indexTable" class="table table-striped">
...
</table>
<%= paginate (#accounts) %>
<br>
<%= link_to 'New Account', new_account_path %>
<% end %>
(there are other similar partials as well)
... in the following layout:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<%= render 'navigation' %>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<main>
<%= render partial: 'admin_navigation' %>
<%= yield :admin_content %>
</main>
<footer>
...
</footer>
</body>
</html>
The reason I'm trying to use content_for is that I have a couple of other pages similar to the above partial that I want to render when a link is clicked in the navigation:
<div id="panel">
<div class="navbar subnav navbar-inverse admin-nav" role="navigation">
<div class="navbar-inner">
<div class="container-fluid">
<h2>Admin Dashboard</h2>
<ul class="pager subnav-pager">
<div class="btn-group btn-group-justified" role="group" aria-label="navigation">
<span role=button><%= link_to "Manage Accounts", {:action=>"manage_accounts"}, :class => "btn btn-primary" %></span>
<span role=button><%= link_to "Manage Customers", {:action=>"manage_customers"}, :class => "btn btn-primary" %></span>
<span role=button><%= link_to "Manage Transactions", {:action=>"manage_acct_transactions"}, :class => "btn btn-primary" %></span>
</div>
</ul>
</div>
</div>
</nav>
</div>
The above links call respective methods in the controller, which are as follows:
def manage_accounts
#accounts = Account.order('id').page(params[:page]).per(15)
render partial: 'manage_accounts'
end
def manage_customers
#customers = Customer.order('lastname').page(params[:page]).per(15)
render partial: 'manage_customers'
end
def manage_acct_transactions
#acct_transactions = AcctTransaction.order('date DESC').page(params[:page]).per(15)
render partial: 'manage_acct_transactions'
end
But when you click the links in navigation, it just shows a blank page with no html at all. The console says that the objects are loaded as per the controller, and that the partial is being rendered. But where is it?
I've tried all kinds of ways to get this to work. The closest I got was actually without the content_for/yield and just calling the method, which of course rendered the partial as if it were its own page (bad). There used to be something called replace_html which would probably work for what I'm trying to do but I'm using Rails 4.1.8.
Initially, I set this navigation up with AJAX, but it just doesn't work here. These partials have links for CRUD actions as well as redirects. Using AJAX led to all kinds of CSRF errors and seems like it adds an unnecessary layer of complexity for something that should be pretty simple.
Why does the above code not render anything?
Am I taking the wrong approach?
Thanks
EDIT: adding logs for the page request..
Started GET "/administrators/145a435c-6632-4d54-aca3-5e834b9e2d41/adminview" for 127.0.0.1 at 2015-04-01 07:58:41 -0400
Processing by AdministratorsController#adminview as HTML
Parameters: {"id"=>"145a435c-6632-4d54-aca3-5e834b9e2d41"}
[1m[36mUser Load (0.6ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = x'145a435c66324d54aca35e834b9e2d41' ORDER BY `users`.`id` ASC LIMIT 1[0m
Rendered administrators/adminview.html.erb within layouts/admin (13.7ms)
Rendered application/_navigation.html.erb (1.4ms)
Rendered application/_admin_navigation.html.erb (1.8ms)
Completed 200 OK in 212ms (Views: 206.9ms | ActiveRecord: 0.6ms)
Started GET "/administrators/145a435c-6632-4d54-aca3-5e834b9e2d41/manage_accounts" for 127.0.0.1 at 2015-04-01 07:58:45 -0400
Processing by AdministratorsController#manage_accounts as HTML
Parameters: {"id"=>"145a435c-6632-4d54-aca3-5e834b9e2d41"}
[1m[35mUser Load (0.6ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = x'145a435c66324d54aca35e834b9e2d41' ORDER BY `users`.`id` ASC LIMIT 1
[1m[36m (49.0ms)[0m [1mSELECT COUNT(*) FROM `accounts`[0m
[1m[35mAccount Load (11.8ms)[0m SELECT `accounts`.* FROM `accounts` ORDER BY id LIMIT 15 OFFSET 0
[1m[36mCustomer Load (12.7ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 490181591 LIMIT 1[0m
[1m[35mCustomer Load (0.7ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 189365990 LIMIT 1
[1m[36mCustomer Load (0.5ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 24420774 LIMIT 1[0m
[1m[35mCustomer Load (0.5ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 772684056 LIMIT 1
[1m[36mCustomer Load (0.5ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 862455622 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 417734356 LIMIT 1
[1m[36mCustomer Load (0.4ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 220490343 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 685817728 LIMIT 1
[1m[36mCustomer Load (0.4ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 53991993 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 676540929 LIMIT 1
[1m[36mCustomer Load (0.4ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 726000565 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 554199658 LIMIT 1
[1m[36mCustomer Load (0.6ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 767280416 LIMIT 1[0m
[1m[35mCustomer Load (0.6ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 237301229 LIMIT 1
[1m[36mCustomer Load (0.6ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 209732030 LIMIT 1[0m
Rendered administrators/_manage_accounts.html.erb (194.7ms)
Completed 200 OK in 217ms (Views: 132.9ms | ActiveRecord: 80.8ms)
Also, here is the routes.rb if that helps solve this problem..
Rails.application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
post "/accounts/adminview" => "devise/sessions#new"
end
root 'home#index'
get 'home/about'
get 'home/help'
get 'accounts/add_account', to: 'accounts#new'
post 'accounts/add_account', to: 'accounts#create'
resources :administrators do
member do
get :adminview
get :manage_accounts
get :manage_customers
get :manage_acct_transactions
end
end
resources :users do
resource :customers
resource :accounts
resource :addresses
end
resources :accounts do
resource :acct_transactions
end
resources :account_types, :accounts, :addresses, :administrators, :customers, :transaction_types, :acct_transactions, :users
end
Again, according to logs, it supposedly is loading the partial _manage_accounts, but I'm only seeing a blank page. I've tried using path helper but it throws a "Missing Template" error. Have tried lots of other things, instead of the action method but I still get the same result.
Why are you rendering partials as the views for your actions?
def manage_accounts
#accounts = Account.order('id').page(params[:page]).per(15)
render partial: 'manage_accounts'
end
Calling render partial like this will only render the content of the partial and will not load the layout. If you want the layout (and it certainly sounds like you do) then rename this file to a normal view app/views/administrators/manage_accounts.html.erb and then remove the render call from your action altogether.
I would also advise splitting each of these manage routes out into their own controllers, so instead you would have app/views/admin/accounts/index.html.erb, which would become the new view to manage accounts. I suggest this because it falls in line with the more traditional CRUD design of a Rails application.
Related
There are two questions.
First, I want to enable an action to update successfully.
Now I don't know why I can't update successfully.
Second,
I want to edit and update a column of existing record, and I found that if I make an action get and correspond to an action which acts as a put action, the get action is recognized as an existence of edit,
I don't hope this, so how can I fix it?
view split_now.html.erb
<div class="container">
<h1>Split</h1>
<%= form_for #currency, url: { action: 'split', controller: 'currencies'}, method: :put do |f|%>
<% if #currency.errors.any? %>
<div id="error_explanation" class="alert alert-danger">
<h2><%= #currency.errors.count %>error(s) exist(s).</h2>
<ul>
<% #currency.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>amount whatever you want to issue like 100</p>
<div class="input-group">
<%= f.number_field :amount,:class=>'form-control form-group',placeholder:"amount",min:"1",step:"1",required:true%>
</div>
<%= f.submit 'done', class: "btn btn-large bg-info" %>
<% end %>
</div>
currencies_contoroller.rb
def split_now
#currency=Currency.find_by_slug(params[:id])
end
def split
#currency=Currency.find_by_slug(params[:id])
if CurrencyUser.where(currency_id:#currency.id,owner:true).first.user==current_user
issue_more(params,#currency)
else
redirect_to #currency,alert:"This currency wasn't issued by you"
end
end
def issue_more(params,currency)
result=false
current_amount=currency.amount
result=currency.increment("amount",params[:amount].to_i)
currency_user=CurrencyUser.find_by(currency_id:currency.id,owner:true)
currency_user.increment("amount",params[:amount].to_i)
return result
end`
routes
resources :currencies do
member do
put :split
get :split_now
end
end
Started PUT "/currencies/hello/split" for 127.0.0.1 at 2018-03-30 23:50:42 +0900
Processing by CurrenciesController#split as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"zZHYVV3AqI6fiqtY47XJTP1iOte1gRzwSPw4NEHHtfWnf0QHNrRnS1fzD+KCIrAm93eKROb1YynGmcwlhx6Vtw==", "currency"=>{"amount"=>"1"}, "commit"=>"done", "
id"=>"hello"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Currency Load (0.5ms) SELECT "currencies".* FROM "currencies" WHERE "currencies"."slug" = $1 LIMIT $2 [["slug", "hello"], ["LIMIT", 1]]
CurrencyUser Load (0.7ms) SELECT "currency_users".* FROM "currency_users" WHERE "currency_users"."currency_id" = $1 AND "currency_users"."owner" = $2 ORDER BY "currency_users"."id" ASC LIMIT $3
[["currency_id", 2], ["owner", "t"], ["LIMIT", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
CurrencyUser Load (0.3ms) SELECT "currency_users".* FROM "currency_users" WHERE "currency_users"."currency_id" = $1 AND "currency_users"."owner" = $2 LIMIT $3 [["currency_id", 2], ["owner", "t"], ["LIMIT", 1]]
Rendering currencies/split.html.erb within layouts/application
Rendered currencies/split.html.erb within layouts/application (0.7ms)
Rendered layouts/_header.html.erb (2.4ms)
Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 181ms (Views: 145.3ms | ActiveRecord: 2.8ms)
I'm trying to make a search kick results page. However when i click search the console states i've got the correct id but the results page isnt opening.
I have this for my controller
def search
#events = Newevent.search params[:search]
render 'events/results'
end
I also have this as my search form
<div class="input-group input-group-lg">
<%= text_field_tag :search, params[:search], class: "form-control", autocomplete: "off" %>
<div class="input-group-btn">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
</div>
<% end %>
And this is inside the results html.erb
<% #events.each do |event| %>
<h1>Test</h1>
<%= event.eventname %>
<% end %>
Heres what the console states when i start the search
Started GET "/search_events?utf8=%E2%9C%93&search=capital&commit=Search" for ::1 at 2015-11-21 15:18:33 +0000
Processing by NeweventsController#search as JS
Parameters: {"utf8"=>"✓", "search"=>"capital", "commit"=>"Search"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Newevent Search (9.8ms) curl http://localhost:9200/newevents_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"capital","operator":"and","boost":10,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"capital","operator":"and","boost":10,"analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"capital","operator":"and","boost":1,"analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"_all":{"query":"capital","operator":"and","boost":1,"analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"size":100000,"from":0,"fields":[]}'
Newevent Load (0.5ms) SELECT "newevents".* FROM "newevents" WHERE "newevents"."id" = 20
Rendered events/results.html.erb within layouts/application (1.6ms)
Rendered layouts/_navigation.html.erb (0.1ms)
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 93ms (Views: 79.5ms | Searchkick: 9.8ms | ActiveRecord: 1.0ms)
Base on your server's log, I think you setup your search form as a remote form (you will see you have remote: true in your form_for tag)
Processing by NeweventsController#search as JS
Do you see text: as JS in the above line. That's because you submit a remote form. That means when you submit your form, the form will call ajax to server wait for server to return a js code to run on client side.
To solve your problem, just remove remote: true in your form_for tag, and everything will be ok ;)
So for some reason I'm getting a "invalid-request-cookie" when trying to submit a ReCaptcha. I'm using the ReCaptcha gem.
What am I doing wrong?
Also, I have my ReCaptcha public and private keys in my bash_profile so that shouldn't be an issue.
UsersStepsController.rb
class UserStepsController < ApplicationController
def show
end
def update
#user = current_user
captcha_message = "The data you entered for the CAPTCHA wasn't correct. Please try again"
if !verify_recaptcha(message: captcha_message)
render :add_recaptcha
else
redirect_to root_path and return
end
end
def add_recaptcha
#user = current_user
end
end
Routes.rb
patch '/user_steps', to: 'user_steps#update', as: 'update_user_steps'
resources :user_steps, except: [:update]
get '/add_recaptcha', to: 'user_steps#add_recaptcha'
user_steps/add_captcha.html.erb
<fieldset class="clearfix">
<div class="g-recaptcha" data-sitekey="6LeUQ_xxxx_etc"></div>
<div align="center"><%= recaptcha_tags :public_key => ENV['RECAPTCHA_PUBLIC_KEY'] %></div>
<%= form_for(#user, {url: update_user_steps_path(id: #user.to_param)}) do |f| %>
<br>
<%= f.submit "Done", class: "styling" %>
<% end %>
</div>
</fieldset>
Update Logs:
Started PATCH "/user_steps?id=35" for 127.0.0.1 at 2014-11-26 11:55:52 -0500
Started PATCH "/user_steps?id=35" for 127.0.0.1 at 2014-11-26 11:55:52 -0500
Processing by UserStepsController#update as HTML
Processing by UserStepsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sEOPI8KVuXLPSLjXVajF8QcWeLcUpibgHq6hHqgtwGA=", "commit"=>"Done", "id"=>"35"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sEOPI8KVuXLPSLjXVajF8QcWeLcUpibgHq6hHqgtwGA=", "commit"=>"Done", "id"=>"35"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 35 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 35 ORDER BY "users"."id" ASC LIMIT 1
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/headers/_header_sign_up_step2.html.erb (0.2ms)
Rendered layouts/headers/_header_sign_up_step2.html.erb (0.2ms)
Rendered user_steps/add_recaptcha.html.erb within layouts/application (28.2ms)
Rendered user_steps/add_recaptcha.html.erb within layouts/application (28.2ms)
Completed 200 OK in 919ms (Views: 800.4ms | ActiveRecord: 0.8ms)
Completed 200 OK in 919ms (Views: 800.4ms | ActiveRecord: 0.8ms)
Here is your answer :) You need to include that inside your form unless the value won't be saved!
<fieldset class="clearfix">
<%= form_for(#user, {url: update_user_steps_path(id: #user.to_param)}) do |f| %>
<div align="center"><%= recaptcha_tags :public_key => ENV['RECAPTCHA_PUBLIC_KEY'] %></div>
<br>
<%= f.submit "Done", class: "styling" %>
<% end %>
</div>
</fieldset>
I have an index page that builds a table, and I am trying to allow users to edit line's in the table. I am trying to do this in the most basic way possible - no javascript, ajax, etc, unless Rails is supplying it.
I have my table displaying fine in the index method, and there is a form as the last row in the table that can be used to add a new row. The new form works fine. Each row has an edit link that routes to the controller's edit method. The controller sets the object to be edited, and renders index, this time with a form in the row that is to be edited. My problem is that this form will not submit, but if I refresh the page it will submit.
The fact that the page will submit after a refresh is very confusing. I don't see how a refresh would do anything different then clicking the link (it should still go through the same routing, with the same variables right?) and I can't see any difference in the form html before and after the refresh. Any have ideas on what might be happening?
I am not sure what code to even start looking in, but here goes;
index.html.erb
...
<tbody>
<% #boms.each do |line| %>
<% if line == #bom %>
<%= render("form_in_table", form_objects: #bom , button_text: "Update") %>
<% else %>
<%= render("bom_in_table", line: line) %>
<% end %>
<% end %>
<% if #bom.new_record? %>
<%= render("form_in_table", form_objects: [#li, #bom] , button_text: "Add") %>
<% end %>
</tbody>
...
_form_in_table.html.erb
<%= form_for(form_objects, html: {class: "form-in-table"}) do |f| %>
<tr>
<td><%= f.text_field :quantity %></td>
<td colspan="2">
<%= f.select(:part_id,
options_from_collection_for_select(#parts, :id, :pricebook_name),
prompt: "Select a Part",) %></td>
<td></td>
<td></td>
<td></td>
<td><%= f.submit(button_text, class: "btn btn-primary btn-mini") %></td>
</tr>
<% end %>
_bom_in_table.html.erb
<tr>
<td><%= line.quantity%></td>
<td><%= line.part_number %></td>
<td><%= line.part_description %></td>
<td><%= number_to_currency(line.part_cost) %></td>
<td><%= line.part_unit %></td>
<td><%= number_to_currency(line.extension) %></td>
<td><%= link_to('Edit', edit_bom_path(line)) %></td>
</tr>
boms_controller.rb
...
def edit
#bom = Bom.find(params[:id])
#li = #bom.line_item
#boms = #li.boms.sorted_by_part_number
#parts = Part.sorted_by_number
render 'index'
end
...
In case this is useful to deciphering the code/intent, I have collections of line_items, parts, and boms; line_item has many boms, and line_item has many parts through boms. In addition to the part/line item relationship, boms have a quantity. Bom is short for bill of materials. #li is the line_item that is being manipulated. The form I having trouble with is for viewing/adding/editing the collection of boms (quantitys and parts) that belong to a line item.
ADDING LOGS
Started GET "/line_items/8/boms" for 127.0.0.1 at 2013-10-14 14:27:27 -0400
Processing by BomsController#index as HTML
Parameters: {"line_item_id"=>"8"}
[1m[35mLineItem Load (0.0ms)[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1 [["id", "8"]]
[1m[36mLineItemSubClass Load (1.0ms)[0m [1mSELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mLineItemClass Load (4.0ms)[0m SELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1 [["id", 1]]
Rendered shared/_error_messages.html.erb (3.0ms)
[1m[36mBom Load (1.0ms)[0m [1mSELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC[0m [["line_item_id", 8]]
[1m[35mPart Load (0.0ms)[0m SELECT "parts".* FROM "parts" WHERE "parts"."id" = ? ORDER BY "parts"."id" ASC LIMIT 1 [["id", 1]]
Rendered boms/_bom_in_table.html.erb (96.0ms)
[1m[36mPart Load (1.0ms)[0m [1mSELECT "parts".* FROM "parts" ORDER BY "parts".number ASC[0m
Rendered boms/_form_in_table.html.erb (103.0ms)
[1m[35m (24.0ms)[0m SELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8
Rendered boms/index.html.erb within layouts/boms (477.0ms)
Rendered layouts/_shim.html.erb (1.0ms)
Rendered layouts/_header.html.erb (0.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Rendered layouts/application.html.erb (69.0ms)
Completed 200 OK in 671ms (Views: 601.0ms | ActiveRecord: 31.0ms)
Started GET "/boms/22/edit" for 127.0.0.1 at 2013-10-14 14:28:13 -0400
Processing by BomsController#edit as HTML
Parameters: {"id"=>"22"}
[1m[36mBom Load (0.0ms)[0m [1mSELECT "boms".* FROM "boms" WHERE "boms"."id" = ? LIMIT 1[0m [["id", "22"]]
[1m[35mLineItem Load (1.0ms)[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? ORDER BY "line_items"."id" ASC LIMIT 1 [["id", 8]]
[1m[36mLineItemSubClass Load (1.0ms)[0m [1mSELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mLineItemClass Load (0.0ms)[0m SELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1 [["id", 1]]
Rendered shared/_error_messages.html.erb (0.0ms)
[1m[36mBom Load (1.0ms)[0m [1mSELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC[0m [["line_item_id", 8]]
[1m[35mPart Load (0.0ms)[0m SELECT "parts".* FROM "parts" ORDER BY "parts".number ASC
Rendered boms/_form_in_table.html.erb (25.0ms)
[1m[36m (0.0ms)[0m [1mSELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8[0m
Rendered boms/index.html.erb within layouts/boms (41.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Rendered layouts/application.html.erb (54.0ms)
Completed 200 OK in 113ms (Views: 104.0ms | ActiveRecord: 3.0ms)
Started GET "/boms/22/edit" for 127.0.0.1 at 2013-10-14 14:28:37 -0400
Processing by BomsController#edit as HTML
Parameters: {"id"=>"22"}
[1m[35mBom Load (0.0ms)[0m SELECT "boms".* FROM "boms" WHERE "boms"."id" = ? LIMIT 1 [["id", "22"]]
[1m[36mLineItem Load (0.0ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? ORDER BY "line_items"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mLineItemSubClass Load (0.0ms)[0m SELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1 [["id", 8]]
[1m[36mLineItemClass Load (1.0ms)[0m [1mSELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1[0m [["id", 1]]
Rendered shared/_error_messages.html.erb (0.0ms)
[1m[35mBom Load (1.0ms)[0m SELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC [["line_item_id", 8]]
[1m[36mPart Load (0.0ms)[0m [1mSELECT "parts".* FROM "parts" ORDER BY "parts".number ASC[0m
Rendered boms/_form_in_table.html.erb (5.0ms)
[1m[35m (0.0ms)[0m SELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8
Rendered boms/index.html.erb within layouts/boms (27.0ms)
Rendered layouts/_shim.html.erb (1.0ms)
Rendered layouts/_header.html.erb (8.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Rendered layouts/application.html.erb (60.0ms)
Completed 200 OK in 131ms (Views: 94.0ms | ActiveRecord: 2.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/parts.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
I believe this is an HTML issue, not a Rails issue. Per this discussion Form inside a table, <form> can not be placed inside <table> or <tbody> or <tr>. After moving the <form> to wrap the table and putting the controls inside the respective <td> the form works.
I still don't understand why refreshing the page made the form work, but...
If it's Rails 4, it's probably because of Turbolinks.
Try putting
data-no-turbolink="true" inside your body tag
This may work, it happend once to me.
This type of error is most frequently one generated by invalid HTML. Various sources of errors can be:
missing < or >
HTML tag not closed
orphaned HTML closing tag (where no opening one is related); in complex forms I've had extra </div>s lying about...
Forms nested within table or tr tags (within td is allowed)
The form helpers need to be properly nested, otherwise these quirks will bite you...
Try putting data-no-turbolink="true" into the link that called the table page.
<a href="/vender" data-no-turbolink="true">
That works form me.
For rails 5, try using data: { turbolinks: false } inside any links to the page containing the form.
E.g. <%= link_to "Get in Touch", 'contact', data: { turbolinks: false } %>
I have to share my experience :
I played with Turbolinks, like you. But suddently, I had an issue : the previous pages needed to have turbolinks disabled too, to work.
After many & many & many hours on the issue, I found the solution :
<% f.submit %> was separated by 2 <div>from the rest of the form !
Here is an example:
Wrong:
<div class="container">
<div class="row">
<!-- Inscription -->
<div class="col-lg-8 contact_col">
<div class="get_in_touch">
<div class="section_title">Modifier une marque</div>
<div class="contact_form_container">
<%= form_for #brand, url: {action: "update"} do |f| %>
<div class="row">
<div class="col-xl-12">
<!-- Name -->
<label for="contact_name">Nom de la marque</label>
<%= f.text_field :brand, class: "contact_input" %>
</div>
<div class="col-xl-12 last_name_col">
<span>
<%= f.label "Image de la marque" %><br />
</span>
<%= f.file_field :brand_picture %>
</div>
</div>
</div>
</div>
<button class="newsletter_button trans_200">
<%= f.submit "Modifier" %>
</button>
<% end %>
</div>
</div>
</div>
Correct:
<div class="container">
<div class="row">
<!-- Inscription -->
<div class="col-lg-8 contact_col">
<div class="get_in_touch">
<div class="section_title">Modifier une marque</div>
<div class="contact_form_container">
<%= form_for #brand, url: {action: "update"} do |f| %>
<div class="row">
<div class="col-xl-12">
<!-- Name -->
<label for="contact_name">Nom de la marque</label>
<%= f.text_field :brand, class: "contact_input" %>
</div>
<div class="col-xl-12 last_name_col">
<span>
<%= f.label "Image de la marque" %><br />
</span>
<%= f.file_field :brand_picture %>
</div>
</div>
<button class="newsletter_button trans_200">
<%= f.submit "Modifier" %>
</button>
<% end %>
</div>
</div>
</div>
</div>
</div>
Think your workaround might be to just reload on form submit. So add remote: true to your form. This will just do a quick refresh for you. Please note that this is not a solution but a workaround. A recommended solution would be Garrett Berneche's answer.
<%= form_for(form_objects, html: {class: "form-in-table"}, remote: true) do |f| %>
I finished Hartl's Rails tutorial book and am now trying to learn a bit more. I have attempted to add a feed-update on post-submission feature. This has worked well, with one exception. For some reason everytime I click the submit button two entries (duplicates) are being added to the db. Basically, if I look at the server dialog in the terminal I see two of this:
Started POST "/microposts" for 127.0.0.1 at 2012-02-07 22:17:41 +1300
Creating scope :from_users_followed_by. Overwriting existing method Micropost.from_users_followed_by.
Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"p2pZx4VtIOSnywyVrXnk0vOpI5WdGVeuhLTfEaXmT6o=", "micropost"=>{"content"=>"This is a test post!"}, "commit"=>"Submit"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "microposts" ("content", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["content", "This is a test post!"], ["created_at", Tue, 07 Feb 2012 09:17:42 UTC +00:00], ["updated_at", Tue, 07 Feb 2012 09:17:42 UTC +00:00], ["user_id", 19]]
(13.6ms) COMMIT
(0.5ms) SELECT COUNT(*) FROM "microposts" WHERE (user_id IN (SELECT followed_id FROM relationships
WHERE follower_id = 19) OR user_id = 19)
Micropost Load (0.5ms) SELECT "microposts".* FROM "microposts" WHERE (user_id IN (SELECT followed_id FROM relationships
WHERE follower_id = 19) OR user_id = 19) ORDER BY microposts.created_at DESC LIMIT 5 OFFSET 0
CACHE (0.0ms) SELECT COUNT(*) FROM "microposts" WHERE (user_id IN (SELECT followed_id FROM relationships
WHERE follower_id = 19) OR user_id = 19)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
Rendered shared/_feed_item.html.erb (11.7ms)
Rendered shared/_feed.html.erb (20.4ms)
(0.3ms) SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = 19
Rendered microposts/create.js.erb (23.3ms)
Completed 200 OK in 103ms (Views: 26.9ms | ActiveRecord: 23.4ms)
This is in my microposts controller:
def create
#micropost = current_user.microposts.build(params[:micropost])
if
#micropost.save!
respond_to do |format|
format.html { redirect_to root_path }
format.js
end
end
end
This is my create.js.erb
$(".ajaxreloadposts").html("<%= escape_javascript(render('shared/feed')) %>")
This is from my home.html.erb
<% if signed_in? %>
<table class="front" summary="For signed-in users">
<tr>
<td class="main">
<h1 class="micropost">Create Post:</h1>
<%= render 'shared/micropost_form' %>
<span class="ajaxreloadposts">
<%= render 'shared/feed' %>
</span>
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
<%= render 'shared/stats' %>
</td>
</tr>
</table>
<% else %>
Lastly, here is my _feed partial
<% #feed_items = current_user.feed.paginate(:page => params[:page], :per_page => 5) %>
<% unless #feed_items.empty? %>
<table class="microposts" summary="User microposts">
<%= render :partial => 'shared/feed_item', :collection => #feed_items %>
</table>
<div class="digg_pagination">
<%= will_paginate #feed_items %>
</div>
<% end %>
Interestingly the pagination buttons reload incorrectly as well with AJAX update.. and take on the form of microposts?= instead of the desired format page?= but this is another matter completely.
It seems as if your ajax request is triggered twice. Check you aplication js and make sure js are include only once. Rather than requiring tree, require js explicitly.