A
A
Andrey2017-07-19 23:12:12
ruby
Andrey, 2017-07-19 23:12:12

Why are the parameters not coming to the action?

I can't figure out what's wrong. send data with ajax

$(document).ready(function() {
  $("#btn").click(function(event) {

    event.preventDefault();

    $.ajax({
      type: 'POST',
      url: '/moods',
      data: 'rating=4&remark=Boston'
    });
  });
});

In the logs I see:
D, [2017-07-19T23:08:22.527103 #19660] DEBUG -- :    (0.4ms)  BEGIN
D, [2017-07-19T23:08:22.542988 #19660] DEBUG -- :   SQL (1.0ms)  INSERT INTO "moods" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  
D, [2017-07-19T23:08:22.566071 #19660] DEBUG -- :    (22.1ms)  COMMIT
D, [2017-07-19T23:08:22.567071 #19660] DEBUG -- :    (0.4ms)  BEGIN
D, [2017-07-19T23:08:22.568624 #19660] DEBUG -- :    (0.3ms)  COMMIT
127.0.0.1 - - [19/Jul/2017:23:08:22 +0300] "POST /moods HTTP/1.1" 303 - 0.2144
127.0.0.1 - - [19/Jul/2017:23:08:24 +0300] "GET / HTTP/1.1" 200 3714 0.1664

Those. params rating=4&remark=Bostondid not come.
+ returns 303 status? why, I direct the request to the action /moods, i.e. my create.
The problem is that the data basically does not reach the action. I remove ajax and everything works fine.
post '/moods' do
  @mood = Mood.create(params[:mood])

  if @mood.save
    redirect :'/'
  else
    redirect :'/'
  end
end


In general, what's the trouble here?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
McBernar, 2017-07-20
@McBernar

Data via GET, but using POST?

A
Andrey Demidenko, 2017-07-20
@Dem1

data: { mood: { rating: 4, remark: 'Boston' } }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question