Javascript 允许以后灵活地向用户类型添加额外字段。也就是说,如果您将用户类型提取到它自己的模型中,那么您可以轻松地从:name的user\u type属性中筛选用户
因此,您的用户模型将具有:Javascript 允许以后灵活地向用户类型添加额外字段。也就是说,如果您将用户类型提取到它自己的模型中,那么您可以轻松地从:name的user\u type属性中筛选用户,javascript,ruby-on-rails,forms,Javascript,Ruby On Rails,Forms,因此,您的用户模型将具有: class User < ActiveRecord::Base has_many :microposts belongs_to :user_type scope :by_user_type, -> user_type { where(:user_type => user_type) } end **请注意,附加字段只是示例,name是唯一必需的字段。但是如果你想让用户进行url搜索,slug很容易使用。i、 e.yoursite.co
class User < ActiveRecord::Base
has_many :microposts
belongs_to :user_type
scope :by_user_type, -> user_type { where(:user_type => user_type) }
end
**请注意,附加字段只是示例,name是唯一必需的字段。但是如果你想让用户进行url搜索,slug很容易使用。i、 e.yoursite.com/user\u type/sellers
现在创建一个迁移,删除用户中现有的userType字段,并为关系创建一个新的userType字段
rails g migration modify_user_type_in_user
该文件的内容将是
class ModifyUserTypeInUser < ActiveRecord::Migration
def change
remove_column :users, :userType
add_reference :users, :user_type, index: true
end
end
你还需要使用UJS,你在文章中没有提到。当单击表单字段时,它将发送一个javascript(ajax)请求。这意味着为了更改数据,您需要一个javascript响应模板
因此,添加文件app/views/users/index.js.erb
,并将以下内容放入其中:
$("#users").html('<%= j(render("user_list", users: @users)) %>');
$("table tbody tr:nth-of-type(even)").css("background","#BD9FB1");
$(“#用户”).html(“”);
$(“表tbody tr:n类型(偶数)”).css(“背景”,“#BD9FB1”);
最后,您需要更改表单,以便它代表正确的可搜索模型。所以编辑“app/views/users/index.html.erb”
<p id="notice"><%= notice %></p>
<h1>User Filter</h1>
<%= form_tag users_path, method: 'get', id: "users_search" do%>
<% @user_types = UserType.all %>
<% @user_types.each do |user_type|%>
<%= check_box_tag "by_user_type[]", user_type.id %><%= user_type.name %><br>
<% end %>
<%= submit_tag "submit" %>
<% end %>
<div id="users"><%= render 'user_list' %></div>
<script type="text/javascript">
$(function () {
$('input[type=checkbox]').change(function(){
$.get($('#users_search').attr('action'),
$('#users_search').serialize(), null, 'script');
return false;
});
$('users_search').submit(function(e) {
e.preventDefault();
$.get(this.action, $(this).serialize(), null, 'script');
return false;
});
});
</script>
用户过滤器
$(函数(){
$('input[type=checkbox]')。更改(函数(){
$.get($('#用户搜索').attr('action'),
$(“#用户搜索”).serialize(),null,'script');
返回false;
});
$('users_search')。提交(函数(e){
e、 预防默认值();
$.get(this.action,$(this.serialize(),null,'script');
返回false;
});
});
您能向我们展示更多您的观点吗?例如,用户列表部分?您能向我们展示更多您的视图吗?例如,用户列表部分?
rails g migration modify_user_type_in_user
class ModifyUserTypeInUser < ActiveRecord::Migration
def change
remove_column :users, :userType
add_reference :users, :user_type, index: true
end
end
class UserType < ActiveRecord::Base
has_many :users
end
$("#users").html('<%= j(render("user_list", users: @users)) %>');
$("table tbody tr:nth-of-type(even)").css("background","#BD9FB1");
<p id="notice"><%= notice %></p>
<h1>User Filter</h1>
<%= form_tag users_path, method: 'get', id: "users_search" do%>
<% @user_types = UserType.all %>
<% @user_types.each do |user_type|%>
<%= check_box_tag "by_user_type[]", user_type.id %><%= user_type.name %><br>
<% end %>
<%= submit_tag "submit" %>
<% end %>
<div id="users"><%= render 'user_list' %></div>
<script type="text/javascript">
$(function () {
$('input[type=checkbox]').change(function(){
$.get($('#users_search').attr('action'),
$('#users_search').serialize(), null, 'script');
return false;
});
$('users_search').submit(function(e) {
e.preventDefault();
$.get(this.action, $(this).serialize(), null, 'script');
return false;
});
});
</script>