简体   繁体   English

Rails:如何在jQuery(.js.erb)中调用“创建”操作?

[英]Rails: How do I call 'create' action in jQuery ( .js.erb )?

I am using Rails 4.1.1 I was wondering, how do I call the 'create' action in jQuery ( .js.erb )? 我正在使用Rails 4.1.1,我想知道如何在jQuery(.js.erb)中调用'create'操作? Please see below my jquery codes and my user_controller.rb , I would like to call the user_controller.rb create action in the jquery if else code. 请在下面查看我的jquery代码和我的user_controller.rb如果其他代码,我想在jquery中调用user_controller.rb create动作。

routes.rb routes.rb

Rails.application.routes.draw do
  resources :user
  root 'user#index'
end

user_controller.rb user_controller.rb

class UserController < ApplicationController

  def index
    @users = User.all
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    @user.save
    redirect_to "/"
  end

  private

  def user_params
    params.require(:user).permit(:email)
  end

end

subscribe.js.erb subscription.js.erb

$(function () {
  $("#subscribe").submit(function (event) {
    var input = $('.my-subscribe-message');
      if(!input.is(':empty')) {
        $('.my-subscribe-message').stop(true);
      }
      event.preventDefault();
      event.stopImmediatePropagation();

      var email = $("input#subscribe-email").val();

      if (email == "") {

        $(".my-subscribe-message").stop(true).html('<i class="fa fa-warning"></i> You must enter a valid e-mail address.');
        $("input#subscribe-email").focus();
      } 
      else if (!isValidEmailAddress( email )) {
        $(".my-subscribe-message").stop(true).html('<i class="fa fa-warning"></i> E-mail address is not valid.');
        $("input#subscribe-email").focus();            
      }
      else {
        $.ajax({

/* I would like to call the create action here */

          success: function () {
            $(".my-subscribe-message").html('<i class="fa fa-check"></i> We will be in touch soon!');
            $('input#subscribe-email').val('');
          }
        });
      }
   });
});

Give this a punt: 给这个平底锅:

$.ajax({
  type: "POST",
  url: "/users",
  data: { user: { email: "blah@blah.com" } },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How do I write a jquery js.erb to have both html and ruby output for a link_to:remote =&gt; in rails 3? - How do I write a jquery js.erb to have both html and ruby output for a link_to :remote => in rails 3? Rails:如何使用控制器加载/触发js.erb? - Rails: How do load/trigger a js.erb using the controller? 如何从作为AJAX调用的回调执行的js.erb触发JS函数? - How do I trigger a JS function from a js.erb executed as a callback on an AJAX call? 用jquery替换图像-js.erb-Rails - Replacing images with jquery - js.erb - Rails 如何从Rails的js.erb中传回一个jquery字符串 - How can I pass back a jquery string from a js.erb in rails Rails 4 - 如何在js.erb文件中使用div_for中的动态div ID? - Rails 4 - How do I use dynamic div ids from div_for in a js.erb file? 如何告诉rails不要在我的应用程序中预编译/缓存.js.erb文件? - How do I tell rails not to precompile/cache a .js.erb file in my app? 如何在Rails 4中将javascript_include_tag与js.erb文件一起使用? - How do I use javascript_include_tag with js.erb file in Rails 4? Rails-如何确保已在js.erb中渲染了部分 - Rails - how do I make sure a partial has been rendered in a js.erb 如何使用嵌入式ruby动态创建jquery选择器(在.js.erb文件中)? - How can I use embedded ruby to dynamically create a jquery selector (in a .js.erb file)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM