Ruby on Rails - 在创建新记录后在show view中显示bootstrap模式

时间:2016-02-09 17:41:19

标签: ruby-on-rails ruby twitter-bootstrap

在我的展示视图中,我有一个引导模式,我想在创建新记录时“激活”

我的控制器:

def create
    @request = current_user.requests.build(request_params)

    respond_to do |format|
      if @request.save
        format.html { redirect_to @request, notice: 'Request was successfully created.' }
        format.json { render :show, status: :created, location: @request }
      else
        format.html { render :new }
        format.json { render json: @request.errors, status: :unprocessable_entity }
      end
    end
  end

我目前的节目观点:

#msgModal.modal.fade{"aria-labelledby" => "myModalLabel", :role => "dialog", :tabindex => "-1"}
  .modal-dialog{:role => "document"}
    .modal-content
      .modal-header
        %button.close{"aria-label" => "Close", "data-dismiss" => "modal", :type => "button"}
          %span{"aria-hidden" => "true"} ×
        %h4#myModalLabel.modal-title.text-muted One last step before we are good to go :)
      .modal-body
        %h4 Share your post with your friends to start on good foot ;)
        %img{:alt => "Sixpercent", :src => "http://localhost:8888/sixpercent.png", :style => "width: 420px;display: block;margin-left: auto;margin-right: auto;\n"}
        %br

%button.btn.btn-success.font-bold.txt-shadow-dark{"data-target" => "#msgModal", "data-toggle" => "modal", :type => "button"}
  #{icon('commenting-o')} Sharing div activater

如何以干净的方式管理它?

2 个答案:

答案 0 :(得分:1)

您需要Rails''flash'功能。闪存是在用户的会话cookie上设置的值。它持续一页加载。有关本页5.2的更多详细信息:

http://guides.rubyonrails.org/action_controller_overview.html

例如,您可以修改视图以根据闪光灯的存在执行不同的操作。 e.g:

- if flash[:notice] == 'Request was successfully created.' 
  #msgModal.modal.fade{"aria-labelledby" => "myModalLabel", :role => "dialog", :tabindex => "-1"}
    .modal-dialog{:role => "document"}
      .modal-content
        .modal-header
          %button.close{"aria-label" => "Close", "data-dismiss" => "modal", :type => "button"}
            %span{"aria-hidden" => "true"} ×
          %h4#myModalLabel.modal-title.text-muted One last step before we are good to go :)
        .modal-body
          %h4 Share your post with your friends to start on good foot ;)
          %img{:alt => "Sixpercent", :src => "http://localhost:8888/sixpercent.png", :style => "width: 420px;display: block;margin-left: auto;margin-right: auto;\n"}
          %br

希望有所帮助!如果我错过了你想要做的事情,我很抱歉: - )

答案 1 :(得分:0)

这对我有用

如果你在视图中有部分模态,那么

<%= render 'your_partial' %>

然后:

`<% if flash[:notice].present? %>
    <script>
      $(document).ready(function(){
        $('#myModal').modal("show");
      });
    </script>
<% end %>`