0

I have some problem with ajax

This request GET http://localhost:3000/projects/new (304 Not Modified) does work, it executes javascript from response:

$('#add-project-button').hide();
$('#project-form').append("<form accept-charset=\"UTF-8\" action=\"/projects\" class=\"new_project\" data-remote=\"true\" id=\"new_project\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"gzu9DETiVV8N+OtRnkdYpD6aa0/RbM7i2H+nkQ0yXGc=\" /><\/div>\n  <div class=\"field\">\n    <label for=\"project_name\">Name<\/label><br />\n    <input id=\"project_name\" name=\"project[name]\" size=\"30\" type=\"text\" />\n  <\/div>\n  <div class=\"actions\"><input name=\"commit\" type=\"submit\" value=\"Create Project\" /><\/div>\n<\/form>");

But another request POST http://localhost:3000/projects (200 OK) seems ok but response is empty, no javascript is executing, but the server answer is ok:

Rendered tasks/_task.html.erb (0.0ms)
  Rendered projects/_project.html.erb (4.5ms)
  Rendered shared/_create.js.erb (5.4ms)
  Rendered projects/create.js.erb (6.1ms)
Completed 200 OK in 96ms (Views: 8.6ms | ActiveRecord: 84.9ms)

So I'm guessing that the browser somehow is of not supposed to execute javascript from server in this case.

What might be a problem? How to make browser receive the javascript and execute it? Thanks.

UPDATE: The form which is making ajax request:

<%= form_for(@project, :remote => true) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="actions"><%= f.submit %></div>
<% end %>

The controller action:

def create
  @project = current_user.projects.build(params[:project])
  respond_with(@project.tap(&:save))
end

Maybe the problem is causing by using partial as layout, projects/create.js.erb:

<% render :layout => '/shared/create', locals: { obj: @project } do %>
  $('#project-container').append("<%= escape_javascript render(@project) %>");
  $('#add-project-button').show('fast');
  $('#project-form').hide('fast', function(){ $('#project-form').empty(); });
<% end %>
3
  • why don't you have a respond_to do |format| block in the controller create action? That way you can ensure the js file is being triggered. According to the log you posted, it seems that html partials are being rendered too instead of just js. I'm not sure if this is the issue but I don't really see anything else Commented Aug 8, 2012 at 1:35
  • respond_with is designed to respond with proper format, i don't see the trouble here and last rendered file is javascript, so it's for sure js. But I'll try some of yours variants to make sure. Commented Aug 8, 2012 at 1:39
  • yeah I don't see anything that should be an issue :( I'm assuming you've tripple checked that there is no syntax error in your .js file. Commented Aug 8, 2012 at 1:40

1 Answer 1

1

The problem was very simple, I forget to put <%= sign in erb, so it should be:

<%= render :layout => '/shared/create', locals: { obj: @project } do %>
  $('#project-container').append("<%= escape_javascript render(@project) %>");
  $('#add-project-button').show('fast');
  $('#project-form').hide('fast', function(){ $('#project-form').empty(); });
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.