我使用了jQuery自動完成插件,我想自定義此事件:當選擇從下拉列表中的元素如何從application.js文件中調用jQuery的控制器動作?
select: function(event, ui) {
$('.topic_field').val(ui.item.topic.name);
return false;
從本質上講,它會觸發回調。截至目前,它只將所選元素添加到文本字段。我希望填充該字段,並讓我的應用程序向視頻更新控制器操作發送POST請求,以便用戶不需要明確地按下按鈕。我怎樣才能做到這一點?
UPDATE:
這裏是視頻控制器的表演形式來看:
<%= form_for @video, :url => {:action => "update"}, :remote => true do |f| %>
<div class="field">
<%= f.text_field :topic_names, :class => "topic_field" %>
</div>
<%= f.submit "Add Topic" %>
<% end %>
這裏是我的jQuery代碼:
var url = $('.edit_video').attr('action');
var val = ui.item.topic.name;
$.post(url, {data:val});
這是我的routes.rb :
resources :videos
resources :video_votes
resources :users
resources :profiles
resources :genres
resources :topics
resources :topicables
resource :session
這裏是我的更新動作:
def update
@video = current_user.videos.find(params[:id])
respond_to do |format|
if @video.update_attributes(params[:video])
format.html { redirect_to(@video) }
format.js
else
format.html { render :action => "edit" }
end
end
end
我只需要通過'type'選項和'url'選項作爲更新操作對應的url? – 2011-04-01 08:28:58
更新操作的相應網址是什麼? – 2011-04-01 08:35:28
您不需要傳遞'type'選項,因爲它默認爲POST,並且沒有明確需要'url'選項,因爲它會在第一個參數中拾取字符串。 – 2011-04-01 08:40:40