也許我只是盯着這個太長時間,但我有一個基本的註冊和登錄表單,我建立,並希望只呈現在一個div的同一頁面,而不是重定向到一個新的頁。如何使用AJAX在頁面上呈現我的基本註冊/登錄表單而不是重新定向?
這裏是我的控制器
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
redirect_to root_url, notice: "Thanks for signing up!"
else
render "new"
end
end
end
有一個類似的會議
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "Logged in!"
else
flash.now.alert = "Email or password is invalid."
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "Logged out!"
end
end
,這裏是單頁的應用程序
<div id="user">
<% if current_user %>
Logged in as <%= current_user.email %>.
<%= link_to "Log Out", logout_path, remote: true, disable_with: "Goodbye!", :class => "logout_a" %>
<% else %>
<%= link_to "Sign Up", signup_path, remote: true, disable_with: "Welcome!", :class => "signup_a" %> or
<%= link_to "Log In", login_path, remote: true, disable_with: "Welcome Back!", :class => "login_a" %>
<% end %>
</div>
<div class="render_here">
</div>
與視圖的相關部分類「render_here」是我想呈現表單的地方。
我已經嘗試渲染link_to規範中的部分內容,在我的視圖中使用.js.erb文件,以及通過使用Google搜索問題獲得的其他一些想法,但我認爲我過於複雜,並且錯過了簡化了註冊和登錄過程的AJAX化解決方案。
謝謝!