本文介绍了Rails的设计多态 - 获取渲染部分使用Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现色器件的注册与多模式这个答案。这TUTS GET参数 user_type 的路径。我想用select user_type 改变这一点。所以,当我选择一个值参数user_type将获得 select_tag

I have implement devise registration with multiple model from this answer. This tuts get params user_type from path.I want to change this with select user_type. So a param user_type will get when I select a value on select_tag.

我有一些code是这样的:

I have some code looks like :

routes.rb中

   namespace :cp do
    devise_scope :user do
     match '/add_user' => 'registrations#new'
     match '/select/admin' => 'registrations#selectuser', :user => { :usertype => 'admin' }
     match '/select/player' => 'registrations#selectuser', :user => { :usertype => 'player' }
    end
   end

registrations_controller.rb

  def selectuser

    respond_to do |format|
      format.js
    end
  end

new.html.erb

<h2>Add User</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :username, "Username" %><br />
  <%= f.text_field :username %></div>

  <div><%= f.label :email, "Email" %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :password, "Password" %><br />

  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation, "Password Confirmation" %><br />
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.label :usertype, "Select User Type" %><br />
  <%= f.select(:usertype, options_for_select([['-- User Type --', nil], ['Admin', 'admin'], ['Player', 'player']], selected: '-- User Type --' )) %>
   </div>

   <div id="selectuser">
   </div>
  <% end %>

  <div><%= f.submit "Submit" %></div>
<% end %>

<script type="text/javascript">
$("#user_usertype").on('change', function() {
   var s = $(this).val();
       $.ajax({
              type: 'GET',
              url: 'http://localhost:3000/cp/select/' + s,
              dataType: "HTML"
              });
});
</script>

selectuser.js.erb

<%  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 => child_class_name.underscore + '_fields', :locals => {:f => rf}
   end
%>
$("#selectuser").append("<%= j nesteds %>");

当我选择管理价值,日志:

When I select admin value, log :

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"=>{"usertype"=>"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)

_admin_fields.html.erb 未出现在 #selectuser

推荐答案

您必须添加=当你试图渲染部分:

You have to add an = when you're trying to render your partial:

<%=  params[:user][:usertype] ||= 'admin'
# ^ This makes whatever is being returned in this block to be added to the output

这篇关于Rails的设计多态 - 获取渲染部分使用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:23