【问题标题】:rails create action with ajax without formrails 使用 ajax 无形式创建动作
【发布时间】:2013-09-22 07:49:27
【问题描述】:

我有一个用于创建帖子的帖子表单,它工作正常,因为它并不难。

需要的是在插入标题之前,我需要一个posts id 以便我可以做更多的过程。

所以我决定创建一个light box(contains no form),其中列出了所有类别和子类别。在单击类别时,我需要执行 ajax 请求以通过仅在表中插入类别来创建记录。

这是我的 jquery 代码

    $('.selection').click(function(){
$('#category_id').val(category_id);
        $.ajax({
        type: "POST",
        url: '/posts.json',
        dataType: 'json'
        })
    })

;

这是我的创建操作

def create
    unless post_params.nil?
        @post = Post.new(post_params)
        if @post.save
            flash[:success] = 'Post added successfully'
            redirect_to action: :new
        else
            flash[:error] = 'Post cannot add. Please try again after some time'
            redirect_to action: :new
        end
    end

  end

我如何修改上面的 ajax 提交操作箱。谁能帮忙。

【问题讨论】:

  • 如何使用此创建操作代码创建类别,您能解释一下吗?
  • Ajax 中没有发送数据。

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

您只是忘记在 Ajax 调用中发送数据。 你应该试试:

    $('.selection').click(function(){
      $('#category_id').val(category_id);
      $.ajax({
        type: "POST",
        url: '/posts.json',
        dataType: 'json'
        data: {
         id: the_id,
         other_param: the_other_param
        }
      })
    })

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多