1

check_box_tag在尝试在表中搜索存档项目时遇到问题。

<%= hidden_field :archive, :value => false %>
<% if current_user.try(:admin?) %>
  Archive: <%= check_box_tag :archive, true, false, :class => "archive" %></H2>
<% else %>
  <%= hidden_field :archive, :value => false %>
<% end %>

因此,如果用户不是管理员,则存档总是错误的。

如果用户是管理员,则存档始终为 false,除非他们选中该复选框。

选中该复选框并提交搜索时,将显示结果,但该框再次变为未选中状态。有没有办法检查它检查?我尝试使用:selection => params[:archive],但这似乎不起作用。提前致谢。

添加搜索功能

def self.like(text); "%#{text}%"; end

  def self.search(search_archive, search_client)
    _projects = Project.scoped 


    if search_archive.present?
 _projects = _projects.where(:archive => search_archive)
    end
    if search_client.present?
      _projects = _projects.where ['client LIKE ?', like(search_client)] 
    end


    _projects

  end


end

搜索动作:

def search

    @search = params[:archive], params[:client]

    @project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)

    @search_performed = !@search.reject! { |c| c.blank? }.empty? 

  @project = Project.new(params[:project])



respond_to do |format|
      format.html # search.html.erb
      format.json { render :json => @project }
    end

end 

网址:

当我搜索 Client: Test 时,已存档检查:

http://localhost:3000/search?utf8=%E2%9C%93&%5Bclient%5D=Test&%5D=&archive=true&per_page=10

没有找到正确的项目,现在我取消点击存档:

http://localhost:3000/search?utf8=%E2%9C%93&%5Bclient%5D=Test&%5D=&archive%5B%7B%3Avalue%3D%3Efalse%7D%5D=&per_page=10

这再次显示了正确的结果,但该框现在已选中,但应该取消选中,以防我想优化搜索

修复: 在我的搜索功能中:

if search_archive.present?
  _projects = _projects.where(:archive => search_archive == "true")
end
if !search_archive.present? 
  _projects = _projects.where(:archive => search_archive == "false")
end

在我的搜索视图中:

<% if current_user.try(:admin?) %>
  Archive: <%= check_box_tag :archive, true, !!params[:archive], :class => "archive" %></H2>

<% end %>
4

1 回答 1

2

改变

<%= check_box_tag :archive, true, false, :class => "archive" %>

<%= check_box_tag :archive, true, !!params[:archive], :class => "archive" %>

check_box_tag更多信息请参考

于 2012-10-17T15:05:43.890 回答