1
如何顯示Rails沒有括號和引號的錯誤消息?Rails:無括號的錯誤消息
當出現錯誤信息,它看起來像:
["please fill out domain"]["please fill out email"]
我怎樣去掉括號和引號?
型號
validates :domain,
:presence => { :message => 'please fill out domain'},
:uniqueness => { :message => 'domain already registered', :case_sensitive => true }
validates :email,
:presence => { :message => 'please fill out email'},
:uniqueness => { :message => 'email already registered', :case_sensitive => true }
控制器
我指定模板的URL(見format.html
)因爲我使用不同的控制器
def create
respond_to do |format|
# save form data
@login = Login.new(params[:login])
# if validation fails, throw error messages
if [email protected]
flash[:error] = @login.errors.to_a if @login.invalid?
end
# render landingpage
format.html { render :template => 'landingpage/index' }
format.js { render :action => 'create' }
end
end
create.js.erb
<% if @login.errors.any? %>
// error
var error_string = "";
<% @login.errors.values.each do |msg| %>
error_string += "<%= msg %>";
<% end %>
$("#flash_messages").html(error_string);
<% else %>
// no error
<% end %>
查看
<div id="flash_messages"></div>
如果您在'@ login.errors.to_a' o que acontece中刪除'to_a'調用? – MurifoX
嘗試'@login.errors.messages.each做| msg |' – HungryCoder
@MurifoX:不幸的是,它不會改變任何東西 – Slevin