Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of things which could cause this problem</p> <p>Considering you're a rails newbie (<em>welcome!</em>), I thought it best to outline the issues in an answer, rather than in the comments</p> <hr> <p><strong>Routes.rb</strong></p> <p>There's a function in the rails routes called <a href="http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions" rel="nofollow"><code>resources</code></a> - this allows you to create a number of rails routes through a single function call - <em><code>resources :controller</code></em></p> <p>You seem to be manually declaring your routes, which is generally considered bad practice for generic functions like <code>new</code> or <code>create</code> I'd advise you to first change your routes.rb file to this:</p> <pre><code>#app/config/routes.rb resources :users </code></pre> <p>This will give you a set of pre-generated routes, which will you keep your code efficient</p> <hr> <p><strong>Users_Controller.rb</strong></p> <pre><code> #/app/controllers/users_controller.rb def create @user = User.new params[:user] @user.save render :json =&gt; @user end </code></pre> <p>You have a big problem here</p> <p>You're using <a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29" rel="nofollow">AJAX</a> (which is Javascript), and are not handling the request properly. You really need to use <a href="http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to" rel="nofollow"><code>respond_to</code></a> to handle the header-type of the request, and to ensure you're able to engage with the app properly</p> <p>Try this:</p> <pre><code> #/app/controllers/users_controller.rb def create @user = User.new params[:user] @user.save respond_to do |format| format.js format.json { render :json =&gt; @user } format.html end end </code></pre> <p>Something you need to ensure is you know the differences between JSON &amp; a standard "Javascript" call through Ajax. <a href="http://en.wikipedia.org/wiki/JSON" rel="nofollow"><code>JSON</code></a> is basically a way to deal with small arrays (called JSON Objects), which can be passed through ajax; whilst a full javascript call has much more scope</p> <hr> <p><strong>Create.js.erb</strong></p> <p><code>.js.erb</code> is basically the same as <code>.html.erb</code></p> <p>In your <code>create.js.erb</code>, you rely on a json object to return your new user's ID. Although this might be considered O.K, I think you can make it more efficient by cutting out the json object completely</p> <p>In <code>create.js.erb</code>, you don't need any functions to fire things. It just works like an HTML View, and so you should just put the code in you want fired:</p> <pre><code>#/app/views/users/create.js.erb alert(&lt;%=j @user.id %&gt;); </code></pre> <p>Although the methodology here is correct, I don't know if it will output anything because we're not explicitly passing the <code>@user</code> variable</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload