Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Devise Polymorphic - Get Render Partial Using Ajax
    text
    copied!<p>I have implement devise registration with multiple model from <a href="https://stackoverflow.com/a/7312205/1297435">this answer</a>. This tuts get params <code>user_type</code> from path. I want to change this with select <code>user_type</code>. So a param user_type will get when I select a value on <code>select_tag</code>.</p> <p>I have some code looks like :</p> <p><strong>routes.rb</strong></p> <pre><code> namespace :cp do devise_scope :user do match '/add_user' =&gt; 'registrations#new' match '/select/admin' =&gt; 'registrations#selectuser', :user =&gt; { :usertype =&gt; 'admin' } match '/select/player' =&gt; 'registrations#selectuser', :user =&gt; { :usertype =&gt; 'player' } end end </code></pre> <p><strong>registrations_controller.rb</strong></p> <pre><code> def selectuser respond_to do |format| format.js end end </code></pre> <p><strong>new.html.erb</strong></p> <pre><code>&lt;h2&gt;Add User&lt;/h2&gt; &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name)) do |f| %&gt; &lt;%= devise_error_messages! %&gt; &lt;div&gt;&lt;%= f.label :username, "Username" %&gt;&lt;br /&gt; &lt;%= f.text_field :username %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.label :email, "Email" %&gt;&lt;br /&gt; &lt;%= f.email_field :email %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.label :password, "Password" %&gt;&lt;br /&gt; &lt;%= f.password_field :password %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.label :password_confirmation, "Password Confirmation" %&gt;&lt;br /&gt; &lt;%= f.password_field :password_confirmation %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.label :usertype, "Select User Type" %&gt;&lt;br /&gt; &lt;%= f.select(:usertype, options_for_select([['-- User Type --', nil], ['Admin', 'admin'], ['Player', 'player']], selected: '-- User Type --' )) %&gt; &lt;/div&gt; &lt;div id="selectuser"&gt; &lt;/div&gt; &lt;% end %&gt; &lt;div&gt;&lt;%= f.submit "Submit" %&gt;&lt;/div&gt; &lt;% end %&gt; &lt;script type="text/javascript"&gt; $("#user_usertype").on('change', function() { var s = $(this).val(); $.ajax({ type: 'GET', url: 'http://localhost:3000/cp/select/' + s, dataType: "HTML" }); }); &lt;/script&gt; </code></pre> <p><strong>selectuser.js.erb</strong></p> <pre><code>&lt;% params[:user][:usertype] ||= 'admin' if ["admin", "player"].include? params[:user][:usertype].downcase child_class_name = params[:user][:usertype].downcase.camelize usertype = params[:user][:usertype].downcase else child_class_name = "Admin" usertype = "admin" end nesteds = fields_for child_class_name.constantize.new do |rf| render :partial =&gt; child_class_name.underscore + '_fields', :locals =&gt; {:f =&gt; rf} end %&gt; $("#selectuser").append("&lt;%= j nesteds %&gt;"); </code></pre> <p>When I select admin value, log :</p> <pre><code>Started GET "/cp/select/admin" for 127.0.0.1 at 2013-10-21 17:00:04 +0700 Processing by Cp::RegistrationsController#selectuser as HTML Parameters: {"user"=&gt;{"usertype"=&gt;"admin"}} Rendered cp/registrations/_admin_fields.html.erb (4.0ms) Rendered cp/registrations/selectuser.js.erb (7.0ms) Completed 200 OK in 22ms (Views: 22.0ms | ActiveRecord: 0.0ms) </code></pre> <p>But <code>_admin_fields.html.erb</code> not appear on <code>#selectuser</code></p>
 

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