开发者

Input form where user could enter regular expression

I have a rails application in which I need to create some input form where user could enter regular expression. This regex needed to be passed to my method check_site(url, rege开发者_运维百科x) that will return true or false depends on regex found on the page. I've tried to create a 'link_to':

link_to 'Search', check(item.name, @pattern)

In this case method "check" is called not with button pressed but with page loading. Do I need to use JS+AJAX? How to?


Crash course on getting AJAX/JS set up with your Rails form:

  1. Ensure correct javascript is called in your page <head>: JQuery + application.js (Jquery-compatible version, of course!)

  2. Change your link to:

    link_to 'Search', check_item_path(:name=>item.name, :id=>@pattern, :var=>var, etc.), :remote => true
    
  3. In items_controller:

    def check  
      ...your regex...  
      respond_to do |format|  
        format.html { redirect_to ...(wherever you'd like if no .js) }  
        format.js  
      end  
    end
    
  4. Create a file called check.js.erb in the corresponding views folder and enter the javascript you'd like to update the page / confirm the form's successful submission / return results from your search, etc.

    $(document).ready( function() {
      $("#result").html(" <%= @item.result == true ? "true" : "false" %> ");
    });
    

No method error: check_item_path

This means Rails is not recognizing the route to that controller action. Run rake routes in console and see if anything corresponds to the items controller, check_item action. If not, add:

match 'items/check_item'

to your routes file. Then run rake routes again and you should see check_item appear somewhere - if it does, you can use check_item_path again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜