【发布时间】:2018-03-24 01:50:15
【问题描述】:
我已经引用了这 2 个 stackoverflow 帖子,但我似乎无法在我的网址中使用“CategoryName”填充q[categories_name_cont]:例如,在我单击名为“CategoryName”的按钮后,/articles/browse?utf8=✓&q[categories_name_cont]=&commit=CategoryName。
1) Rails filter with button or link_to using Ransack 2)How to construct a clickable link with a scope using Ransack and Rails
browse.html.erb
<% @categories.each do |category| %>
<%= search_form_for @q, url: browse_articles_path, method: :get do |f| %>
<%= f.hidden_field :categories_name_cont %>
<%= f.submit category.name, :class => 'btn btn-primary', :onclick => "document.getElementById('q_categories_name_cont').value = '#{category.name}'" %>
<% end %>
<% end %>
articles_controller.rb
def browse
@q = Article.ransack(params[:q])
@articles = @q.result.includes(:categories, :users)
@categories = Category.with_articles.order('name ASC').all
end
更新:
1) 硬编码 document.getElementById('q_categories_name_cont').value = "CategoryName" 也没有生成这个 url。
/articles/browse?utf8=✓&q[categories_name_cont]=CategoryName 在我点击名为“CategoryName”的按钮后
2)我最终使用以下方法将参数传递给url:
<% @categories.each do |category| %>
<a class="btn btn-large btn-primary" href="/articles/browse?utf8=✓&q[categories_name_cont]=<%= category.name %>">
</a>
<% end %>
【问题讨论】:
标签: ruby-on-rails ruby search ruby-on-rails-5 ransack