在 Rails 中的条件下从视图调用引导模式



首先,我解释我想做什么:当用户在sign_in或sign_up弹出表单(Bootstrap modal)中输入错误的值时,我应该调用$("#sign_up").show();或$("#login").show();。

所以,我需要从视图中调用 js:我看到它是这样的:

  <%= devise_error_messages! //here call $("#sign_up").show(); %>

或其他方式,但几乎相同,但devise_error

  <% if @user.errors.any? %>
<%"eval('$("#sign_up").show();')"%> 

或者它应该如何...

我应该重写我的注册吗?

请纠正我,我无法做到这一点。

如果我理解这个问题,你不需要调用模态。只是不要在页面加载后隐藏它。也就是说,必须有这样的东西:

<% if @user.errors.any? %>
  <div id="myModal" class="modal">
<% else %>
  <div id="myModal" class="modal hide">
<% end %>
    #your modal here
  </div>

但总的来说,我建议在模态内渲染表单的动作中使用 Ajax 和 respond js create

#create action
respond_to do |format|
  if @user.save
    ...
  else
    ...
    format.js { render action: "new" }
  end

#new.js.erb
$('#myModal').html("<%= j render 'form' %>");

最新更新