我的应用程序中的下拉菜单(由select_tag构建)应在用户更改下拉菜单中的值时立即调用filter-category-action并点击"Go"按钮.
现在我想摆脱'Go'按钮并让观察者(observe_field?)在用户更改下拉菜单中的值时立即调用filter-category-action.
下面你看到我写的代码.它使用'Go'-Button工作,但只是更改下拉菜单中的值不起作用.我的observe_category_select-helper出了什么问题?
视图 - 部分与下拉菜单和项目列表
<!-- drop down menu -->
<% form_tag(filter_category_path(:id), :method => :post, :class => 'categories') do %>
<label>Categories</label>
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, @category_id)) %>
<!-- i would like to get rid of this button -->
<%= submit_tag "Go" %>
<% end %>
<!-- list of projects related to categories chosen in drop down menu -->
<ul class="projects">
<% @projects.each do |_project| %>
<li>
<%= link_to(_project.name, _project) %>
</li>
<% end %>
</ul>
<%= observe_category_select …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码在rails 3中设置AJAX操作.代码的AJAX部分似乎有效,但它没有请求正确的文件,我的respond_to为常规HTML提供服务.
路由信息:
resources :zones do
resources :records
end
Run Code Online (Sandbox Code Playgroud)
控制器:
def new
@zone = Zone.new
respond_to do |format|
format.html
format.js
end
end
Run Code Online (Sandbox Code Playgroud)
链接视图(haml):
= link_to 'Add a zone →', new_zone_path, :remote=>true
Run Code Online (Sandbox Code Playgroud)
从link_to生成的HTML(还注意到html实体的失败呈现......但这是另一个问题):
<a href="/zones/new" data-remote="true">Add a zone &#8594;</a>
Run Code Online (Sandbox Code Playgroud)
对于踢,视图/区域的目录列表.我不确定我这样做是否正确,所以我有new.js.rjs和new.rjs.它们都具有相同的内容,但从未被动作拾取.
| `~zones/
| |-_form.html.haml
| |-_record.html.haml
| |-edit.html.haml
| |-index.html.haml
| |-new.html.haml
| |-new.js.rjs
| |-new.rjs
| `-show.html.haml
Run Code Online (Sandbox Code Playgroud)
最后,从我点击链接时的服务器日志:
Started GET "/zones/new" for 127.0.0.1 at Wed Dec 29 00:04:03 -0700 2010
Processing by ZonesController#new as */*
User Load (0.4ms) SELECT "users".* …Run Code Online (Sandbox Code Playgroud) 我正在尝试显示一条Flash消息并使用Ajax呈现它,但是在我刷新页面之前,消息似乎没有出现.
这是我的create.rjs文件:
page.insert_html :top, :feed_items, :partial => 'shared/feed_item', :object => @micropost
page.replace_html :user_info, pluralize(current_user.microposts.count, "micropost")
page[:micropost_form].reset
page.replace_html :notice, flash[:notice]
flash.discard
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序布局视图的相关部分:
<div id= "notice"><% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %></div>
Run Code Online (Sandbox Code Playgroud)
这是我的微控制器的相关部分:
class MicropostsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
respond_to do |format|
if @micropost.save
flash[:success] = "Micropost created!"
format.html { redirect_to root_path }
format.js
else
@feed_items …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails和jquery与RJS模板来执行各种AJAX请求.
对于我的大多数Ajax内容,我将一个提交处理程序附加到我的application.js中的表单,如下所示:
$('#tagging_flickr_photos').submitWithAjax();
$('#tag_submit').click(function() {
$('#flickr-photos-status').show();
});
Run Code Online (Sandbox Code Playgroud)
这会调用表单操作进行一些处理,然后转发到RJS模板,如下所示:
$("#flickr-photos-status").hide();
$("#flickr-photos").fadeIn();
$("#flickr-photos").html("<%= escape_javascript(render(:partial => 'flickr_photos_for_tagging_content')) %>");
Run Code Online (Sandbox Code Playgroud)
这是一种享受.
现在我尝试做同样的事情,但只是基于在下拉列表中选择不同的值而不是提交表单.这是我的javascript将处理程序附加到下拉列表:
$('#film_film_name_id').change(function() {
$.get('/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val());
});
Run Code Online (Sandbox Code Playgroud)
我的控制器方法做了一些处理,然后转发到RJS模板(make_tags.js.erb):
$("#film_tags").val(<%=@tags%>)
Run Code Online (Sandbox Code Playgroud)
但是,模板似乎不会执行.我可以在我的日志中看到它正在调用我的方法并呈现模板的条目,但无论我在模板中放置什么似乎都没有发生.我在那里放了一个Javascript警报,它不会触发.
我假设问题是附加我的Javascript处理程序,但我无法弄清楚我缺少什么.
在此先感谢您的帮助.
嗨,目前我正在开发一个rails项目.我必须重新加载部分页面.
例如,创建操作
def create
# Some transaction to the database
respond_to do |format|
format.html {render :text => 'Add post' }
format.js
end
end
Run Code Online (Sandbox Code Playgroud)
在我的观点posts/create.js.rjs我想重新加载users/_login.html.erb文件这包含帖子总数或帖子的大小,当它创建新帖子时应该重新加载部分页面_login. html.erb并显示帖子递增的大小
在create.js.rjs中,如果我使用page.reload 它重新加载整个页面,如何仅重新加载create.js.rjs中的部分文件_login.html.erb.
#online_exam_controller
class OnlineExamController < ApplicationController
def index
@user=current_user
@employee=Employee.find_by_employee_number(@user.username)
# flash[:notice]="#{@employee.id}"
@subjects=EmployeesSubject.find_all_by_employee_id(@employee.id)
@subject_name=[]
@batch_details=[]
@display_details=[[]]
@count=0
for @t in @subjects
@subject_name[@count]=Subject.find(@t.subject_id,:select=>'name,code,batch_id')
@batch_details[@count]=Batch.find(@subject_name[@count].batch_id,:select =>'name')
@display_details[@count][0]=@subject_name[@count].name+" "+@batch_details[@count].name
@display_details[@count][1]=@t.subject_id
@count+=1
end
end
def show
if params[:subject_id]==''
@question_type=[]
else
@question_type=['multiple_choice','fill_ups','matches','descriptive']
@subject_id=params[:subject_id]
end
respond_to do |format|
format.js {render:action=>'show' }
end
end
def new_multiple_choice
@quiz=MultipleChoiceQuestion.new
@subject=Subject.find params[:id] if request.xhr? and params[:id]
respond_to do |format|
format.js {render :action => 'new_multiple_choice'}
end
end
end
#index.html.erb
<div id="content-header">
<img src="/images/show_settings.png" alt="Subjects"/>
<h1>Subjects</h1>
<h3>Home</h3>
<div id="app-back-button">
<%= link_to_function image_tag("/images/buttons/back.png",:border => …Run Code Online (Sandbox Code Playgroud)