0
我是ROR編程新手,我的問題可能看起來很新鮮!如何創建一個AJAX按鈕?
我有一個Rails項目。我需要保存訂閱者數據而不刷新頁面!
在代碼塊「app/views/users/new.html.haml」的最後一行中,我需要將form.button更改爲ajax按鈕。我在項目中包含了jQuery。我應該如何編碼?當你回答時請考慮這個即時通訊初學者!
謝謝先進! (-_ ^)
這是路線:
users_new GET /users/new(.:format) users#new
users GET /users/index(.:format) users#index
create POST /users/create(.:format) users#create
這是我的應用程序/視圖/用戶/ new.html.haml:
.form1
%h1
Subscriber
=form_for @user, :url => :create do |form|
- unless @user.errors.empty?
- @user.errors.full_messages.each do |message|
.alert.alert-danger
%ul
- @user.errors.full_messages.each do |message|
%li= message
%div
.row.form-group
.col-lg-3
%label
Email:
=form.text_field :email, :class => 'form-control'
.row.form-group
.col-lg-3
%label
Name:
=form.text_field :name, :class => 'form-control'
.row.form-group
.col-lg-3
%label
Phone Number:
=form.text_field :phone_number, :class => 'form-control'
.row.form-group
.col-lg-3
%label
Address:
=form.text_field :address, :class => 'form-control'
%div=form.button 'Save', :class => 'btn btn-primary'
這是應用程序/控制器/users_controller.rb:
class UsersController < ApplicationController
def index
end
def new
@user = User.new
end
def create
@user = User.new params[:user]
@user.save
redirect_to users_new_path
end
end
當我這樣做時,它會在我的url的末尾爲post方法添加'/ true'!這是爲什麼? – Kasra