0

I'm trying to request a 'POST' for a Controller Method which looks like this:

$.ajax({type: "POST", url: something, data: {to_find_id: 978636122}, success:function(){ }});

It sends my Data, which shows the following log:

Started POST "/thingys/519716477/do_something" for 127.0.0.1 at 2014-04-03 14:54:44 +0200
Processing by ThingysController#do_something as */*
  Parameters: {"to_find_id"=>"978636122", "thingy_id"=>"519716477"}
  ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT "users".* FROM "users" WHERE "users"."id" =? LIMIT 1←[0m  [["id", 702273327]]
Redirected to http://localhost:3000/
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)

A short explanation of 302 Found: WIKIPEDIA-LINK

My method:

def do_something
  Something.create(timespan_id: params[:to_find_id].to_s, thingy_id: @thingy.id)
  respond_to do |format|
    format.js {render :nothting => true}
  end
end

I've allready defined my method in my routes.rb-file.

resources :thingys do
  post 'do_something'
  post 'undo_something'
end

So it sends the request, even finds the method.

Do you know, what can I do, to make it work?


EDIT:

I've added my Authenticity Token, but I still can't access my method.

Here is what I get now:
Started POST "/thingys/519716477/add_something?&authenticity_token=1l3tTWfwNgt56lp93w2QghUJvUYZn87V/8Zs7Fcrijq+8=" for 127.0.0.1 at 2014-04-07 13:04:15 +0200
Processing by ThingysController#add_something as */*
  Parameters: {"to_find_id"=>"310026847", "authenticity_token"=>"1l3tTWfwNgt56lp93w2QghUJvUYZn87V/8Zs7Fcrijq 8=", "subgroup_id"=>"519716477"}
  ←[1m←[35mUser Load (0.5ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 702273327]]
  ←[1m←[36mThingyp Load (0.5ms)←[0m  ←[1mSELECT "subgroups".* FROM "thingys" WHERE "thingys"."id" = ? LIMIT 1←[0m  [["id", "519716477"]]
Redirected to http://localhost:3000/
Completed 302 Found in 22ms (ActiveRecord: 1.0ms)

I have also added protect_from_forgery with: :exception in my ApplicationController and skip_before_filter:verify_authenticity_token in my ThingysController.

Saggex
  • 2,980
  • 2
  • 19
  • 33

2 Answers2

1

you cant send POST param in rails without authenticity token in rails.

Refer to the CSRF Countermeasure in this page : http://guides.rubyonrails.org/security.html

or to this thread Understanding the Rails Authenticity Token

Community
  • 1
  • 1
0

I'm assuming that "doesn't work" means the action is not preforming that task you expect, which would be the creation of a Something instance. If I had to guess, that something is failing to pass the validations defined on the model. This will prevent the model from saving.

You can try: Something.create!(attrs) instead, which will raise an error if validations fail, making it clearer why it's not saving.

To get it to work, you just need to save a valid Something, whatever that means to your application.


You also have a typo

format.js {render :nothting => true}
#                      ^ typo!
Alex Wayne
  • 145,435
  • 42
  • 271
  • 302
  • Hello Alex, thank you for your fast response, It does not do anything. I've tried to add some code, to see, if it calls the method, like adding a line with `puts "-----IT WORKS-----"` into my method, but it does not print it. So it does not execute my method. – Saggex Apr 07 '14 at 07:05