gpt4 book ai didi

ruby-on-rails - Rails 设计多态 - 使用 Ajax 获取部分渲染

转载 作者:行者123 更新时间:2023-12-04 05:51:50 25 4
gpt4 key购买 nike

我已经实现了来自 this answer 的多个模型的设计注册.这 tuts 得到参数 user_type从路径。
我想用 select user_type 改变这个.所以当我在 select_tag 上选择一个值时会得到一个参数 user_type .

我有一些代码看起来像:

路线.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>

选择user.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 %>");

当我选择管理员值时,记录:
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

最佳答案

你在这里混淆了几件事。

  • 您的路由定义了一个默认值

  • 如果您需要从下拉列表中进行选择,则无需在此处定义默认值。它只是让事情复杂化
     match '/add_user' => 'registrations#new', :user => { :usertype => nil }
    match '/select/admin' => 'registrations#selectuser', :user => { :usertype => 'admin' }
    match '/select/player' => 'registrations#selectuser', :user => { :usertype => 'player' }
  • 您没有使用正确的表单助手

  • 您需要使用 f.select如果你想得到 params[:user][:usertype] 的输出.如果您的模型没有这样的属性,您可以将其添加为 attr_accessor到你的模型。
    <%= label :usertype, "Select User Type" %><br />
    <%= select_tag(:usertype, options_for_select([['-- User Type --', nil], ['Admin', 'admin'], ['Player', 'player']], selected: '-- User Type --' )) %>

    关于ruby-on-rails - Rails 设计多态 - 使用 Ajax 获取部分渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19446624/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com