My client app sends JSON encoded POST to rails server but the server shows 406 error and doesn't respond to json.
UsersController create
# POST /users
# POST /users.json
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
Rails console:
Started POST "/users.json" for 127.0.0.1 at 2013-05-21 16:38:47 +0100
Processing by Devise::RegistrationsController#create as JSON
Parameters: {"user"=>{"name"=>"", "available"=>"true", "email"=>"", "sex"=>"male", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
WARNING: Can't verify CSRF token authenticity
(0.2ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '' LIMIT 1
(0.2ms) rollback transaction
Completed 406 Not Acceptable in 773ms (ActiveRecord: 6.1ms)
Can anyone help me?
Going to try to answer this a while later in case anyone sees it. I've run into this problem over and over even when I've only had JSON as the format option at the bottom of my controller (like this in your example):
respond_to do |format|
format.json { render json: #user, status: :created, location: #user }
end
Even when calling it explicitly with $.getJSON on my requests which does call it with JSON data types.
The trick I found was calling it with .json on my page name.
$.getJSON('controllername-your-calling.json?', function(json){
//do your stuff...
});
Maybe it helps someone.
You'll need to add respond_to :json, only: [:create] at the top of your controller. You may need to add respond_to :html for the actions that take HTML.
Related
I have read countless SO questions about this issue, and can not find a solution.
I have a button that updates Meeting here:
<%= form_for(meeting) do |f| %>
<%= f.hidden_field :accepted, value: true %>
<%= button_tag(type: 'submit', class: "btn_primary") do %>
Accept <svg><use xlink:href="#checkmark"/></svg>
<% end %>
<% end %>
I get an unknown format error, BUT it still updates. What is causing this error?
I have tried removing respond_to do |format| from the update method. which solves the problem for this one button, but then breaks all the other buttons on the platform that calls meetings/update.
class MeetingsController < ApplicationController
respond_to :json, :html
def update
respond_to do |format| **error highlights this line**
if #meeting.update(meeting_params)
format.json { render :show, status: :ok, location: #meeting }
else
format.html { render :edit }
format.json { render json: #meeting.errors, status: :unprocessable_entity }
end
end
end
end
Console:
Started PATCH "/meetings/224" for 127.0.0.1 at 2016-11-11 16:05:27 -0500
Processing by MeetingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hK6AoOZuw9DWyKUXw1dXWOSUolooWgBUPnkItUJX5Tm7XvimsHd9518pkqwVvNhUi3L3vlA4OZaJZiAgrbS0Ig==", "meeting"=>{"accepted"=>"true"}, "button"=>"", "id"=>"224"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Meeting Load (0.2ms) SELECT "meetings".* FROM "meetings" WHERE "meetings"."id" = ? LIMIT 1 [["id", 224]]
(0.3ms) begin transaction
SQL (0.5ms) UPDATE "meetings" SET "accepted" = ?, "updated_at" = ? WHERE "meetings"."id" = ? [["accepted", "t"], ["updated_at", "2016-11-11 21:05:27.882521"], ["id", 224]]
(1.3ms) commit transaction
Completed 406 Not Acceptable in 24ms (ActiveRecord: 2.7ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/meetings_controller.rb:65:in `update'
This was never an issue, and seemingly came out of no where. What is the cause of this error? Thanks!
I think I know what the issue is. I've added a line to your update method
class MeetingsController < ApplicationController
respond_to :json, :html
def update
respond_to do |format| **error highlights this line**
if #meeting.update(meeting_params)
format.html { redirect_to some_rails_path }
format.json { render :show, status: :ok, location: #meeting }
else
format.html { render :edit }
format.json { render json: #meeting.errors, status: :unprocessable_entity }
end
end
end
end
The reason you're getting an unknown format error but still successfully updating meeting is because your method has solid logic, so it's updating the meeting, but then once it does that, and you're only telling it what to do in terms of JSON but not in terms of HTML, you address both in your else statement, but not if the update succeeds.
Is this supposed to be an ajax request? It doesn't seem to be. But you're rendering json instead of html. If you're going to allow the page to reload you need to add an html option to if #meeting.update(meeting_params)
Something like: format.html { redirect_to #meeting, notice: 'Meeting was successfully updated.' }
If you're doing an ajax request this isn't the correct answer, but I don't see any indication in your question that you are. Lmk if you need further clarification.
I am trying to update a post, and ever since I added the Redcarpet gem, I get an error when I try to update a post.
Here is the error: undefined method 'update' for nil:NilClass
Here is my posts controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find_by_urlid(params[:urlid])
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edite
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.urlid = SecureRandom.urlsafe_base64
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Your Post was successfully created!' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Your Post was successfully updated!' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'You have successfully deleted the Post!' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find_by_urlid(params[:urlid])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:urlid, :author, :title, :body, :likes, :dislikes, :tags)
end
end
I've look at other posts, and their answers don't work. Any ideas?
Edit:
Server Log:
Started PATCH "/posts/sfqm5y99cbomqh4nmuxq5w" for 127.0.0.1 at 2014-10-31 13:05:07 -0700
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FRRgB2pI6GAQa6YL8KLZrUShshjjGd7+IXrLv1hTi5E=", "post"=>{"author"=>"Un3qual", "title"=>"First post", "body"=>" on new *dev* system **:D**", "likes"=>"9999", "dislikes"=>"0", "tags"=>""}, "commit"=>"Update Post", "urlid"=>"sfqm5y99cbomqh4nmuxq5w"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."urlid" = 'sfqm5y99cbomqh4nmuxq5w' LIMIT 1
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `update' for nil:NilClass):
app/controllers/posts_controller.rb:46:in `block in update'
app/controllers/posts_controller.rb:45:in `update'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.9ms)
Started PATCH "/posts/sfqm5y99cbomqh4nmuxq5w" for 127.0.0.1 at 2014-10-31 15:42:18 -0700
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FRRgB2pI6GAQa6YL8KLZrUShshjjGd7+IXrLv1hTi5E=", "post"=>{"author"=>"Un3qual", "title"=>"First post", "body"=>" on new *dev* system **:D**", "likes"=>"9999", "dislikes"=>"0", "tags"=>""}, "commit"=>"Update Post", "urlid"=>"sfqm5y99cbomqh4nmuxq5w"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."urlid" = 'sfqm5y99cbomqh4nmuxq5w' LIMIT 1
Completed 500 Internal Server Error in 4ms
NoMethodError (undefined method `update' for nil:NilClass):
app/controllers/posts_controller.rb:46:in `block in update'
app/controllers/posts_controller.rb:45:in `update'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.2ms)
"Post.find_by_urlid(params[:urlid])" code has deprecated "find_by_" method. Rails 4 has deprecated "find_by_" methods. [Link]: http://edgeguides.rubyonrails.org/4_0_release_notes.html
Try Using: Post.find_by(urlid: params[:urlid]).
There is a typo error in "edite" method.
Based on the comments to your question, this is happening because there is no record with this urlid.
You should change your callback (before_action) filter like this.
def set_post
#post = Post.find_by(urlid: params[:urlid])
redirect_to posts_path, flash: {error: "The post does not exists"} if #post.nil?
end
This will redirect to the index of the posts, with an error message (you should have a div for errors flashing in your layout or your templates for it to appear).
If the #post exists, then it will relay to the action normally.
If you're not using any preloader which instantiate #post variable then it's the problem. For update action you should have:
def update
#post = Post.find params[:id]
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Your Post was successfully updated!' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
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"}
I been working on my application in which I have a number of roles and also some users.
However suddenly I can't sign-up anymore. Signing in works fine, but signing up always redirects me back to the signing page and produces the same error:
The console always shows the following when I try to sign up:
Started POST "/registers/user/registration" for 127.0.0.1 at 2013-04-18 00:48:22 +0100
Processing by RegistersController#registration as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ofucDxxzZFbTVLfVaA3QxixOHL2V9hzxfSFU/99Pd60=", "user"=>{"name"=>"Omar Arroum", "email"=>"omar#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "id"=>"user"}
Completed 401 Unauthorized in 1ms
Started GET "/users/sign_in" for 127.0.0.1 at 2013-04-18 00:48:22 +0100
Processing by Devise::SessionsController#new as HTML
Rendered devise/shared/_links.html.erb (0.4ms)
Rendered devise/sessions/new.html.erb within layouts/application (9.8ms)
Rendered layouts/_navigation.html.erb (1.3ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 63ms (Views: 61.6ms | ActiveRecord: 0.0ms)
I haven't made any changes to the `user_controller.rb, so not sure what is causing this issue
EDIT: Added the RegistersController
class RegistersController < ApplicationController
#before_filter :authenticate_user!
load_and_authorize_resource
# GET /registers
# GET /registers.json
def index
#registers = Register.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #registers }
end
end
# GET /registers/1
# GET /registers/1.json
def show
#register = Register.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #register }
end
end
# GET /registers/new
# GET /registers/new.json
def new
#register = Register.new(:event_id => params[:event_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: #register }
end
end
# GET /registers/1/edit
def edit
#register = Register.find(params[:id])
end
# POST /registers
# POST /registers.json
def create
#register = Register.new(params[:register])
respond_to do |format|
if #register.save
format.html { redirect_to #register, notice: 'Register was successfully created.' }
format.json { render json: #register, status: :created, location: #register }
else
format.html { render action: "new" }
format.json { render json: #register.errors, status: :unprocessable_entity }
end
end
end
# PUT /registers/1
# PUT /registers/1.json
def update
#register = Register.find(params[:id])
StudentRegister.find_all_by_register_id(#register).each do |streg|
streg.update_attributes(:present => false, :late => false)
#sets the users status as being absent, mainly for the purpose of when copying a new register,
#as the presence and lateness is otherwise maintained
end
respond_to do |format|
if #register.update_attributes(params[:register])
format.html { redirect_to #register, notice: 'Register was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #register.errors, status: :unprocessable_entity }
end
end
end
# DELETE /registers/1
# DELETE /registers/1.json
def destroy
#register = Register.find(params[:id])
#register.students.delete_all_on_destroy
#register.destroy
respond_to do |format|
format.html { redirect_to registers_url }
format.json { head :no_content }
end
end
def registration
#studentregister = StudentRegister.find_all_by_register_id(params[:id])
end
def copy
#register = Register.find(params[:id]).amoeba_dup #Creates a duplicate of the existing register with all Students in it
#register.date = Date.today #Sets the registers date to today's date
#register.save
respond_to do |format|
format.html { redirect_to edit_register_path(#register) } #Redirects to the edit page so that they can make any updates
format.json { head :no_content }
end
end
end
You are doing before_filter :authenticate_user! in your RegistersController (most likely). That is what produces the 401 response and that flash message with Devise. CanCan should have nothing to do with it. Post your RegistersController.
Ok figured out what the issue was.
Due to the fact that I had a file called Registration.html.erb and an action name registration my route looked like this:
match 'registers/:id/registration' => 'registers#registration', :as => 'registration' #Route for taking the register
Problem is that Devise uses this action name internally, so my code was being routed wrongly.
To solve this, I changed everything named registration to take_register, like so:
match 'registers/:id/take_register' => 'registers#take_register', :as => 'take_register' #Route for taking the register
That did the trick for me, and now all works
I am trying to learn how to post some data using $.ajax through jquery to a simple rails scaffold project. There is one standard scaffold created controller => Images
class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
#images = Image.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #images }
end
end
# GET /images/1
# GET /images/1.json
def show
#image = Image.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #image }
end
end
# GET /images/new
# GET /images/new.json
def new
#image = Image.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #image }
end
end
# GET /images/1/edit
def edit
#image = Image.find(params[:id])
end
# POST /images
# POST /images.json
def create
#image = Image.new(params[:image])
respond_to do |format|
if #image.save
format.html { redirect_to #image, notice: 'Image was successfully created.' }
format.json { render json: #image, status: :created, location: #image }
else
format.html { render action: "new" }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.json
def update
#image = Image.find(params[:id])
respond_to do |format|
if #image.update_attributes(params[:image])
format.html { redirect_to #image, notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
#image = Image.find(params[:id])
#image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
end
with one route => resources :images. The database schema consists of one field => t.string :name.
My initial test html file is:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"> </script>
</head>
<body>
<script>
$(document).ready(function(){
$.ajax({
type: 'POST', url: "localhost:3000/images",
data: { name: "johngalt" }
});
});
</script>
</body>
The result from webrick is:
Started POST "/images" for 127.0.0.1 at 2012-04-17 09:50:19 -0500
Processing by ImagesController#create as */*
Parameters: {"name"=>"johngalt"}
WARNING: Can't verify CSRF token authenticity
(0.1ms) begin transaction
SQL (63.5ms) INSERT INTO "images" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Tue, 17 Apr 2012 14:50:21 UTC +00:00], ["name", nil], ["updated_at", Tue, 17 Apr 2012 14:50:21 UTC +00:00]]
(2.0ms) commit transaction
Redirected to http://localhost:3000/images/7
Completed 302 Found in 81ms (ActiveRecord: 65.6ms)
I'm not sure why name doesn't contain "johngalt". Does this have something to do with the " WARNING: Can't verify CSRF token authenticity"?
Edit
When I use curl:
curl -d "image[name]=johngalt" localhost:3000/images.json
The record is created and the name field contains "johngalt". In essence, I'm trying to figure out the .ajax equivalent of doing what I was able to do in curl?
The CSRF token is automatically added in your post forms when you use the Rails form_for helper to create a form, and is meant to protect users against cross-site request forgery attacks. So, if you are trying to post in a javascript file you won't have access to the token.
You can disable CSRF token authentication for specific actions if you so wish, as long as you understand the consequences.
There are a few ways you can do that, listed here: Turn off CSRF token in rails 3
Edit Looking at your CURL example, it looks like you are ajax posting the wrong data. You are ommiting the 'image' param namespace. Try:
<script>
$(document).ready(function(){
$.ajax({
type: 'POST', url: "localhost:3000/images",
data: { image: { name: "johngalt" } }
});
});
</script>