user2493476
user2493476

Reputation:

Rails ajax error while creating

Hello guys I want use ajax in my rails application : Here is my routes.rb file

ExampleAjax::Application.routes.draw do
   root 'users#index'
    resources :users
end

my controller file

class UsersController < ApplicationController
 def index
    @users = User.all
    @user = User.new
  end
 def create
  @user = User.new(user_params)

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.js   {}
      format.json { render json: @user, status: :created, location: @user }
    else
      format.html { render action: "new" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

private 
def user_params
    params.require(:user).permit(:fname)
end
end

My view files index.html.erb

<b>Users</b>

<ul id="users">
<% @users.each do |user| %>
  <%= render user %>
<% end %>
</ul>

<br>

<%= form_for(@user, remote: true) do |f| %>
  <%= f.label :fname %><br>
  <%= f.text_field :fname %>
  <%= f.submit %>
<% end %>

new.html.erb

<%=form_for(@user, remote: true) do |f|%>
    <p>
        <%= f.label :fname %><br>
        <%= f.text_field :fname %>
    </p>
    <p>
        <%= f.label :lname %><br>
        <%= f.text_field :lname %>
    </p>
    <p>
        <%= f.submit %>
    </p>
<% end %>

_user.html.erb

<li><%= user.fname %></li>

_create.js.erb

$("<%= escape_javascript(render @user) %>").appendTo("#users");

And my problem is when I click create user I got the following error in console

POST http://localhost:3000/users 500 (Internal Server Error) 

Upvotes: 3

Views: 86

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Ajax

I want use ajax in my rails application

To give you some info about Ajax, it's basically integrated in your application already:

Ajax is a group of interrelated web development techniques used on the client-side to create asynchronous web applications

It's basically a way to send an asynchronous request to your web app (through javascript). Asynchronous essentially means you're sending requests out of scope of the typical HTTP "synchronous" request structure

HTTP works by responding to requests. Each click you perform is sending a request to your server, which responds with data. Synchronous requests are ones you perform in order, whereas asynchronous "ajax" ones are sent independently


Code

Your problem is you don't have a create.js.erb file:

respond_to do |format|
    format.html #-> action.html.erb
    format.js #-> action.js.erb
end

Unless you call some other methods in this block (such as render nothing), Rails will look for your action.js.erb file in your views directory


Rails

Rails handles all requests equally, and determines if it's an ajax request using the ActionDispatch::Http::MimeNegotiation class

This means you don't have to do anything "special" to use Ajax -- just send the requests to your routes, and handle them in the backend using respond_to do |format|

Upvotes: 0

sevenseacat
sevenseacat

Reputation: 25029

Your code is hitting this block:

format.js   {}

Which has nothing defined in it, so your app will try to do what it normally does, ie. render the view for the action you are in.

You are in a create action, and your format is js, so it will try to render create.js.erb.

You should rename your _create.js.erb to remove the underscore.

Upvotes: 1

Related Questions