下面是如何成功地做到了。
我已經把註冊表格在我home#index
我的文件:
視圖/家庭/ index.html.erb
<%= render :file => 'registrations/new' %>
幫手/ home_helper.rb
module HomeHelper
def resource_name
:user
end
def resource
@resource = session[:subscription] || User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
def devise_error_messages!
return "" if resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => resource.errors.count,
:resource => resource_name)
html = <<-HTML
<div id="error_explanation">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
HTML
html.html_safe
end
end
您需要這個部分,因爲Devise與resource
一起工作,它應該被定義,所以你可以撥打你的registration#new
任何地方。
像這樣,你應該可以註冊。但是,我需要在同一頁面上顯示錯誤。下面是我添加了什麼:
佈局/ home.html.erb(通過索引視圖中使用的佈局)
<% flash.each do |name, msg| %>
# New code (allow for flash elements to be arrays)
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :div, message, :id => "flash_#{name}" %>
<% end %>
<% else %>
# old code
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %> #don't forget the extra end
<% end %>
我發現這個代碼here
而且這裏的東西我創建:我救了我的資源對象如果在會話中無效,以便用戶不必再填充每個字段。我想一個更好的解決方案存在,但它的工作原理,它足以讓我)
控制器/ registration_controller.rb
def create
build_resource
if resource.save
if resource.active_for_authentication?
# We delete the session created by an incomplete subscription if it exists.
if !session[:subscription].nil?
session[:subscription] = nil
end
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
# Solution for displaying Devise errors on the homepage found on:
# https://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
# We store the invalid object in session so the user hasn't to fill every fields again.
# The session is deleted if the subscription becomes valid.
session[:subscription] = resource
redirect_to root_path #Set the path you want here
end
end
我想我沒有忘記任何代碼。隨意使用任何你需要的。
此外,您還可以在表單中添加你的星座在同一個頁面(類似的東西:)
<%= form_for("user", :url => user_session_path) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.submit 'Sign in' %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
<%= link_to "Forgot your password?", new_password_path('user') %>
<% end %>
乾杯!
非常感謝你!我會盡快嘗試!你能告訴你爲什麼使用這段代碼:html = << - 腳本中的HTML?再次感謝,最誠摯的問候,Thijs – Thijs
這實際上是由Devise Helper本身提供的代碼。你可以找到代碼[這裏](https://github.com/plataformatec/devise/blob/30f9da9d71640025f78f46f359e371fcd02add66/app/helpers/devise_helper.rb) – Lucas
我試過了,它的工作原理...只有我失去了我的CSS ?我使用rails 3.1rc4,如果我將devise.css.scss複製/粘貼到welcome_page.css.scss,它不起作用?而當我輸入無效的數據時,它會去/用戶/註冊...但也許這與我的路由,我必須重定向到歡迎頁面,我想?最好的問候, – Thijs