Unable to process POST request for create action in controller - ruby-on-rails

I am using paperclip-5.0.0, and Rails 5.0.2.This is my code in controller
class PawsController < ApplicationController
before_action :set_paw, only: [:show, :edit]
def index
#paws = Paw.all
end
def show
end
def new
#paw = Paw.new
end
def edit
end
def create
#paw = Paw.new(paw_params)
respond_to do |format|
if #paw.save
format.html { redirect_to #paw, notice: 'Paw was successfully created.' }
format.json { render :show, status: :created, location: #paw }
else
format.html { render :new }
format.json { render json: #paw.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_paw
#paw = Paw.find(params[:id])
end
def paw_params
params.require(:paw).permit(:avatar,:name) if params[:avatar]
end
end
Avatar here is the column for paw which stores images.In model I have added required validations for paperclip and validation for presence of name.
Problem: In rails console I am successfully able to open the image file from my computer and add it to the paw model i.e. #paw.save returns true in console, but in the browser it redirects me to the new_paw_url with two errors.
Name can't be blank. and
Avatar can't be blank.
There are no exceptions or errors thrown in between the process.I have no idea how should I fix this.
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PawsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"zKpb2XFEG4FqX3aVQx9oyyGI9x2NpmF+lmt4eZy/3p3VZr2eWYvHLaEjS5AvisJ6iEbRm61SJkJSuTpMx4c0JA==", "paw"=>{"name"=>"Sheru", "gender"=>"Male", "breed"=>"", "dob"=>"", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x005608576e1f30 #tempfile=#<Tempfile:/tmp/RackMultipart20170323-14343-1o95nqx.jpg>, #original_filename="art.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"paw[avatar]\"; filename=\"art.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Upload"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendering paws/new.html.erb within layouts/application
Rendered paws/_form.html.erb (15.8ms)
Rendered paws/new.html.erb within layouts/application (17.3ms)
Rendered layouts/_navbar.html.erb (0.6ms)
Completed 200 OK in 326ms (Views: 287.5ms | ActiveRecord: 2.0ms)
Feel free to ask for any other code snippets from me.

Your paw_params method is breaking it. Remove the if statement and it should work. The problem you're having is that the params you're recieving through your controller are nested in the params[paw] hash and not the params hash. Your paw_params if statement should be
params.require(:paw).permit(:avatar,:name) if params[:paw][:avatar]
Look at the console of your params hash and you'll see that Rails will nest all params submitted via a form helper within the class name of the object. In this case it's paw.
"paw"=>{"name"=>"Sheru", "gender"=>"Male", "breed"=>"", "dob"=>"", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x005608576e1f30
More obviously:
params[:name] #nil
params[:paw][:name] #"Sheru"
Also, it's not a good practice to add conditionals for strong_paramaters based on the presence of certain attributes. If someone submits a name without an avatar, the name wont be permitted due to strong_parameters but would give the user poor feedback. Instead you should use validations in your Paw model that requires the presence of both the name and avatar.

Related

ActionController::ParameterMissing (param is missing or the value is empty: account)

Gurus,
Need help with RoR API
I see all the required parameters are being sent, however the server does not like one or some of it.
controller:
def create
#account = Account.new(account_params)
if #account.save
render json: #account, status: :created, location: #account
else
render json: #account.errors, status: :unprocessable_entity
end
end
def account_params
params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end
call looks like this:
Started POST "/accounts?name=TD-Trading&currency_id=1&institution=TD%20Waterhouse&type=Investment&client_id=1" for ::1 at 2020-11-27 01:16:08 -0700
Processing by AccountsController#create as /
Parameters: {"name"=>"TD-Trading", "currency_id"=>"1", "institution"=>"TD Waterhouse", "type"=>"Investment", "client_id"=>"1"}
Client Load (0.4ms) SELECT "clients".* FROM "clients" WHERE "clients"."email" = $1 ORDER BY "clients"."id" ASC LIMIT $2 [["email", "akaghzi#gmail.com"], ["LIMIT", 1]]
Completed 400 Bad Request in 271ms (ActiveRecord: 0.4ms | Allocations: 1706)
ActionController::ParameterMissing (param is missing or the value is empty: account):
app/controllers/accounts_controller.rb:55:in account_params' app/controllers/accounts_controller.rb:21:in create'
This piece of code:
def account_params
params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end
Is saying that it expects an account hash with inside that the attributes. Your logs shows you send all attributes not wrapped inside an account hash.
You can solve this by wrapping the params inside a account hash or remove the require(:account) part from the account_params.

redirect_to inside controller action doesn't work

I use Rails 5.1 and the "redirect_to #search" inside create action doesn't work.
My SearchesController:
class SearchesController < ApplicationController
def new
#search = Search.new
end
def create
#search = Search.create!(search_params)
redirect_to #search # search_path(#search) doesn't work either
end
def show
#search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit!
end
end
After creating a new search entry by clicking the submit button it doesn't redirect to show page.
My app/views/searches/new.html.erb:
<div>
<h1>Advanced Search Form</h1>
<%= form_with model: #search do |form| %>
<%= form.text_field :keywords %>
<%= form.select :ort, options_from_collection_for_select(Imagecapturing.cities, :ort, :city_name, prompt: false, include_blank: false) %>
<%= form.submit("Suchen", :id=>"button", :class=>"Test", :name=>"submit") %>
<% end %>
</div>
config/routes.rb:
Rails.application.routes.draw do
root 'imagecapturings#index'
resources :searches
end
Log:
Started POST "/searches" for ::1 at 2018-06-14 17:37:54 +0200
Processing by SearchesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"B91lIUxZZvanOx1luhhfWBJ9mAO5Np/6Bx4xzPdv2Ygj29bprWk5+wIBP7kMVl5Eoxz0KcyJF5DK8UaVUhQaFQ==", "search"=>{"keywords"=>"", "ort"=>"A-St. Paul"}, "submit"=>"Suchen"}
(0.6ms) BEGIN
SQL (22.8ms) INSERT INTO `searches` (`keywords`, `ort`, `created_at`, `updated_at`) VALUES ('', 'A-St. Paul', '2018-06-14 15:37:58', '2018-06-14 15:37:58')
(11.1ms) COMMIT
Redirected to http://localhost:4000/searches/23
Completed 302 Found in 50ms (ActiveRecord: 34.5ms)
Started GET "/searches/23" for ::1 at 2018-06-14 17:37:58 +0200
Processing by SearchesController#show as JS
Parameters: {"id"=>"23"}
Search Load (0.7ms) SELECT `searches`.* FROM `searches` WHERE `searches`.`id` = 23 LIMIT 1
Rendering searches/show.html.erb within layouts/application
Imagecapturing Load (10.4ms) SELECT `imagecapturing`.* FROM `imagecapturing` WHERE (ort LIKE '%A-St. Paul%') ORDER BY `imagecapturing`.`id` DESC
Rendered searches/show.html.erb within layouts/application (62.5ms)
Rendered layouts/_top_nav.html.erb (6.0ms)
Imagecapturing Load (17.0ms) SELECT distinct(ort) FROM `imagecapturing` ORDER BY `imagecapturing`.`ort` ASC
Rendered searches/_links.html.erb (33.5ms)
Completed 200 OK in 409ms (Views: 340.1ms | ActiveRecord: 28.0ms)
Is in "Processing by SearchesController#show as JS" the "as JS" part the issue?
How can I get the redirect working so that after clicking the submit button it redirects to the show action?
--- UPDATE
With suggestion of user Rockwell I modified the create action to:
def create
#search= Search.create!(search_params)
respond_to do |format|
if #search
format.html {redirect_to #search}
format.json { render :show, status: :created, location: #step }
format.js { redirect_to #search }
else
format.html { render :new }
format.json { render json: #search.errors, status: :unprocessable_entity }
format.js { render :new }
end
end
end
But it still processes "as JS":
Started POST "/searches" for ::1 at 2018-06-14 18:09:18 +0200
Processing by SearchesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"D7l5ibv8hu08Z18VKqa0Y+iqGHfT+SIZTIoM8vHdcpArv8pBWszZ4Jldfcmc6LV/Wct0XaZGqnOBZXurVKaxDQ==", "search"=>{"keywords"=>"", "ort"=>"Cologny"}, "submit"=>"Suchen"}
(0.2ms) BEGIN
SQL (11.8ms) INSERT INTO `searches` (`keywords`, `ort`, `created_at`, `updated_at`) VALUES ('', 'Cologny', '2018-06-14 16:09:18', '2018-06-14 16:09:18')
(17.0ms) COMMIT
Redirected to http://localhost:4000/searches/30
Completed 302 Found in 36ms (ActiveRecord: 29.0ms)
Started GET "/searches/30" for ::1 at 2018-06-14 18:09:19 +0200
Processing by SearchesController#show as JS
Parameters: {"id"=>"30"}
Search Load (0.8ms) SELECT `searches`.* FROM `searches` WHERE `searches`.`id` = 30 LIMIT 1
Rendering searches/show.html.erb within layouts/application
Imagecapturing Load (28.1ms) SELECT `imagecapturing`.* FROM `imagecapturing` WHERE (ort LIKE '%Cologny%') ORDER BY `imagecapturing`.`id` DESC
Rendered searches/show.html.erb within layouts/application (16157.3ms)
Rendered layouts/_top_nav.html.erb (3.6ms)
Imagecapturing Load (27.6ms) SELECT distinct(ort) FROM `imagecapturing` ORDER BY `imagecapturing`.`ort` ASC
Rendered searches/_links.html.erb (39.2ms)
Completed 200 OK in 16404ms (Views: 16314.1ms | ActiveRecord: 56.5ms)
I believe that you are correct, the Processing by SearchesController#show as JS is what is causing your error. If you respond to the format it should solve your issue. I added HTML and JSON here but you wouldn't need to if you know you will never need that, just the js should be fine.
def create
#search = Search.new(search_params)
respond_to do |format|
if #search.save
format.html {redirect_to #search}
format.json { render :show, status: :created, location: #step }
format.js { redirect_to #search }
else
format.html { render :new }
format.json { render json: #search.errors, status: :unprocessable_entity }
format.js { render :new }
end
end
end
If you are sending this as an AJAX you could specify the type that gets sent as well, but I would need to see the JS code for that to give a solution to fix that.
it is working as expected, you can see it on the log, the insert is done and then the redirect too.
but the problem is the format you are asking. it seems like the create is called as js Processing by SearchesController#create as JS (maybe you are doing an ajax request with that format or your form has remote:true). and then the redirect isn't done visually of course. you need to make the create request without js format or don't make the redirect on the controller and create a "create.js" view that does the redirect. but of course it's not the best solution.
if you want to do the redirect, the best way is to change the format of the create request.

Date failing to update, but returning true for Update (Rails ActiveRecord)

I have a Rails model called user that has a birthday field with type date in Postgres.
My first issue is that I'm not sure how to update the value of a user. If I pass in a string like "10/10/1980", it does not update.
The second issue is that even when the birthday is not updated, Rails returns true for the User.update(user_params) action
My two questions:
How can I update a date field?
How can I make sure that rails throws an error if an incorrect date is passed in?
How can I update a date field?
Update method from controller below. Fairly standard from scaffolding.
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
puts(user_params)
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
Edit: Params:
def user_params
params.require(:user)
.permit(:points, :payment_option, :first_name, :last_name,
:payment_email, :phone_number, :facebook_id, :profile_pic_url,
:locale, :timezone, :gender, :email, :country, :city,
:age_group, :birthday, :employment_status, :occupation,
:education_level, :income_bracket)
end
Rails log (note that I added a puts(user_params) after the update statement.
Started PATCH "/api/v1/users/1" for 127.0.0.1 at 2017-12-07 19:16:37 +0800
Processing by UsersController#update as JSON
Parameters: {"user"=>{"birthday"=>"12dsafkljfsldk"}, "id"=>"1"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Can't verify CSRF token authenticity.
{"birthday"=>"10/10/1900"}
(0.2ms) BEGIN
(0.1ms) COMMIT
Rendering users/show.json.jbuilder
Rendered users/_user.json.jbuilder (0.6ms)
Rendered users/show.json.jbuilder (1.6ms)
Completed 200 OK in 31ms (Views: 5.9ms | ActiveRecord: 3.1ms)
I was hoping that I could use before_update to parse the date string into a Date object before saving, but it doesn't look like self.birthday is even showing up in the model.
Place this line in your ApplicationController
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
If it doesn't work then. Try to skip the action
skip_before_action :verify_authenticity_token

Carrierwave Cloudinary image upload works but null values returned right after upload

First of all, let me say - Cloudinary and Carrierwave are both amazing. Carrierwave, super simple image uploading.
Cloudinary offering free plan with 10GB of storage, very generous.
My Problem
I'm basically testing Cloudinary with Carrierwave.
I've got them both to work in the sense that when I create a new user (or update existing one) with an image file attachment using Postman app on my Mac, the images actually do get saved on the Cloudinary CDN and the URL values in my database afterwards but the JSON response values for avatar are null:
The problem you can see in the returned response, the avatar field all contains null values.
However, when I view my index action of my UsersController in my browser, the avatar values show up:
Any ideas what might be causing this issue?
Could it be that by the time the User entity is created/updated, Cloudinary hasn't finish uploading the info quite in time yet and so Carrierwave saves the value as null and later, when Cloudinary has finished processing, it makes a callback to Carrierwave to update the values?
I'm not doing anything fancy, just basic code:
UsersController
class UsersController < ApplicationController
def index
users = User.all
render json: users
end
def create
user = User.new(user_params)
if user.save
render json: user, status: :created
else
render json: user.errors, status: :unprocessable_entity
end
end
def update
# simple test, change to params id later
user = User.where(id: 3)
if user.update(user_params)
render json: user, status: :ok
else
render json: user.errors, status: :unprocessable_entity
end
end
private
def user_params
params.permit(:id, :first_name, :last_name, :email, :password, :password_confirmation, :avatar)
end
end
Mac Terminal output
Processing by UsersController#update as */*
Parameters: {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fa39caad5b0 #tempfile=#<Tempfile:/var/folders/sk/tjj1jxkd62ngkhfxmh8yc04r0000gn/T/RackMultipart20170918-2307-lw2ga8.jpg>, #original_filename="doc.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"doc.jpg\"\r\nContent-Type: image/jpeg\r\n">, "first_name"=>"Emmett", "last_name"=>"Brown", "id"=>"3"}
Can't verify CSRF token authenticity.
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? [["id", 3]]
(0.1ms) begin transaction
SQL (0.4ms) UPDATE "users" SET "first_name" = ?, "avatar" = ?, "updated_at" = ? WHERE "users"."id" = ? [["first_name", "Emmett"], ["avatar", "kzc9ghjln9gfoowhzudi.png"], ["updated_at", "2017-09-18 02:55:59.269637"], ["id", 3]]
SQL (0.1ms) UPDATE "users" SET "avatar" = 'image/upload/v1505703360/kzc9ghjln9gfoowhzudi.png' WHERE "users"."id" = ? [["id", 3]]
(2.7ms) commit transaction
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (3.52ms)
Completed 200 OK in 3259ms (Views: 14.9ms | ActiveRecord: 4.0ms)
Hopefully it's some silly mistake or quick configuration option that I am not aware of and not some bug on either Carrierwave or Cloudinary's side.
Has anyone encountered this problem before?
Umm...okay, I think I found a solution/work around?
Maybe some Rails or Carrierwave devs can explain to me why I need to do this to fix the problem:
def create
user = User.new(user_params)
if user.save
# -------------------------------------------------
# NEED TO RELOAD FOR CARRIERWAVE
# -------------------------------------------------
user.reload
render json: user, status: :created
else
render json: user.errors, status: :unprocessable_entity
end
end
Seems like an extra step which can impact system performance.
I shouldn't need to do this right?

Writing to Rails with a JSON API

I've been trying to write to a regular one model rails app from POSTman for a few days now, and can't figure out how to do it, or find any information on how to do it.
My app has a User model with a name field. All I'm trying to do is change the name remotely via JSON using POSTman.
Any help on how to do this is appreciated, including a link to a basic resource on how to do it.
I imagine this is pretty basic.
EDIT: here is a screenshot from the POSTman output
EDIT 2:
from server log:
Started PUT "/users/1.json" for 127.0.0.1 at 2013-07-17 16:53:36 -0400
Processing by UsersController#update as JSON
Parameters: {"name"=>"Jeff", "id"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
(0.1ms) commit transaction
Completed 204 No Content in 3ms (ActiveRecord: 0.4ms)
The transaction is happening, but the record isn't actually being updated. Do I need to change something in the controller? It's just a regular rails generated controller with no changes.
EDIT 3:
Here is my output from just going to http://localhost:3000/users/1.json
{
"created_at": "2013-07-02T21:51:22Z",
"id": 1,
"name": "Arel",
"updated_at": "2013-07-02T21:51:22Z"
}
Again, I've changed nothing from the scaffold, and I haven't been able to figure out how to format the JSON to nest it under user like the answer suggests.
Here is the relevant part of my controller:
# GET /users/1
# GET /users/1.json
def show
#user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #user }
end
end
# PUT /users/1
# PUT /users/1.json
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { }
else
format.html { render action: "edit" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
I don't understand why this is so difficult. I'm just trying to update a name via JSON ...
For the controller you want to post to:
protect_from_forgery with: :null_session
or like from: How do I bypass protect_from_forgery in Rails 3 for a Facebook canvas app?
skip_before_filter :verify_authenticity_token, :only => [THE ACTION]
EDIT
Your JSON is incorrect... you are posting
{"name"=>"Jeff", "id"=>"1"}
Since your controller does user.update_attribute(params[:user]), your JSON needs to be under a user attribute
{
"id": 1,
"user": {
"name": "Jeff"
}
}
This will create a hash of
{"user"=>{"name"=>"Jeff"}, "id"=>"1"}

Resources