Rails Ajax表单:无法确定我做错了什么

时间:2013-09-22 15:37:39

标签: ruby-on-rails-3 jquery

我的表单没有向控制器发送必要的params params [:tag_ids]。我无法弄清楚我做错了什么?

的routes.rb

put 'posts_filter', to: 'articles#index', as: :posts_filter

查看/物品/ index.html.haml

%ul{style: "list-style-type: none; margin: 0px; padding: 0px;"}
  = form_tag posts_filter_path, remote: true, method: :put do
    - @tags.each do |tag|
      - if tag.count_of_articles != 0
        %li.content
          = check_box_tag "tag_ids[]", tag.id, @selected_tag_ids.first.blank? ? true : @selected_tag_ids.include?(tag.id.to_s) ? true : false
          = "#{tag.name} (#{tag.count_of_articles})"
    = submit_tag "Filter", class: "btn"

控制器/ articles_controller

def index
@tags = Tag.asc
logger.info params.inspect
if params[:tag_ids].present?
  logger.info params[:tag_ids].inspect
  @articles = Article.where(id: Tagging.pluck_taggable_ids(params[:tag_ids])).desc.page(params[:page]).per(24)
  @selected_tag_ids = params[:tag_ids]
else
  logger.info "nononoononononononono"
  @articles = Article.pub.desc.page(params[:page]).per(24)
  @selected_tag_ids = []
end
respond_to do |format|
    format.html
    format.js
end

1 个答案:

答案 0 :(得分:0)

设置sublime编辑器标签的标签设置:2并粘贴以下代码:

%ul{style: "list-style-type: none; margin: 0px; padding: 0px;"}
= form_tag posts_filter_path, remote: true, method: :put do
- @tags.each do |tag|
  - if tag.count_of_articles != 0
    %li.content
      = check_box_tag "tag_ids[]", tag.id, @selected_tag_ids.first.blank? ? true : @selected_tag_ids.include?(tag.id.to_s) ? true : false
      = "#{tag.name} (#{tag.count_of_articles})"
= submit_tag "Filter", class: "btn"

问题在于ajax请求....