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

时间:2010-09-28 02:42:43

标签: javascript ruby-on-rails-3

我试试这个:

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以生成一个有效的网址来“显示”一个城市。所以我尝试硬编码“1”为:id,只是为了让它去,然后覆盖:id在:with:

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的操作,或者我可能真的需要在运行时在客户端动态生成底层的ajax调用。

1 个答案:

答案 0 :(得分:0)

因此,对于您的路线,:id存在,对吧?这就是为什么你必须在生成路线时提供id。

您可以覆盖路线,使其不包含:id,或将:id设置为可选(使用(:id),然后将您的ID附加为param to send。

但是我之前没有使用过remote_function:D

=====更新=====

所以我真的不熟悉remote_function,因为我很少使用内置原型,但是jquery。

您似乎正在提供城市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

在cities / show.js.erb

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

渲染会使用cities / _city.html.erb来提供城市HTML代码

<%= select_tag "cities", cities_options(with_id_as_the_option_value), :onchange => "show_city(this, '#{url_for(ajax_show_city_path)}')" %>
# This will generate something like:
<select name="cities" id="cities" onchange="show_city(this, '/cities')"> # note the path generated
  <option value="1">City 1</option>
  <option value="2">City 2</option>
</select>

需要js功能:

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
}

就是这样。

因此,如果关闭了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替换所选ID的0