Rails remote_函数,我可以用javascript在客户端动态设置id吗?

Rails remote_函数,我可以用javascript在客户端动态设置id吗?,javascript,ruby-on-rails-3,Javascript,Ruby On Rails 3,我试试这个: remote_function(:url => {:controller => '/cities', :action => 'show'}, :method => 'get') 但是我得到了一个路由错误,因为我没有指定ID。但是我希望ID能够根据用户在运行时在页面上选择的值进行更改。于是我尝试了这个: remote_function(:url => {:controller => '/cities', :action => 'show'}

我试试这个:

remote_function(:url => {:controller => '/cities', :action => 'show'}, :method => 'get')
但是我得到了一个路由错误,因为我没有指定ID。但是我希望ID能够根据用户在运行时在页面上选择的值进行更改。于是我尝试了这个:

remote_function(:url => {:controller => '/cities', :action => 'show'}, :method => 'get', :with => "'id=' + $F('query_city_id')")
但这也给了我一个路由错误,我猜是因为它想让我在:url中定义:id,以便生成一个有效的url来“显示”一个城市。因此,我尝试为:id硬编码一个“1”,以使其生效,然后用以下内容覆盖:中的:id:

remote_function(:url => {:controller => '/cities', :action => 'show', :id => 1}, :method => 'get', :with => "'id=' + $F('query_city_id')")
但这也不起作用。看来我真正想要的是这样的东西:

remote_function(:url => {:controller => '/cities', :action => 'show', :id => "$F('query_city_id')"}, :method => 'get')

但我知道这是行不通的。有人能给我指出正确的方向吗?我可能试图使用错误的远程函数,或者我可能需要做一些更像Rails 3的事情,或者我可能真的需要在运行时在客户端动态生成底层ajax调用

所以对于您的路线,
:id
是存在的,对吗?这就是为什么在生成路线时必须提供id的原因

您可以覆盖路由,使其不包含
:id
,或将
:id
设置为可选(使用
(:id)
),然后将您的id作为参数附加以发送

但是,我以前没有使用过remote_函数:D

====更新=====

所以我对远程函数并不熟悉,因为我很少使用内置的原型,而是jquery

似乎您正在为choose提供一个城市id列表,然后将id传递给服务器以获取城市,并将其显示在页面上

以下只是一个示例,可能不适合您的情况:

定义路线:

map.ajax_show_city "/cities/:id", :controller => "cities", :action => "show"
在CitiesController中

def show
  @city = City.find(params[:id])
  respond_to do |format|
    format.html #if you don't like, you could delete this line
    format.js # auto call cities/show.js.erb
  end
end
在城市/show.js.erb

jQuery("#the_place_you_want").append("<%= javascript_escape(render(:partial=>@city)) %>");
就这些

因此,如果关闭了js,用户仍然可以按下submit按钮(使用js隐藏或分离按钮)并获得正确的结果!添加js只是添加了更多有趣的东西。 当然,我同意几乎每个人都不会关闭他们的js支持,但这是对js的可访问性

=====

对于资源的显示路径,似乎必须提供一个
id
,才能生成没有错误的路径,这是相当棘手的

您可以测试以下路径:

map.connect "/test", :controller => "cities", :action => "test" # for test only

map.resources :cities
map.other "/cities/show/:id", :controller => "cities", :action => "show", :method => :get

def test
  render :text => url_for(city_path)     # ambiguous routes error
  render :text => url_for(city_path(0)   # gives /cities/0
  render :text => url_for(other_path)    # gives /cities
  render :text => url_for(other_path(0)) # gives /cities/0
end

一种解决方法是生成
/cities/0
,并在运行时使用js将
0
替换为您选择的id。

那么您使用什么从页面进行远程调用呢?这似乎是任何Rails应用程序都会做的一件非常常见的事情。这一点都不奇怪,但我很难保持它的RESTful和making a remote call…我不想链接到远程,也不想提交远程表单。我还有什么其他选择?我需要移动到JQuery和Uobtrusive Javascript吗?我在UJS和JQuery中找不到关于这方面的好信息,你能告诉我一个好的链接吗?事实上我正在使用JQuery。我通常在没有任何AJAX调用的情况下首先构建网站它甚至在禁用javascript或屏幕阅读器时也能工作。之后,我使用javascript使表单提交使用AJAX(因此,如果禁用javascript,则不会受到影响)。通过AJAX使表单提交只需将通用函数附加到onsubmit事件即可。
$(“.turn_me_to_AJAX”).live(“提交”,使用_AJAX提交)
函数submit_with_ajax(){$.post($(this.attr(“action”),$(this.serialize(),null,“script”);}
.Simple^^^首先,我不关心禁用javascript的用户。如果他们没有JS,那么他们甚至不必费心在互联网上做任何事情。我也不关心屏幕阅读器。你描述的是用JQuery提交远程表单,这不是我想要的。当然,我知道如何提交远程表单te表单,它很简单。我需要从页面调用远程函数。如果你不这么做,那么你的站点一定非常无聊和简单!我认为提交ajax表单与调用远程函数类似(如果远程函数也是一种ajax调用)。那么您要做的就是通过ajax将
id
发送到服务器,然后更新当前页面,对吗?通过使用
respond\u to
,您可以给当前页面更新javascript!请查看我的更新(正在编写,请稍候)
function show_city(select, link) { # sorry for using jQuery
 $.post(link + "/" + retrieve_option_value(select), "", null, "script");
 # $.post(link_to_post, addintional_data, handling_function_after_success, request_type)

 # This would post to /cities/2 with no any params added.
 # Due the the request type being set to script, the returned data would be treated as javascript and executed. So no handling function is needed (that's why null)
}

function retrieve_option_value(select) {
  $(select).children(":selected").val(); # just get the selected option's value
}
map.connect "/test", :controller => "cities", :action => "test" # for test only

map.resources :cities
map.other "/cities/show/:id", :controller => "cities", :action => "show", :method => :get

def test
  render :text => url_for(city_path)     # ambiguous routes error
  render :text => url_for(city_path(0)   # gives /cities/0
  render :text => url_for(other_path)    # gives /cities
  render :text => url_for(other_path(0)) # gives /cities/0
end