最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to show modal window in controller's action? - Stack Overflow

programmeradmin3浏览0评论

So I've got controller PagesController with actions index and full_search. Action index is for home page. On home page I've got the button 'search' and text_field. If user types sometiong in text_field and clicks on button 'search' he/she goes to action full_search. In this action I try to find something by user's query. if I don't find anything I should just show a modal window with 'no results' otherwise redirect to another page

def full_search
  ...do search...
  if search_result.empty?
    show modal window with text 'no results'
  else
    redirect to another page
  end
end

How can I show modal window(like function alert in js)? I think I have to use javascript..but where???

if search_result is empty I don't need to render any view...I just need to stay at the same page and show modal window

So I've got controller PagesController with actions index and full_search. Action index is for home page. On home page I've got the button 'search' and text_field. If user types sometiong in text_field and clicks on button 'search' he/she goes to action full_search. In this action I try to find something by user's query. if I don't find anything I should just show a modal window with 'no results' otherwise redirect to another page

def full_search
  ...do search...
  if search_result.empty?
    show modal window with text 'no results'
  else
    redirect to another page
  end
end

How can I show modal window(like function alert in js)? I think I have to use javascript..but where???

if search_result is empty I don't need to render any view...I just need to stay at the same page and show modal window

Share Improve this question edited Jun 14, 2016 at 19:24 Vitalii Paprotskyi asked Jun 14, 2016 at 18:30 Vitalii PaprotskyiVitalii Paprotskyi 3898 silver badges20 bronze badges 2
  • Do you have the modal on your page? Not the content, only the structure. Like <div class="modal... – Kumar Commented Jun 14, 2016 at 18:38
  • No I don't have it – Vitalii Paprotskyi Commented Jun 14, 2016 at 18:47
Add a ment  | 

2 Answers 2

Reset to default 6

Step 1.

In your rendered view, have a div with id "my_modal_content" where will insert the modal's content and show the modal.

Step 2.

Render a js.erb file which will append the modal to our page

#your rails controller method
def my_method
  if something == true
    respond_to do |format|

      format.js { render :partial => 'somewhere/somefile.js.erb' }
      #I'm assuming its js request
    end
  else
    redirect to another page
  end
end

Step 4.

Write your js.erb file like this, which appends modal and then shows it.

#_somefile.js.erb file

$("#my_modal_content").html("<%= escape_javascript(render 'somewhere/my_modal') %>");
$("#myModal").modal('show');

Step 3.

Modal partial file _my_modal.html.erb placed within views/somewhere/ with you modal content ready.

<div class="modal fade" tabindex="-1" id="myModal" role="dialog">
  <div class="modal-dialog">
    ...<!-- all your content insdie -->
  </div>
</div>

Update

If you want to show an alert, its pretty straight forward. You just render js

if search_result.empty?
  message = "No result found. Try something else!"
  respond_to do |format| 
    format.js { render js: "alert(#{message});" }
  end
else
  redirect to another page
end

You can save a variable with true or false in your controller: @var = true

And then in a view use javascript:

if (<%= @var%> == true) {
  alert("some text");
}
发布评论

评论列表(0)

  1. 暂无评论