简体   繁体   中英

Ruby on Rails - Make an AJAX request and render the partial of the resulting model

I have looked everywhere for an answer to my doubt, but none of them worked for me (I probably did something wrong).

I am very new to Ruby/Ruby on Rails, so I do not know very much how things work.


I have two models in my application

  • Attribute name:string and;

  • Product name:string .

I do also have two controllers:

  • attributes_controller and;

  • products_controller .

I also have the following view ../app/views/product_models/new.html.erb . In this view, I want to be able to create an attribute and show it to the user right afterwards (using AJAX)

<div id="attributes_list">
  <%= render :partial => "attributes/attribute", :collection => @attributes, :as => :attribute %>
</div>

<div id="new_attribute">
  <strong>Adicionar novo atributo</strong> <br />
  Nome: <input type="text" class="name"><br />
  <span class="create_button">Criar</span>
</div>

for this situation, I have the following jquery (in ../app/assets/javascripts/application.js ):

$(document).ready(function () {
    var new_attribute_div = $(document).find('#new_attribute')
    new_attribute_div.on('click', '.create_button', function () {
        // alert('funfou');
        $.ajax({
            url: '/attributes',
            type: 'POST',
            dataType: "script",
            data: {
                name: new_attribute_div.find(".name").val()
            },
            success: function (data) {
                $(document)
                    .find('#attributes_list')
                    .append("<%= j render @attribute %>");

                new_attribute_div.find(".name").val("");

            },
            error: function (data) {
                alert("error:" + data)
            }
        });
    });
});

and my attributes_controller is just like this

class AttributesController < ApplicationController

  def create
    @attribute = Attribute.new(attribute_params)
    @attribute.save

    respond_to do |format|
      format.html { redirect_to @attribute }
      format.js
      format.json
    end
  end
end

private
def attribute_params
  params.permit(:name)
end

The problem I am having is, when the following line of the jquery is executed

.append("<%= j render @attribute %>");

Instead of rendering the partial of the attribute, rails is rendering the string itself ( <%= j render @attribute %> )

What am I doing wrong? How can I render the @attribute that is returned from the controller?

You cannot use rails snippets or variables in assets

"<%= j render @attribute %>"

To fix this, you have to have .js.erb file in attributies/create.js.erb

In the file pass your render attribute

$(document)
      .find('#attributes_list')
      .append("<%= j render @attribute %>"

Remember, this will trigger only when request comes as JS.

奥古斯托,您应该在 #{ } 内的 jquery 中包含代码的 Rails 部分,所以它应该是:

.append("#{<%= j render @attribute %>}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM