Sinatra 下拉列表

Sinatra dropdown list

很高兴您回答了我关于开发问题的问题,这次我会尽量做到不言自明。

我有一个主要 app.rb,我在其中使用多个端点重定向到我的 Sinatra Haml views.My 项目是关于一个软件组合,所以我有这个 class:软件和类别,其关系是:一个软件有一个类别,一个类别有很多软件。在您创建新软件条目的表单中,我放了一个下拉列表,您可以在其中选择 3 个不同的类别:桌面、Web 和应用程序。 在那之前,一切都很顺利。问题是,当软件列表出现时,我想放一个下拉列表来按创建的类别进行过滤(我已经有了 "add category" 表单及其 Class),但我不知道如何在软件列表表单的过滤器按钮中添加过滤器。你们能帮帮我吗?我当然知道如何将按钮放在那里,但我只想显示所选类别匹配的软件条目。这是列表形式。

%select{:name => "category"}
 %option Desktop
 %option Web
 %option Device
 %input{:type => "submit", :value => "Filter", :class => "btn"}
%ul.list
 - @sware.each do |software|
  %div{:class =>"list-group"}
  %a{:href =>"/software/edit/#{software.id}", :class =>"btn btn-lg btn-primary"} 
   = software.title
   %a.pull-right(href="/software/delete/#{software.id}" class="btn btn-lg btn-danger")  Delete

提前致谢!

您正在这样调用 index 方法:

post '/all' do
    index(:category)
end

您将参数传递给 index 调用,但 index 方法不接受任何参数。

请在您的问题中包含完整的错误。

除了@max pleaner 提出的观点之外,您实际上并没有在 params 中调用正确的对象。它应该是 params[:category] 并且你应该能够更简单地重写:

get '/all' do
    halt(401,'Not Authorized, please login to continue') unless session[:admin]
    @sware = Software.all
    haml :sware
end

post '/:category' do
    @sware = Software.title.where(categorization: {Software.categorization => params[:category]}
    haml :index # assuming index.haml is where you want to go
end

然后,假设您的文件缩进正确,您的 Haml 文件应该也能正常工作:

%select{:name => 'category'}
    %option Desktop
    %option Web
    %option Device
%input{:type => 'submit', :value => 'Filter', :class => 'btn'}
%ul.list
    - @sware.each do |software|
        %div{:class =>'list-group'}
            %a{:href =>"/software/edit/#{software.id}", :class =>'btn btn-lg btn-primary'} 
                = software.title
            %a.pull-right{:href=>"/software/delete/#{software.id}" :class=>'btn btn-lg btn-danger'} Delete

当然,你能提供的信息越多,问题就越容易理解。

index 操作可以被 DRY'ed:

def index
  category = case
             when params[:Web] then :Web
             when params[:Desktop] then :Desktop
             when params[:Device] then :Device
             end

  @sware = Software.title.where(categorization: { Software.categorization => category })
end

"It just doesn't work" 不是开始调查问题的好地方。需要更多调试信息。