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.
Related
When I attempt to submit a post request using form_with and a custom url pointing to an existing controller action, the view does not render.
I am trying to follow a tutorial which shows the differences between form_for and form_tag in ROR, but trying to use form_with instead because the former are going to be deprecated. It all works fine using the form_tag.
The Rails Guides for form helpers only shows form_for and form_tag.
The ROR API docs does show some examples with form_with, but I am not connecting the dots.
<!-- index.html.erb -->
<%= form_with(url: create_users_path) do |f| %>
<%= f.text_field :name, placeholder: "Your Name" %>
<%= f.number_field :age, placeholder: "Your Age" %>
<%= f.submit "Create Profile" %>
<% end %>
and
# routes.rb
get 'users' => 'welcome#users'
post 'create_users' => 'welcome#users'
and
# welcome_controller.rb
def users
if params[:name] != nil
user = User.create(name: params[:name], age: params[:age])
end
#users = User.all
end
and
<!--users.html.erb-->
<table border="2">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.age %></td>
</tr>
<% end %>
</table>
<br>
<%= link_to "Return to Index", root_path %>
My users table updates with the data sent in the form, but I am left on my index.html.erb view instead of at users.html.erb where I expect, as the .create active record call is in the users action.
And here are my server logs from the POST request where welcome/users.html.erb is said to have been rendered (browser doesn't update.) A GET will render the page in the browser.
Started POST "/create_users" for 127.0.0.1 at 2018-08-05 22:18:26 -0400
Processing by WelcomeController#users as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"upoeRgVioYXs+Z+vsyBdg4CnXrSTOMCzPFXSHUKT2IlrVMDtvnf8UD7yJkPMYw0zkwihmPvY3Yq0BlG7RFsIIQ==", "name"=>"testy testerson", "age"=>"28", "commit"=>"Create Profile"}
(0.1ms) begin transaction
SQL (2.4ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "testy testerson"], ["age", 28], ["created_at", "2018-08-06 02:18:26.466212"], ["updated_at", "2018-08-06 02:18:26.466212"]]
(3.2ms) commit transaction
Rendering welcome/users.html.erb within layouts/application
User Load (0.2ms) SELECT "users".* FROM "users"
Rendered welcome/users.html.erb within layouts/application (4.3ms)
Completed 200 OK in 147ms (Views: 56.8ms | ActiveRecord: 6.7ms)
Started GET "/users" for 127.0.0.1 at 2018-08-05 22:19:19 -0400
Processing by WelcomeController#users as HTML
Rendering welcome/users.html.erb within layouts/application
User Load (0.3ms) SELECT "users".* FROM "users"
Rendered welcome/users.html.erb within layouts/application (2.3ms)
Completed 200 OK in 60ms (Views: 51.1ms | ActiveRecord: 0.3ms)
I am pretty sure that having the create and show in the same action is not a best practice...just trying to get a better understanding of rails. What am I missing about the routes/controller/views relationship?
I am using rails versrion 5.1.6
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 display images on the homepage with paperclip and I have unpermitted params and a routing error. I have tried various solutions including trying to pass in an array but I think this is happening because of my own lack of knowledge about rails.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Hdw1RzedMZdUE0cPrAXz0fkctQKfW9HX3S5ZwYh4lr0PwTJhHhVwcrglJv5qrMQF3T5YkcJZ9zBiRRRlCoNCNQ==", "document"=>{"doc"=>[#<ActionDispatch::Http::UploadedFile:0x007feaf2db80f0 #tempfile=#<Tempfile:/var/folders/1q/zfrk5kxj1015crsc13jwwrz40000gp/T/RackMultipart20170608-15326-1pcmtgl.jpg>, #original_filename="P1150645.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"document[doc][]\"; filename=\"P1150645.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Upload Document"}
Unpermitted parameter: doc
(1.9ms) begin transaction
SQL (2.0ms) INSERT INTO "documents" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-06-08 04:47:55 UTC], ["updated_at", 2017-06-08 04:47:55 UTC]]
(0.8ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 46ms (ActiveRecord: 4.8ms)
Started GET "/" for ::1 at 2017-06-08 12:47:55 +0800
Processing by PagesController#home as HTML
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" LIMIT ? [["LIMIT", 1]]
Rendering pages/home.html.erb within layouts/application
Document Load (3.1ms) SELECT "documents".* FROM "documents" ORDER BY created_at
Rendered documents/_documents.html.erb (56.7ms)
Rendered documents/_new.html.erb (4.2ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" != 3)
Conversation Load (0.2ms) SELECT "conversations".* FROM "conversations" WHERE (conversations.sender_id = 3 OR conversations.recipient_id = 3)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "messages" WHERE "messages"."conversation_id" = ? [["conversation_id", 4]]
Rendered conversations/_index.html.erb (18.5ms)
Rendered pages/home.html.erb within layouts/application (141.2ms)
Rendered shared/_navbar.html.erb (5.2ms)
Completed 200 OK in 426ms (Views: 401.3ms | ActiveRecord: 6.2ms)
Started GET "/docs/original/missing.png" for ::1 at 2017-06-08 12:47:56 +0800
ActionController::RoutingError (No route matches [GET] "/docs/original/missing.png"):
I've tried nesting the params given that document => doc is passing to an array, even if I leave the params blank it still uploads to the database with null fields and routing error, DocumentsController:
def doc_params
params.require(:document).permit(:id, :doc)
end
I've tried multiple validations in here:
class Document < ApplicationRecord
has_attached_file :doc
do_not_validate_attachment_file_type :doc
end
I wondering if rendering the index is causing the routing error as I got rid of temporarily by re-routing but the image was still null, documents/_index.html.erb
<div class="container">
<div class="row around-xs">
<center>
<% #documents.each do |document| %>
<li class="col-xs-3" id="item-grid">
<%= link_to image_tag(document.doc.url, class: 'media-object', size:"108x152"), document.doc.url, target: '_blank' %>
<% if #users %>
<% if current_user.is_admin? %>
<%= link_to 'Remove', document_path(document), class: 'btn btn-danger', method: :delete, data: {confirm: 'Are you sure?'} %>
<% end %>
<% end %>
</li>
<% end %>
</center>
</div>
</div>
home.html.erb
<section id="about" class="about-section">
<div class="container" id="services">
<div class="col-lg-8 col-md-offset-2" id="progpos"><h1>My Work</h1></div>
<%= render 'documents/index' %>
<br>
<%= render 'documents/new' %>
</div>
</section>
Please please help! thanks! documents/_new.html.erb
<% if #users %>
<% if current_user.is_admin? %>
<%= form_for #document, html: { multipart: true } do |f| %>
<% if #document.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(#document.errors.count, "error")} prohibited this document from being saved:" %>
</h2>
<ul>
<% #document.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group" style="width: 20%">
<%= f.file_field :doc, class: 'form-control', placeholder: "Document", multiple: true %>
</div>
<div class="form-group">
<%= f.submit 'Upload Document', class: 'btn btn-default' %>
</div>
<% end %>
<% end %>
<% end %>
Here's the whole of the controller and I've edited documents/_new.html.erb to how it looks:
class DocumentsController < ApplicationController
def index
#documents = Document.order('created_at')
end
def new
#document = Document.find(params[:id])
end
def create
#document = Document.new(doc_params)
if #document.save
**params[:document][:doc].each do |d| #iterate over multiple attached files
#document.create(doc: d)**
end
flash[:success] = "The document was added!"
redirect_to root_path
else
render '_new'
end
end
def destroy
#document = Document.find(params[:id])
if #document.destroy
flash[:notice] = "Successfully deleted photo!"
redirect_to root_path
else
flash[:alert] = "Error deleting photo!"
end
end
private
def doc_params
params.require(:document).permit(:id, **document: {doc: []}**)
end
end
I've added Pavans code to mine and i've also changed the params at the bottom which now gives me undefined method `create' for #. I think that's progress?
Unpermitted parameter: doc
You have multiple :true set for field doc, so doc should an array to accept the values. You should change the doc_params to below
def doc_params
params.require(:document).permit(:id, doc: [])
end
No handler found for
[#,
#original_filename="P1150645.jpg", #content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"document[doc][]\";
filename=\"P1150645.jpg\"\r\nContent-Type: image/jpeg\r\n">]
You should also set multipart: true to the form to handle file uploads
<%= form_for #document, html: { multipart: true } do |f| %>
ActionController::RoutingError (No route matches [GET]
"/docs/original/missing.png")
Paperclip will try to find missing.png when an object doesn't have an uploaded file and you have tell Paperclip where to find it!
class Document < ApplicationRecord
has_attached_file :doc, :styles => { :medium => "250x250>", :thumb => "150x150>" }, :default_url => "/system/:attachment/:style/missing.jpg"
do_not_validate_attachment_file_type :doc
end
Update:
Your create action should look like below
def create
#document = Document.new(doc_params)
if #document.save
params[:document][:doc].each do |d| #iterate over multiple attached files
#doumnet.create(doc: d)
end
flash[:success] = "The document was added!"
redirect_to root_path
else
render 'new'
end
end
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.
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| %>