Unable to sign_up using Devise and CanCan in Rails - ruby-on-rails

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

Related

New to ruby. Using baserails. Experencing problems

I have been recently trying to learn ruby by using ruby on rails with baserails. Everything was fine until I got in the spot in the tutorial where there wasn't much guidance on how to install s3 using paperclip the tutorial was for dropbox. I am so lost and have been getting errors for 2 days. I have researched this issue alot, but with my limited knowledge I am having a hard time understanding it. I know my issue is a paperclip/s3 issue. I have all my gems up to to date. Could someone please work with me and help guide me in fixing my issue. Here is my listings_controller.fb file. Line 66 gives me this error.
uninitialized constant ListingsController::Listing.
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
# GET /listings
# GET /listings.json
def index
#listings = Listing.all
end
# GET /listings/1
# GET /listings/1.json
def show
end
# GET /listings/new
def new
#listing = Listing.new
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
#listing = Listing.new(listing_params)
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render :show, status: :created, location: #listing }
else
format.html { render :new }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /listings/1
# PATCH/PUT /listings/1.json
def update
respond_to do |format|
if #listing.update(listing_params)
format.html { redirect_to #listing, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: #listing }
else
format.html { render :edit }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
______________Terminal_______________-
Started GET "/listings/1" for 127.0.0.1 at 2017-02-09 21:36:39 +0900
Processing by ListingsController#show as HTML
Parameters: {"id"=>"1"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
NameError (uninitialized constant ListingsController::Listing):
app/controllers/listings_controller.rb:67:in `set_listing' Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
(5.2ms) Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms) Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(1.3ms) Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout (161.7ms)

undefined method `update' for nil:NilClass post scaffold

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

rails generate scaffold command on Windows causes ArgumentError

I used "rails generate scaffold project" to create an new web application. I already did this in the past on Linux and Mac OSX running other versions of rails and ruby and all worked fine, but this time I'm working on Windows 7. Here is my environment
C:\Users\user1\Company>ruby -v
ruby 2.0.0p451 (2014-02-24) [x64-mingw32]
C:\Users\user1\Company>rails -v
DL is deprecated, please use Fiddle
Rails 4.1.0
C:\Users\user1\Company>
after I ran the scaffold command, I ran rake db:migrate and I was able to create my first project successfully. Then I can edit the project, but when I click update, I get the following error message
ArgumentError (When assigning attributes, you must pass a hash as an argument.):
Full server log message
Started PATCH "/projects/1" for 127.0.0.1 at 2014-04-29 05:16:33 -0700
Processing by projectsController#update as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"gST6BUQNwOZQDYVj60DXLuFANv1JsM02YAIM+xYwt/M=", "commit"=>"Update project", "id"=>"1"}
project Load (0.0ms) SELECT "projects".* FROM "projects" WHERE "projects"."id"= ? LIMIT 1 [["id", 1]]
(1.0ms) begin transaction
(0.0ms) rollback transaction
Completed 500 Internal Server Error in 10ms
ArgumentError (When assigning attributes, you must pass a hash as an argument.):
app/controllers/projects_controller.rb:44:in `block in update'
app/controllers/projects_controller.rb:43:in `update'
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (104.0ms)
Here is my "update" method (as was created automatically by the scaffold command)
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to #project, notice: 'Update Successful!' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
I tried removing the "PATCH/" keyword, but no luck. I replaced the whole method with the following (this worked for my other application, but not this time on Windows)
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
#project = Project.find(params[:id])
if #project.update_attributes(params[:project])
flash[:notice] = "Update Successful!"
end
respond_with(#project)
end
but this did not make any difference.
I also tried (I found this by browsing SO)
# PUT /projects/1
# PUT /projects/1.json
def update
#project = Project.find(params[:id])
respond_to do |format|
if #project.update_attributes(params[:project])
format.html { redirect_to #project, notice: 'Update Successful!' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
but no success either
Here are the other working methods (all as automatically generated by "rails generate scaffold" command, and they all work fine)
# GET /projects
# GET /projects.json
def index
#projects = project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
#project = project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
#project = project.new(project_params)
respond_to do |format|
if #project.save
format.html { redirect_to #project, notice: 'Creation Successful!' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
Why is "rails generate scaffold" command not working on Windows and working fine on Linux and Mac OSX?
Update 1
Here are the other methods that were automatically created by "rails generate scaffold" command
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
<other methods listed above : index, show, new, edit, create, update, and destroy>
private
# Use callbacks to share common setup or constraints between actions.
def set_project
#project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params[:project]
end
end
Working Code after making the changes suggested by Kirti Thorat
This is what worked for me
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
#project = Project.find(params[:id])
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to #project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
#project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:UnitMgtAddress)
end
You just created a scaffold without any other fields. So your projects table simply has 3 default fields id, created_at and updated_at. Because of which in your update action when you do:
if #project.update(project_params)
Or
if #project.update_attributes(params[:project])
you get error as update and update_attributes require a Hash as an argument and params[:project] is nil. Look at your params hash from server log
{"utf8"=>"√", "authenticity_token"=>"gST6BUQNwOZQDYVj60DXLuFANv1JsM02YAIM+xYwt/M=", "commit"=>"Update project", "id"=>"1"}
It doesn't have a key project.
Possible Solutions
Without adding new fields
If you are not planning to add any new fields in your projects table then there is no point in having an update action as what field would you update on?
With adding new fields
You can add new fields to your projects table like name, duration, etc as per your requirement (By creating a migration to add new fields).
After this you would just need to update the project_params method as below:
def project_params
params.require(:project).permit(:name, :duration)
end

Completed 406 Not Acceptable in 773ms

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.

Basic .ajax post to a rails 3.2.2 scaffold generating "WARNING: Can't verify CSRF token authenticity"

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>

Resources