0

我正在尝试找到一种方法,在该选择框中填充类别,当用户选择类别并提交时,他们将被重定向到所选类别页面,该页面显示所有标记有该类别的列表。

基本上它是一种habtm关系,其中列表有_and_belongs_to_many类别,反之亦然,并且通过类别控制器,所有类别都列在索引视图的选择框中,并且任何特定类别的选择应该重定向以显示该类别中的所有列表。

这就是我在类别索引页面中的内容

<ul>
  <% @categories.each do |category| %>
    <li><%= link_to category.name, category_path(category) %></li>
  <% end %>
</ul>
<%= form_for :categories, :url => {:action => :show}, :method => "get" do |f| %>
  <div class="field">
    <%= f.label :category %><br />
    <%= collection_select(:name, :id, Category.all, :id, :name) %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

提交时,只有 url 会自行更新。像这样 - localhost:3000/categories?utf8=%E2%9C%93&name%5Bid%5D=1&commit=Submit

尝试使用 ul 列表,它有效。但不能用选择框列表解决

类别控制器

def index
  @categories = Category.all
end

def show
  @category = Category.find(params[:id])
end

怎么了

更新

嘿,我做了这个 sql 来排序 -

def show     

@Category = Category.find_by_sql ["select place from listings l , categories c, categories_listings cl where c.id = cl.category_id and l.id = cl.listing_id and name in (?,?)" ,  params[:name][:id] , params[:name][:id]]

end

在索引页面中,我更新了表格 -

<div class="field">
    <%= f.label :category %><br />
    <%= collection_select(:name, :id, Category.all, :id, :name) %>
  </div>
<div class="field">
    <%= f.label :category %><br />
    <%= collection_select(:name, :id, Category.all, :id, :name) %>
  </div> 

显示页面

<ul>
  <% @Category.each do |c| %>
    <li><%= c %></li>
  <% end %>
</ul>

- 选择框没有映射到正确的参数,并且只映射到第二个选择,这不会产生任何结果。我该如何纠正这个或者我做得对吗

4

1 回答 1

0

您可能在路线方面遇到了一些问题。如果您resources :categories在 routes.rb 中进行操作,则 categories#show 的路由是 /categories/:id,因此您的表单实际上将指向 categories#index。

也就是说,在您当前的设置中,id 应该是 URL 的一部分,而不是表单参数。

尝试添加get 'categories/show' => 'categories#show'到您的路线。它不如默认路由好,但它可能会解决您的问题。

于 2013-10-28T22:39:45.450 回答