开发者

RAils named routes inside Jquery autosuggest

I am working in rails2.3.11. I have a javascript like

 <script type="text/javascript">
        $(item).autoSuggest("/users/sampledata",{selectedItemProp: "name", searchObjProps: "name,login", asHtmlID: elmid,beforeRetrieve: function(string){ $('#spinner').show(); return string; },retrieveComplete: function(data){ $('#spinner').hide(); return data; } });
 </script>

I am trying to convert the path "/users/sampledata" to rails format like sampledata_users_path.. how to do this ??

The controller is Users controller and action is sampledata this action has

   def sampledata
@users = User.se开发者_如何学Goarch(params["q"].gsub(/[^ \w]/, "").strip + "*", :limit => 8)
js = []
@users.each do |user|
      js << {:value => user.id.to_s, :name => user.name, :image => user.avatar.url(:micro), :login => user.login}
end
respond_to do |format|
  format.json { render :json => js.to_json }
end
     end


Is the above "javascript" in a javascript file or is it a part of your erb template?

If its a part of your erb template then its easy.

<script type="text/javascript">
  $(item).autoSuggest("<%= sampledata_users_path %>",{selectedItemProp: "name", searchObjProps: "name,login", asHtmlID: elmid,beforeRetrieve: function(string){ $('#spinner').show(); return string; },retrieveComplete: function(data){ $('#spinner').hide(); return data; } });
 </script>

If the above is not in a dynamically generated file, you may want to look at a plugin which reads the routes file and generates javascripts constants for use in js. I think it was called "less_routes" or something.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜