单击项目名称显示任务列表

时间:2013-03-04 09:10:42

标签: javascript jquery ruby-on-rails

我有两个模型项目和任务。每个项目都有很多任务。

项目索引页面上有一个项目列表。

我希望,当用户点击项目名称时,项目列表除了项目列表外显示

我尝试类似的东西。

的application.js

$(document).ready(function(){
  $('#projects').on('click', 'h2', function(){
    $('#tasks').append("<%= escape_javascript render(:file => 'tasks/index.html.erb') %>");
  });
});

将application.js文件扩展名更改为js.erb?

是否正确

更新

我工作的结果 _project.html.erb

<%= link_to " #{project.title} tasks list", project_tasks_path(project), :remote => true %>

task_controller.rb

  def index
@project = Project.find(params[:project_id])    
@tasks = @project.tasks
respond_with @tasks

index.js.erb的

$('#project-tasks').html("<%= j render :file => 'projects/tasks/_tasks', :locals => { :tasks => @tasks }  %>");

_tasks.html.erb

<%= render @tasks %>

3 个答案:

答案 0 :(得分:0)

我认为最好将其保存为somethingelse.js.erb而不是更改application.js

答案 1 :(得分:0)

您无法将应用程序js重命名为其他内容。最好制作新的js.erb文件并将代码放入其中

答案 2 :(得分:0)

您应该使用rails方式来执行此操作。使用ajax调用并生成js.erb文件以更新视图。 见http://railscasts.com/episodes/205-unobtrusive-javascript