Rails object loses params value while creating it - carrierwave

I have Product model with some params (price, category_id, image etc.).
When creating a new product in the form partial, category is being selected via select_tag and image gets uploaded via Carrierwave, image uploader. Bad things happens when I send user to crop the uploaded image in crop.html.erb file. There user crops image, clicks submit, which uses path as update (not very sure which HTTP protocol), and gets redirected into product edit page, since product somehow lost category_id parameter. Form shows error to select a category (all other params from input_fields does not get lost).
My tries was like defining the crop method and setting category there, but no luck...
products_controller.rb:
def edit
#categories = Category.all
end
def create
#product = Product.new(product_params)
#product.category_id = params[:category_id]
#product.user_id = current_user.id
respond_to do |format|
if #product.save
if params[:product][:image].present?
format.html { render :crop }
else
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
end
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def update
#product.category_id = params[:category_id]
respond_to do |format|
if #product.update(product_params)
if params[:product][:image].present?
format.html { render :crop }
else
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
#products = Product.all
ActionCable.server.broadcast 'products',
html: render_to_string('store/index', layout: false)
end
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
crop.html.erb (in products folder):
<%= image_tag #product.image_url(:large), id: "cropbox" %>
<h4>Preview</h4>
<div style="width:300px; height:200px; overflow:hidden;">
<%= image_tag #product.image.url(:large), :id => "preview" %>
</div>
<%= form_for #product do |f| %>
<% %w[x y w h].each do |attribute| %>
<%= f.hidden_field "crop_#{attribute}" %>
<% end %>
<div class="actions">
<%= f.submit "Crop" %>
</div>
<% end %>
I haven't tried to set a callback that stores category_id, but I wonder would it be a good idea? Any help? Thanks!
EDIT - also adding a form partial, but it works fine - just for your curiosity:
<%= form_for(product, hmtl: { multipart: true } ) do |f| %>
...
<div class="field">
<%= f.label :category %>
<%= select_tag(:category_id, options_from_collection_for_select(Category.all, :id, :name, #product.category_id), include_blank: "Select Category") %>
</div>
UPDATE
Logs for these actions:
Started GET "/products/new" for ::1 at 2016-12-15 09:03:31 +0200
Processing by ProductsController#new as HTML
[1m[36mCart Load (0.4ms)[0m [1m[34mSELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2[0m [["id", 5], ["LIMIT", 1]]
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2[0m [["id", 5], ["LIMIT", 1]]
[1m[36mUser Load (2.0ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
Rendering products/new.html.erb within layouts/application
[1m[36mCategory Load (0.4ms)[0m [1m[34mSELECT "categories".* FROM "categories"[0m
Rendered products/_form.html.erb (6.2ms)
Rendered products/new.html.erb within layouts/application (11.2ms)
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT "categories".* FROM "categories"[0m
[1m[36mLineItem Load (0.5ms)[0m [1m[34mSELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1[0m [["cart_id", 5]]
Completed 200 OK in 127ms (Views: 113.2ms | ActiveRecord: 3.3ms)
Started POST "/products" for ::1 at 2016-12-15 09:03:40 +0200
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eWZuqL6AqcN8wknjEL115ax9uBnpY4b9eP0o2xN2aPntd61YKyc4Ym1lUgjV1YrXfZbPr57HANXy7Kz5swCtlg==", "product"=>{"title"=>"kj", "description"=>"kj", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fe40ba4abe0 #tempfile=#<Tempfile:/var/folders/dn/zq9x2jkd4856kwhfj5gbz2tc0000gn/T/RackMultipart20161215-3987-1adfacs.png>, #original_filename="Screen Shot 2016-12-05 at 09.14.48.png", #content_type="image/png", #headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"Screen Shot 2016-12-05 at 09.14.48.png\"\r\nContent-Type: image/png\r\n">, "price"=>"98", "quantity"=>"98"}, "category_id"=>"1", "commit"=>"Create Product"}
[1m[36mCart Load (0.3ms)[0m [1m[34mSELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2[0m [["id", 5], ["LIMIT", 1]]
[1m[36mCACHE (0.0ms)[0m [1m[34mSELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2[0m [["id", 5], ["LIMIT", 1]]
[1m[36mUser Load (0.7ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
[1m[35m (0.3ms)[0m [1m[35mBEGIN[0m
[1m[36mCategory Load (0.3ms)[0m [1m[34mSELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
[1m[35mSQL (1.0ms)[0m [1m[32mINSERT INTO "products" ("title", "description", "price", "quantity", "created_at", "updated_at", "category_id", "user_id", "image") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"[0m [["title", "kj"], ["description", "kj"], ["price", #<BigDecimal:7fe40ccf9110,'0.98E2',9(18)>], ["quantity", 98], ["created_at", 2016-12-15 07:03:41 UTC], ["updated_at", 2016-12-15 07:03:41 UTC], ["category_id", "1"], ["user_id", 1], ["image", "Screen_Shot_2016-12-05_at_09.14.48.png"]]
[1m[35m (3.1ms)[0m [1m[35mCOMMIT[0m
Rendering products/crop.html.erb within layouts/application
Rendered products/crop.html.erb within layouts/application (3.1ms)
[1m[36mCategory Load (0.7ms)[0m [1m[34mSELECT "categories".* FROM "categories"[0m
[1m[36mLineItem Load (0.3ms)[0m [1m[34mSELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1[0m [["cart_id", 5]]
Completed 200 OK in 957ms (Views: 127.8ms | ActiveRecord: 6.7ms)
So I noticed that it renders products/crop.html.erb without a particular ID, so I added in my routes:
resources :products do
get 'crop', on: :member
end
And products_controller create action:
...
respond_to do |format|
if #product.save
if params[:product][:image].present?
format.html { render crop_product_path(#product) }
else
...
Still the same error, that a category must be selected.
And to note - if simply go to edit product (but not from the crop action), category sits there as supposed...

Since form only submits the parameters gathered which are defined in it, you should add a hidden field to set the category_id in crop.html view
<%= image_tag #product.image_url(:large), id: "cropbox" %>
<h4>Preview</h4>
<div style="width:300px; height:200px; overflow:hidden;">
<%= image_tag #product.image.url(:large), :id => "preview" %>
</div>
<%= form_for #product do |f| %>
<% %w[x y w h].each do |attribute| %>
<%= f.hidden_field "crop_#{attribute}" %>
<% end %>
<%= f.hidden_field :category_id %>
<div class="actions">
<%= f.submit "Crop" %>
</div>
<% end %>
Now, when you submit the form, you will receive category_id on server in params[:product][:category_id]

Related

Ajax in Rails 5 throw "ActionController::UnknownFormat " error

I am trying to implement a search method that will automaticaly populate a form through the use of Ajax.
I have a simple_form_form that is feed with a collection of Client instance. Once I select a client, a js code submit the form and then goes to the corresponded controller method.
This part is working, but when the code hits the Client#search it stops at my "respond_to |format|" line. Stating that this is an unknown format.
I looked differents stackover posts, but I could'nt find any concrete answers.
Thanks,
here is the code:
My error:
error messages
routes.rb
Rails.application.routes.draw do
class OnlyAjaxRequest
def matches?(request)
request.xhr?
# raise
end
end
get 'search', to: 'invoices#search', as: 'search', constraint: OnlyAjaxRequest.new
end
formSubmit.js
let clientOption = document.querySelector('#search_client');
const form = document.querySelector('.search');
clientOption.addEventListener('change', (event) => {
form.submit();
})
Client Controller:
def search
if params[:search]
#client_found = Client.find(params[:search][:client]) if Client.find(params[:search][:client]).present?
respond_to do |format|
#format.html {}
format.js { render :search}
end
end
end
Search.js.erb
console.log('hello');
$('#ajx-cli').html("<%= escape_javascript(render partial: 'invoices/invoice_new_partial/find_client')%>");
Client Selection Form
<%= simple_form_for :search, url: search_path, remote: true, method: :get, html: { id: :find_cli } do |f| %>
<%= f.input :client, label:"Sélectionnez un client", collection: #my_clients, label_method: :company_name, value_method: :id, include_blank: true, as: :select %>
<% end %>
Updated Form
<div id='ajx-cli'>
<%= f.fields_for :client do |client| %>
<%= render partial: "invoices/invoice_new_partial/oneoff_client",locals: {f: client, client_found: #client_found} %>
<% end %>
</div>
Also, my search action seems to be processed as html instead of js.
Logs terminal
Started GET "/search?utf8=%E2%9C%93&search%5Bclient%5D=176" for ::1 at 2020-10-17 14:15:07 +0200
Processing by InvoicesController#search as HTML
Parameters: {"utf8"=>"✓", "search"=>{"client"=>"176"}}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 92], ["LIMIT", 1]]
↳ /Users/utilisateur/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Invoice Exists (0.7ms) SELECT 1 AS one FROM "invoices" WHERE (due_date < '2020-10-17') AND "invoices"."status" != $1 LIMIT $2 [["status", 3], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:32
Invoice Update All (0.9ms) UPDATE "invoices" SET "status" = 4 WHERE (due_date < '2020-10-17') AND "invoices"."status" != $1 [["status", 3]]
↳ app/controllers/application_controller.rb:34
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT $2 [["id", 176], ["LIMIT", 1]]
↳ app/controllers/invoices_controller.rb:56
CACHE Client Load (0.0ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT $2 [["id", 176], ["LIMIT", 1]]
↳ app/controllers/invoices_controller.rb:56
Completed 406 Not Acceptable in 9ms (ActiveRecord: 2.3ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/invoices_controller.rb:58:in `search'
EDIT
Trying to force format didn't work either. My request was correctly processed as JS, so I didn't have the same error, but my search.js.erb was rendered instead of being executed.
New Invoice#search
def search
if params[:search]
#client_found = Client.find(params[:search][:client]) if Client.find(params[:search][:client]).present?
respond_to do |format|
format.js { render layout: false, content_type: 'text/javascript'}
end
end
New Client Selection Form
<%= simple_form_for :search, url: search_path(format: :js), remote: true, method: :get, html: { id: :find_cli } do |f| %>
<%= render partial: "invoices/invoice_new_partial/client_selection",locals: {my_clients: #my_clients, f: f, client_found: #client_found} %>
<% end %>
Logs:
Started GET "/search.js?utf8=%E2%9C%93&search%5Bclient%5D=178" for ::1 at 2020-10-17 19:19:43 +0200
Processing by InvoicesController#search as JS
Parameters: {"utf8"=>"✓", "search"=>{"client"=>"178"}}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 92], ["LIMIT", 1]]
↳ /Users/utilisateur/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Invoice Exists (0.6ms) SELECT 1 AS one FROM "invoices" WHERE (due_date < '2020-10-17') AND "invoices"."status" != $1 LIMIT $2 [["status", 3], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:32
Invoice Update All (6.5ms) UPDATE "invoices" SET "status" = 4 WHERE (due_date < '2020-10-17') AND "invoices"."status" != $1 [["status", 3]]
↳ app/controllers/application_controller.rb:34
Client Load (0.3ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT $2 [["id", 178], ["LIMIT", 1]]
↳ app/controllers/invoices_controller.rb:56
CACHE Client Load (0.1ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT $2 [["id", 178], ["LIMIT", 1]]
↳ app/controllers/invoices_controller.rb:56
Rendering invoices/search.js.erb
Rendered invoices/invoice_new_partial/_find_client.html.erb (0.5ms)
Rendered invoices/search.js.erb (2.2ms)
Completed 200 OK in 24ms (Views: 5.6ms | ActiveRecord: 7.9ms)
I couldn't wrapped my mind around why my js.erb was rendered as a plain text, instead of being executed, so I started over.
I updated my form Submit.js and it worked like a charm.
New routes.rb
post 'search', to: 'invoices#search', as: 'search'
New Client Selection form
<%= simple_form_for :search, remote: true, html: { id: :find_cli } do |f| %>
<%= render partial: "invoices/invoice_new_partial/client_selection",locals: {my_clients: #my_clients, f: f, client_found: #client_found} %>
<% end %>
New formSubmit.js
let clientOption = document.querySelector('#search_client');
const form = document.querySelector('.search');
clientOption.addEventListener('change', (event) => {
let selector = document.querySelector('#search_client')
let client = selector.options[selector.selectedIndex].value;
Rails.ajax({
type: "post",
url: `/search?utf8=✓&search%5Bclient%5D=${client}`,
headers: {
accept: '*/*',
'X-CSRF-Token': Rails.csrfToken()
}
});
});

using ajax for acts_as_voteable in rails

im trying to use ajax on acts_as_votable gem so the page doesnt refresh on a vote/upvote. I've adjused the controller methods for this, and added remote true on the links. However i think the only thing left are the two .js.erb files. ive looked around on here and the ruby documentation and i cant seem to find how to do it for this instance. thanks
VM23182:1 Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (application-50000f29e1bf2752ae7e56dda39f10dd0f67a47aea5c3f07ed0d933f3e1a256a.js:268)
at application-50000f29e1bf2752ae7e56dda39f10dd0f67a47aea5c3f07ed0d933f3e1a256a.js:196
at XMLHttpRequest.xhr.onreadystatechange (application-50000f29e1bf2752ae7e56dda39f10dd0f67a47aea5c3f07ed0d933f3e1a256a.js:251)
`$('.unlike_post').bind('ajax:success', function(){
$(this).parent().parent().find('.vote_count').html('<%= escape_javascript #post.votes_for.size.to_s %>');
$(this).closest('.unlike_post').hide();
$(this).closest('.votes').html(' <%= link_to "Like", like_post_path(#post), remote: true, method: :get, class: "like_post" %>');
});`
Started PUT "/tweets/60/like" for 127.0.0.1 at 2018-10-02 00:06:40 +0100
Processing by TweetsController#upvote as JS
Parameters: {"id"=>"60"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 3], ["LIMIT", 1]]
↳ /Users/benherring/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Tweet Load (0.2ms) SELECT "tweets".* FROM "tweets" WHERE "tweets"."id" = $1 LIMIT $2 [["id", 60], ["LIMIT", 1]]
↳ app/controllers/tweets_controller.rb:57
(0.4ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = $3 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = $4 [["votable_id", 60], ["votable_type", "Tweet"], ["voter_id", 3], ["voter_type", "User"]]
↳ app/controllers/tweets_controller.rb:58
ActsAsVotable::Vote Load (0.6ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."voter_id" = $3 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = $4 ORDER BY "votes"."id" DESC LIMIT $5 [["votable_id", 60], ["votable_type", "Tweet"], ["voter_id", 3], ["voter_type", "User"], ["LIMIT", 1]]
↳ app/controllers/tweets_controller.rb:58
(0.2ms) BEGIN
↳ app/controllers/tweets_controller.rb:58
ActsAsVotable::Vote Update (0.5ms) UPDATE "votes" SET "updated_at" = $1, "vote_flag" = $2 WHERE "votes"."id" = $3 [["updated_at", "2018-10-01 23:06:40.357671"], ["vote_flag", true], ["id", 10]]
↳ app/controllers/tweets_controller.rb:58
(1.3ms) COMMIT
↳ app/controllers/tweets_controller.rb:58
Rendering tweets/vote.js.erb
(0.4ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 [["votable_id", 60], ["votable_type", "Tweet"], ["vote_flag", true]]
↳ app/views/tweets/_icons.html.erb:5
(0.6ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 [["votable_id", 60], ["votable_type", "Tweet"], ["vote_flag", false]]
↳ app/views/tweets/_icons.html.erb:9
Rendered tweets/_icons.html.erb (5.7ms)
Rendered tweets/vote.js.erb (8.7ms)
Completed 200 OK in 40ms (Views: 21.0ms | ActiveRecord: 4.7ms)
resources :tweets do
member do
put "like", to: "tweets#upvote"
put "dislike", to: "tweets#downvote"
end
<div id="tweets">
<% #tweets.each do |tweet| %>
<div class="card">
<h4 class = "username"><%= link_to tweet.user.username, user_path(tweet.user) %></h4>
<p class="tweet-content"><%= tweet.content %></p>
<p class="date"><%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %></p>
<div class="icons">
<%= link_to like_tweet_path(tweet),remote: true , method: :put do %>
<div class="upvote"><i class="fas fa-thumbs-up"></i> <%= tweet.get_upvotes.size %></div>
<% end %>
<%= link_to dislike_tweet_path(tweet), remote: true, method: :put do %>
<div><i class="fas fa-thumbs-down"></i> <%= tweet.get_downvotes.size %></div>
<% end %>
</div>
</div>
<% end %>
</div>
def upvote
#tweet = Tweet.find(params[:id])
#tweet.upvote_by current_user
respond_to do |format|
format.html {redirect_to :tweets}
format.js {render layout: false}
# redirect_to :tweets
# redirect_to :tweets
# if request.xhr?
# head :ok
# else
end
end
def downvote
#tweet = Tweet.find(params[:id])
#tweet.downvote_by current_user
respond_to do |format|
format.html {redirect_to :tweets}
format.js {render layout: false}
# redirect_to :tweets
end
end
It looks like the way you initially started on is to attach handlers to the buttons that listen for successful requests. This is a fair way to do it, but - since you're already using Rails UJS - there's a possibly "better" way.
With Rails UJS, JS files returned from a remote action are automatically executed. In my opinion, a more idiomatic Rails way to do this is as follows:
Extract out the icons into their own template:
<div id="tweets">
<% #tweets.each do |tweet| %>
<div id="tweet-<%= tweet.id %>" class="card">
<h4 class = "username"><%= link_to tweet.user.username, user_path(tweet.user) %></h4>
<p class="tweet-content"><%= tweet.content %></p>
<p class="date"><%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %></p>
<%= render 'icons', tweet: tweet %>
</div>
<% end %>
</div>
tweets/_icons.html.erb:
<div class="icons">
<%= link_to like_tweet_path(tweet),remote: true , method: :put do %>
<div class="upvote"><i class="fas fa-thumbs-up"></i> <%= tweet.get_upvotes.size %></div>
<% end %>
<%= link_to dislike_tweet_path(tweet), remote: true, method: :put do %>
<div class="downvote"><i class="fas fa-thumbs-down"></i> <%= tweet.get_downvotes.size %></div>
<% end %>
</div>
Then, in your upvote/downvote controller actions you would format.js { render 'vote' } which would live at tweets/vote.js.erb and look like:
document.querySelector('#tweet-<%= #tweet.id %> .icons').outerHTML = '<%= j render('icons', tweet: #tweet) %>';
At this point, you're now free to modify tweets/_icons.html.erb to provide extra functionality (e.g. visual feedback on votes or changing the URL of the links to undo a vote).
Old jQuery method
Keeping this "older" jQuery method for those people who encounter a similar issue and are using jQuery. Though, really, you probably don't need it.
$('#tweet-<%= #tweet.id %> .icons').replaceWith('<%= j render('icons', tweet: #tweet) %>');

Rails 5 Enum With Select Field Not Saving

I understand that this has been asked many times, but for some reason none of the other solutions worked for me. I'm still unable to save a field with an enum on update.When I pass a non-enum value, like first_name, with the same form, it updates just fine, but the enum value, gender, never saves even though it shows that it is...
View:
<%= form_for(#patient, url: patient_path(params[:id])) do |f| %>
<%= f.fields_for :role do |r| %>
<%= r.fields_for :user do |u| %>
<div class="keyvalue">
<div class="key"><p>Gender</p></div>
<div class="value">
<%= u.select :gender, User.genders.map { |key, value| [key.humanize, key] }, include_blank: '-Select A Gender-' %>
</div>
<div class="push"></div>
</div><!--keyvalue-->
<% end %>
<% end %>
<% end %>
Controller:
def update
#patient = Patient.find(params[:id])
if #patient.update(patient_params)
flash[:success] = "Patient's record has been updated."
redirect_to patient_path(params[:id])
else
flash[:error] = #patient.errors.full_messages.to_sentence
redirect_to patient_path(params[:id])
end
end
Log:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"*********", "commit"=>"Update", "patient"=>{"role_attributes"=>{"user_attributes"=>{"first_name"=>"John", "middle_name"=>"Joseph", "last_name"=>"Doe", "date_of_birth"=>"1943-12-25", "gender"=>"male", "blood_type"=>"", "ethnicity"=>"", "marital_status"=>"", "id"=>"1"}, "id"=>"1"}}, "id"=>"1"}
Patient Load (0.1ms) SELECT "patients".* FROM "patients" WHERE "patients"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
Role Load (0.1ms) SELECT "roles".* FROM "roles" WHERE "roles"."roleable_id" = ? AND "roles"."roleable_type" = ? LIMIT ? [["roleable_id", 1], ["roleable_type", "Patient"], ["LIMIT", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = ? AND ("users"."id" != ?) LIMIT ? [["username", "dev"], ["id", 1], ["LIMIT", 1]]
SQL (0.3ms) UPDATE "users" SET "gender" = ?, "updated_at" = ? WHERE "users"."id" = ? [["gender", 0], ["updated_at", "2018-04-24 02:11:23.126843"], ["id", 1]]
(2.6ms) commit transaction
Redirected to http://localhost:3000/patients/1
Completed 302 Found in 10ms (ActiveRecord: 3.2ms)
Enum in User model:
enum gender: [:male, :female, :non_binary, :rather_not_say]
Migration:
t.integer :gender
The log shows no issue, it shows the gender field being updated, but if I go to the rails console it shows gender as nil

How do I troubleshoot a failed video upload (Carrierwave) if no errors being sent to log?

I am doing a video upload with Carrierwave, and it keeps failing. But the log doesn't show me what's wrong.
This is my video_uploader.rb:
class VideoUploader < CarrierWave::Uploader::Base
storage :fog
def content_type_whitelist
/video\//
end
def extension_whitelist
%w(mp4 mov wmv mpeg4 avi 3gpp webm)
end
end
This is what my log looks like of the successful update action (which includes the new file to be uploaded):
Started PATCH "/profiles/kristin-walter-jamaica-college" for ::1 at 2016-10-19 05:22:56 -0500
Processing by ProfilesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YBrqV==", "profile"=>{"remove_avatar"=>"0", "avatar_cache"=>"", "first_name"=>"Kristin", "last_name"=>"Walter", "dob(3i)"=>"1", "dob(2i)"=>"5", "dob(1i)"=>"1998", "weight"=>"249", "height"=>"85", "bib_color"=>"19", "player_type"=>"player", "video_url"=>"https://www.youtube.com/embed/a4k", "position_ids"=>[""], "school_id"=>"2", "grade"=>"", "grades_attributes"=>{"0"=>{"subject"=>"", "result"=>"", "grade_type"=>"csec", "_destroy"=>"false"}}, "transcript_cache"=>"", "achievements_attributes"=>{"0"=>{"body"=>"", "achievement_type"=>"academic", "_destroy"=>"false"}}, "videos_attributes"=>{"0"=>{"url"=>"", "remove_video"=>"0", "video"=>#<ActionDispatch::Http::UploadedFile:0x007fbccc812f28 #tempfile=#<Tempfile:/var/folders/0f/hgplttnd7dg6q9m62qtbnpn00000gn/T/RackMultipart20161019-12863-159wlxq.MOV>, #original_filename="P8250323.MOV", #content_type="video/quicktime", #headers="Content-Disposition: form-data; name=\"profile[videos_attributes][0][video]\"; filename=\"P8250323.MOV\"\r\nContent-Type: video/quicktime\r\n">, "video_cache"=>"", "vimeo_url"=>"", "vimeo_embed_code"=>"", "official"=>"", "_destroy"=>"false", "id"=>"4"}, "1"=>{"url"=>"", "remove_video"=>"0", "video_cache"=>"", "vimeo_url"=>"", "vimeo_embed_code"=>"", "official"=>"", "_destroy"=>"false"}}, "articles_attributes"=>{"0"=>{"source"=>"Gleaner", "title"=>"JC Off The Mark With Easy Win", "url"=>"http://jamaica-gleaner.com/article/sports/20160911/jc-mark-easy-win", "_destroy"=>"false", "id"=>"1"}, "1"=>{"source"=>"", "title"=>"", "url"=>"", "_destroy"=>"false"}}}, "commit"=>"Update Profile", "id"=>"kristin-walter-jamaica-college"}
Profile Load (1.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."slug" = $1 ORDER BY "profiles"."id" ASC LIMIT $2 [["slug", "kristin-walter-jamaica-college"], ["LIMIT", 1]]
Grade Load (1.0ms) SELECT "grades".* FROM "grades" WHERE "grades"."profile_id" = 9
HABTM_Positions Load (1.0ms) SELECT "positions_profiles".* FROM "positions_profiles" WHERE "positions_profiles"."profile_id" = 9
Achievement Load (1.0ms) SELECT "achievements".* FROM "achievements" WHERE "achievements"."profile_id" = 9
(0.9ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."profile_id" = $1 [["profile_id", 9]]
(0.6ms) BEGIN
Position Load (1.1ms) SELECT "positions".* FROM "positions" WHERE 1=0
Article Load (1.1ms) SELECT "articles".* FROM "articles" WHERE "articles"."profile_id" = $1 AND "articles"."id" = 1 [["profile_id", 9]]
Video Load (1.9ms) SELECT "videos".* FROM "videos" WHERE "videos"."profile_id" = $1 AND "videos"."id" = 4 [["profile_id", 9]]
SQL (3.3ms) UPDATE "profiles" SET "updated_at" = $1, "avatar" = $2 WHERE "profiles"."id" = $3 [["updated_at", 2016-10-19 10:22:56 UTC], ["avatar", "Recrutz-6.jpg"], ["id", 9]]
(78.2ms) COMMIT
Redirected to http://localhost:3000/profiles/kristin-walter-jamaica-college
Completed 302 Found in 126ms (ActiveRecord: 91.2ms)
This my Video.rb model:
class Video < ApplicationRecord
mount_uploader :video, VideoUploader
belongs_to :profile
end
This is my profile_controller#update.rb, which is where the associated video object is being uploaded from:
def update
respond_to do |format|
if #profile.update(profile_params)
format.html { redirect_to #profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: #profile }
else
format.html { redirect_to edit_profile_path(#profile), message: #profile.errors.messages }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
This is the relevant portion of the _form.html.erb:
<h3 class="text-center">Videos</h3>
<div id="videos" class="text-center">
<%= f.simple_fields_for :videos do |video| %>
<%= render 'video_fields', f: video %>
<% end %>
<div class="links">
<%= link_to_add_association 'Add Video', f, :videos, class: "btn btn-success add-grade-button" %>
</div>
</div>
This is the _video_fields.html.erb partial referenced in the _form partial (truncated for brevity):
<%= f.input_field :url, class: 'form-control' %>
<% if !#profile.videos.empty? %>
<% #profile.videos.each do |video| %>
<%= video.video_identifier %>
<label>
<%= f.check_box :remove_video %>
Remove video
</label>
<% end %>
<% end %>
<%= f.file_field :video, class: 'form-control' %>
<%= f.input_field :video_cache, as: :hidden, class: 'form-control '%>
<%= f.input_field :vimeo_url, class: 'form-control' %>
<%= f.input_field :vimeo_embed_code, class: 'form-control' %>
<%= f.input_field :official, as: :radio_buttons, class: 'form-control' %>
<% end %>
I have two other uploaders for images & documents, and they work fine. For some reason, this one doesn't work.
It creates the folder on S3, but it doesn't actually upload the file.
How do I troubleshoot this?
Edit 1
In my profile.rb model, I have the following:
has_many :videos, dependent: :destroy
accepts_nested_attributes_for :videos, reject_if: :reject_videos, allow_destroy: true
Edit 2
This is the log output when I upload a file that should NOT work, i.e. an image in the video upload. Nothing obvious happens. It just fails silently.
Started PATCH "/profiles/kristin-walter-jamaica-college" for ::1 at 2016-10-20 02:21:37 -0500
Processing by ProfilesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"390ImtDcSl4RoEZA==", "profile"=>{"remove_avatar"=>"0", "avatar_cache"=>"", "first_name"=>"Kristin", "last_name"=>"Walter", "dob(3i)"=>"1", "dob(2i)"=>"5", "dob(1i)"=>"1998", "weight"=>"249", "height"=>"85", "bib_color"=>"19", "player_type"=>"player", "video_url"=>"https://www.youtube.com/embed/Rpa4k", "position_ids"=>[""], "school_id"=>"2", "grade"=>"", "grades_attributes"=>{"0"=>{"subject"=>"", "result"=>"", "grade_type"=>"csec", "_destroy"=>"false"}}, "transcript_cache"=>"", "achievements_attributes"=>{"0"=>{"body"=>"", "achievement_type"=>"academic", "_destroy"=>"false"}}, "videos_attributes"=>{"0"=>{"url"=>"", "remove_video"=>"0", "video"=>#<ActionDispatch::Http::UploadedFile:0x007fdff5b64c48 #tempfile=#<Tempfile:/var/folders/0f/hgplttnd7dg6q9m62qtbnpn00000gn/T/RackMultipart20161020-27906-95t0fg.jpg>, #original_filename="Photo Oct 13, 3 36 47 PM.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"profile[videos_attributes][0][video]\"; filename=\"Photo Oct 13, 3 36 47 PM.jpg\"\r\nContent-Type: image/jpeg\r\n">, "video_cache"=>"", "vimeo_url"=>"", "vimeo_embed_code"=>"", "official"=>"", "_destroy"=>"false", "id"=>"4"}, "1"=>{"url"=>"", "remove_video"=>"0", "video_cache"=>"", "vimeo_url"=>"", "vimeo_embed_code"=>"", "official"=>"", "_destroy"=>"false"}}, "articles_attributes"=>{"0"=>{"source"=>"Gleaner", "title"=>"JC Off The Mark With Easy Win", "url"=>"http://jamaica-gleaner.com/article/sports/20160911/jc-mark-easy-win", "_destroy"=>"false", "id"=>"1"}, "1"=>{"source"=>"", "title"=>"", "url"=>"", "_destroy"=>"false"}}}, "commit"=>"Update Profile", "id"=>"kristin-walter-jamaica-college"}
Profile Load (1.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."slug" = $1 ORDER BY "profiles"."id" ASC LIMIT $2 [["slug", "kristin-walter-jamaica-college"], ["LIMIT", 1]]
Grade Load (0.8ms) SELECT "grades".* FROM "grades" WHERE "grades"."profile_id" = 9
HABTM_Positions Load (1.1ms) SELECT "positions_profiles".* FROM "positions_profiles" WHERE "positions_profiles"."profile_id" = 9
Achievement Load (1.0ms) SELECT "achievements".* FROM "achievements" WHERE "achievements"."profile_id" = 9
(0.8ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."profile_id" = $1 [["profile_id", 9]]
(1.0ms) BEGIN
Position Load (1.2ms) SELECT "positions".* FROM "positions" WHERE 1=0
Article Load (1.0ms) SELECT "articles".* FROM "articles" WHERE "articles"."profile_id" = $1 AND "articles"."id" = 1 [["profile_id", 9]]
Video Load (0.9ms) SELECT "videos".* FROM "videos" WHERE "videos"."profile_id" = $1 AND "videos"."id" = 4 [["profile_id", 9]]
SQL (1.9ms) UPDATE "profiles" SET "updated_at" = $1, "avatar" = $2 WHERE "profiles"."id" = $3 [["updated_at", 2016-10-20 07:21:37 UTC], ["avatar", "Recrutz-6.jpg"], ["id", 9]]
(111.8ms) COMMIT
Redirected to http://localhost:3000/profiles/kristin-walter-jamaica-college
Completed 302 Found in 151ms (ActiveRecord: 122.6ms)
Edit 3
As a point of comparison, here is the log from the update operation of a successful avatar upload -- note this is governed by a different model, and different uploader, etc. But just to show what the log looks like when it actually uploads successfully.
Started PATCH "/profiles/kristin-walter-jamaica-college" for ::1 at 2016-10-20 05:52:41 -0500
Processing by ProfilesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"RqAORU3Iw==", "profile"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fdff618de58 #tempfile=#<Tempfile:/var/folders/0f/hgplttnd7dg6q9m62qtbnpn00000gn/T/RackMultipart20161020-27906-1cg2dxq.jpg>, #original_filename="DSC_0010.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"profile[avatar]\"; filename=\"DSC_0010.jpg\"\r\nContent-Type: image/jpeg\r\n">, "avatar_cache"=>"", "first_name"=>"Kristin", "last_name"=>"Walter", "dob(3i)"=>"1", "dob(2i)"=>"5", "dob(1i)"=>"1998", "weight"=>"249", "height"=>"85", "bib_color"=>"19", "player_type"=>"player", "video_url"=>"https://www.youtube.com/Rpa4k", "position_ids"=>[""], "school_id"=>"2", "grade"=>"", "grades_attributes"=>{"0"=>{"subject"=>"", "result"=>"", "grade_type"=>"csec", "_destroy"=>"false"}}, "transcript_cache"=>"", "achievements_attributes"=>{"0"=>{"body"=>"", "achievement_type"=>"academic", "_destroy"=>"false"}}, "videos_attributes"=>{"0"=>{"url"=>"", "remove_video"=>"0", "video_cache"=>"", "vimeo_url"=>"", "vimeo_embed_code"=>"", "official"=>"", "_destroy"=>"false", "id"=>"4"}, "1"=>{"url"=>"", "remove_video"=>"0", "video_cache"=>"", "vimeo_url"=>"", "vimeo_embed_code"=>"", "official"=>"", "_destroy"=>"false"}}, "articles_attributes"=>{"0"=>{"source"=>"Gleaner", "title"=>"JC Off The Mark With Easy Win", "url"=>"http://jamaica-gleaner.com/article/sports/20160911/jc-mark-easy-win", "_destroy"=>"false", "id"=>"1"}, "1"=>{"source"=>"", "title"=>"", "url"=>"", "_destroy"=>"false"}}}, "commit"=>"Update Profile", "id"=>"kristin-walter-jamaica-college"}
Profile Load (1.3ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."slug" = $1 ORDER BY "profiles"."id" ASC LIMIT $2 [["slug", "kristin-walter-jamaica-college"], ["LIMIT", 1]]
Grade Load (1.0ms) SELECT "grades".* FROM "grades" WHERE "grades"."profile_id" = 9
HABTM_Positions Load (1.3ms) SELECT "positions_profiles".* FROM "positions_profiles" WHERE "positions_profiles"."profile_id" = 9
Achievement Load (1.0ms) SELECT "achievements".* FROM "achievements" WHERE "achievements"."profile_id" = 9
(0.9ms) SELECT COUNT(*) FROM "videos" WHERE "videos"."profile_id" = $1 [["profile_id", 9]]
(0.6ms) BEGIN
Position Load (0.8ms) SELECT "positions".* FROM "positions" WHERE 1=0
Article Load (1.4ms) SELECT "articles".* FROM "articles" WHERE "articles"."profile_id" = $1 AND "articles"."id" = 1 [["profile_id", 9]]
Video Load (1.3ms) SELECT "videos".* FROM "videos" WHERE "videos"."profile_id" = $1 AND "videos"."id" = 4 [["profile_id", 9]]
SQL (2.3ms) UPDATE "profiles" SET "avatar" = $1, "updated_at" = $2 WHERE "profiles"."id" = $3 [["avatar", "DSC_0010.jpg"], ["updated_at", 2016-10-20 10:52:41 UTC], ["id", 9]]
(1.3ms) COMMIT
Redirected to http://localhost:3000/profiles/kristin-walter-jamaica-college
Completed 302 Found in 1053ms (ActiveRecord: 13.4ms)

Rails - Name can't be blank error when the input isn't blank

I get an error of 'Name can't be blank' and "Name only allows letters, numbers and '-'" when trying to update a name. This was working fine but I have just gone through it again while writing a test for it and realise it isn't working anymore, I can't figure out why or what has changed to break it.
Categories controller
def update
if #category.update category_params
redirect_to new_guide_category_item_path(#guide, #category)
flash[:info] = "Updated successfully"
else
render 'edit'
end
end
private
def category_params
params.require(:category).permit(:name, :template)
end
edit.html.erb
<%= render 'shared/error_messages', object: f.object %>
.....
<%= form_for([#guide, #category], url: guide_category_path) do |f| %>
<%= f.label :name, "Category name" %>
<%= f.text_field :name %>
<%= f.label :template, "Template" %>
<%= f.text_area :template, { :id => 'edit' } %>
<%= f.submit "Save", :value => "Save Template" %>
<% end %>
model
validates :name, presence: true, length: { maximum: 255 }, uniqueness: { scope: :guide_id, case_sensitive: false },
exclusion: { in: %w( guide guides category categories item items page pages post posts tag tags key keys item key item keys item-key item-keys item_key item_keys mod moderator mods moderators admin admins), message: "%{value} cant be taken." },
format: { with: /\A[a-zA-Z0-9 -]+\z/, message: "only allows letters, numbers, spaces and '-'" }
routes
resources :guides do
resources :categories, only: [:new, :create, :edit, :update] do
end
end
Anyone have an idea of what might be going wrong?
Edit added log output
Started PATCH "/guides/ghj/categories/ijijij" for ::1 at 2016-02-11 14:08:04 +1100
Processing by CategoriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IqiUJ8wF8ZY4t8f91+t4aOMc9xenx/F2beKjKs9GY7JzLD6ZgPYDY1ueC2s+OIDL+PjROVtMe2+GvgYdan1CDQ==", "category"=>{"name"=>"ijijijddd", "template"=>""}, "commit"=>"Save Template", "guide_id"=>"ghj", "id"=>"ijijij"}
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mGuide Load (0.1ms)[0m [1mSELECT "guides".* FROM "guides" WHERE "guides"."slug" = ? ORDER BY "guides"."id" ASC LIMIT 1[0m [["slug", "ghj"]]
[1m[35mCategory Load (0.1ms)[0m SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? ORDER BY "categories"."id" ASC LIMIT 1 [["slug", "ijijij"]]
[1m[36m (0.1ms)[0m [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ?[0m [["category_id", 6]]
[1m[35m (0.0ms)[0m SELECT "check_category_item_keys"."name" FROM "check_category_item_keys" WHERE "check_category_item_keys"."category_id" = ? [["category_id", 6]]
[1m[36m (0.1ms)[0m [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?[0m [["category_id", 6], ["key_type", 1]]
[1m[35m (0.0ms)[0m SELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ? [["category_id", 6], ["key_type", 2]]
[1m[36m (0.1ms)[0m [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?[0m [["category_id", 6], ["key_type", 3]]
[1m[35mGameModsRelationship Exists (0.1ms)[0m SELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 3 LIMIT 1 [["user_id", 1]]
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mCategoryItemKey Exists (0.1ms)[0m SELECT 1 AS one FROM "category_item_keys" WHERE ("category_item_keys"."name" IS NULL AND "category_item_keys"."guide_id" IS NULL) LIMIT 1
[1m[36mCategory Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "categories" WHERE (LOWER("categories"."name") = LOWER('ijijijddd') AND "categories"."id" != 6 AND "categories"."guide_id" = 3) LIMIT 1[0m
[1m[35m (0.0ms)[0m rollback transaction
[1m[36mCACHE (0.0ms)[0m [1mSELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 3 LIMIT 1[0m [["user_id", 1]]
Rendered shared/_error_messages.html.erb (0.5ms)
Rendered categories/edit.html.erb within layouts/application (5.6ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.7ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 157ms (Views: 145.1ms | ActiveRecord: 1.1ms)`
You need to add error_messages helper tag in the partial. That handles all error messages and show it properly.
It looks like your database has an existing record with the same name. Try inputting a unique name in. In your model, it is checking for :uniqueness.

Resources