18

With simple controller:

  def new
    @product = Product.new
    respond_to do |format|
      format.html #new.html.erb
      format.json { render json: @product}
    end
  end

  def create
    @product = Product.new(params[:product])
    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: "Save process completed!" }
        format.json { render json: @product, status: :created, location: @product }
      else
        format.html { 
          flash.now[:notice]="Save proccess coudn't be completed!" 
          render :new 
        }
        format.json { render json: @product.errors, status: :unprocessable_entity}
      end
    end
  end

and simple ajax request

$("h1").click ->
  $.post
    url: "/products/"
    data:
        product:
            name: "Filip"
            description: "whatever"

    dataType: "json"
    success: (data) ->
      alert data.id

im trying to send new product but server answers

[2013-07-09 18:44:44] ERROR bad URI `/products/[object%20Object]'.

and nothing changes in database. Why instead of getting /products uri its taking prducts/[oobject] thing? Whats wrong there?

Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
  • 1
    Just a side note, but respond_to do |format| is unnecessary as of Rails 3. You can use respond_to :html, :xml at teh top of the controller and then respond_with @object_name in the action. See this Railscast if you're interested: http://railscasts.com/episodes/224-controllers-in-rails-3?view=asciicast – Brian Dear Jul 11 '13 at 02:18

1 Answers1

34

Try this out:

CoffeeScript

$ ->
  $("h1").click ->
    $.ajax({
      type: "POST",
      url: "/products",
      data: { product: { name: "Filip", description: "whatever" } },
      success:(data) ->
        alert data.id
        return false
      error:(data) ->
        return false
    })

ES6-ified

$(() => $("h1").click(() => $.ajax({
  type: "POST",
  url: "/products",
  data: { product: { name: "Filip", description: "whatever" } },
  success(data) {
    alert(data.id);
    return false;
  },
  error(data) {
    return false;
  }
})));
dasnixon
  • 988
  • 7
  • 18
  • Why do you return false and...why it changed anything? It's working but still dont get it why. – Filip Bartuzi Jul 09 '13 at 23:08
  • 2
    Might be some good explanation here http://stackoverflow.com/questions/855360/when-and-why-to-return-false-in-javascript – dasnixon Jul 09 '13 at 23:12
  • Thanks! I found its useful. great! – Filip Bartuzi Jul 09 '13 at 23:14
  • I'm not sure that returning false in the success or error function does anything. I usually return false at the end of the event function. So in this case, at the very end of the code snippet. – Julien Dec 11 '14 at 01:42
  • @vipin8169 `$.post`and `$.ajax` have different syntaxes for parameters. `jQuery.post( url [, data ] [, success ] [, dataType ] )` has no keys before values. It's simple `$.post( "api/json.php", { name: "John", time: "2pm" } );` – Paul Geisler Apr 11 '16 at 15:14
  • It’s hilarious seeing all these coffeescript examples considering it’s pretty much dead. – fatfrog Aug 30 '19 at 06:10