N
N
Nazar Jeveling2014-08-03 14:40:26
JavaScript
Nazar Jeveling, 2014-08-03 14:40:26

AJAX not working with Rails app?

And again, I continue the block of questions about Rails applications. There is a todo application, it displays a list of all Projects and tasks for them. Below there is a form for adding a new Project. I added format.js to the controller code

def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to todo_path, notice: 'Project was successfully created.' }
 =>    format.js
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

Added remote: true to the form itself
<%= form_for(@project, remote: true) do |f| %>
        <%= f.text_field :name, :placeholder => ' Enter new project here..' %>
        <but><%= f.submit 'Add Project', remote: true %></but>
    <% end %>

Create.js.erb file created
$('#projects').html("<%= escape_javascript render('projects') %>");

Only here there is one caveat, the creation of a new project, that is, the form itself, I have it in Todo (app/views/todo/index.html.erb) and the controller for creating a new Project in (app/controllers/project_controller.rb ) , and above is the generated create.js.erb file at (app/views/projects/create.js.erb) .
And that's actually what happens on the page: after clicking on the Add Project button, nothing happens, but if you update it, a new Project is added. What have I done wrong??

This is what the log says after clicking the button:
ActionView::Template::Error (Missing partial projects/_projects, application/_projects with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
  * "/home/xo8bit/ruby/todo-app/app/views"
):
    1: $('#projects').html("<%= escape_javascript (render 'projects') %>");
  app/views/projects/create.js.erb:1:in `_app_views_projects_create_js_erb__2233098556876763135_37757080'
  app/controllers/projects_controller.rb:29:in `create'


  Rendered /home/xo8bit/.rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
  Rendered /home/xo8bit/.rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.4ms)
  Rendered /home/xo8bit/.rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (17.7ms)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shetani, 2014-08-03
@xo8bit

$('#projects').html("<%= escape_javascript (render 'projects') %>");

fix the brackets, it might work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question