F
F
Fahrenhei72015-11-02 10:47:55
JavaScript
Fahrenhei7, 2015-11-02 10:47:55

How to leave the user on the page after submitting the form?

There is a form, after accepting which, there is a redirect to the path of the name of the model / controller, which I do not need (redirect). The form is on the main one, everything is sent to the database, only you need to make sure that there is no redirection, and when Order.count is changed, jquery code is executed, and the page is not reloaded. What we have:
1. Form. I don't think it will help solve the problem. Just a form with two text fields and a button.
2. A controller that is both root and creates a new Order

class OrdersController < ApplicationController
  def mainp
  end
  def help
  end
  def list
    @order = Order.all
  end
  def new
    @order=Order.new
  end
  def create
    @order = Order.new(order_params)
    if @order.save
      #
    else
      #
    end
  end
  def order_params
    params.require(:order).permit(:name, :phone)
  end
end

3. Migration files
class CreateOrders < ActiveRecord::Migration
  def change
    create_table :orders do |t|
      t.string :name
      t.string :phone

      t.timestamps null: false
    end
  end
end

+
class AddIndexToOrdersPhone < ActiveRecord::Migration
  def change
    add_index :orders, :phone, unique: false
  end
end

4. And the actual jqery code that should be executed if Order.count has changed
$(document).ready(function(){
  $("#success-b").on("click", function(){
    if (/*<%= assert_difference 'Order.count'*/) {
      console.log("1")
    } else {
      console.log("2")
    }
  })
})


All examples are approximate and far from production. Roughly speaking, if there was a universal PL, you need to do it in such a way that
def create
    @order = Order.new(order_params)
    if @order.save
      "JQUERY CODE" без перезагрузки страницы
    else
      #
    end
  end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Zelensky, 2015-11-02
@Fahrenhei7

jquery ajax google

I
inDeepCode, 2015-11-02
@inDeepCode

<form ... action="">
not?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question