【问题标题】:Rails helper method issueRails 辅助方法问题
【发布时间】:2012-11-13 17:51:02
【问题描述】:

我是 Rails 新手,我认为这是一个非常简单的问题。

我有一个“任务”列表。当您单击一个任务时,我想更新它在数据库中的行以将其标记为完成(将“状态”列从 0 更改为 1)。

这是我认为链接的样子:

<td><%= link_to t.name, change_task_status_path(:id => t.id) %>

这是我的 tasks_controller.rb 中的内容:

def change_task_status
  @t = Task.find_by_id(params[:id])
  @t.status = '1' # 1 = complete
  @t.save
  render :nothing => true
end

我不知道如何正确格式化链接!加载视图时出现此错误:

undefined method `change_task_status_path' for #<#<Class:0x3a6c144>:0x3a69d54>

编辑 rake 路线显示:

       tasks GET    /tasks(.:format)             tasks#index
             POST   /tasks(.:format)             tasks#create
    new_task GET    /tasks/new(.:format)         tasks#new
   edit_task GET    /tasks/:id/edit(.:format)    tasks#edit
        task GET    /tasks/:id(.:format)         tasks#show
             PUT    /tasks/:id(.:format)         tasks#update
             DELETE /tasks/:id(.:format)         tasks#destroy
      phases GET    /phases(.:format)            phases#index
             POST   /phases(.:format)            phases#create
   new_phase GET    /phases/new(.:format)        phases#new
  edit_phase GET    /phases/:id/edit(.:format)   phases#edit
       phase GET    /phases/:id(.:format)        phases#show
             PUT    /phases/:id(.:format)        phases#update
             DELETE /phases/:id(.:format)        phases#destroy
    projects GET    /projects(.:format)          projects#index
             POST   /projects(.:format)          projects#create
 new_project GET    /projects/new(.:format)      projects#new
edit_project GET    /projects/:id/edit(.:format) projects#edit
     project GET    /projects/:id(.:format)      projects#show
             PUT    /projects/:id(.:format)      projects#update
             DELETE /projects/:id(.:format)      projects#destroy

【问题讨论】:

  • 运行 rake routes 以查看您的路线名称实际上是什么。
  • 我正在编辑我的问题以显示我的 rake 路由响应。路径似乎根本没有出现。
  • 尝试查看此链接以获取更多信息:guides.rubyonrails.org/routing.html。添加控制器操作时,您不会自动访问它。您必须在您的 routes.rb 文件中定义它。然后,根据您如何定义它,它将决定您的帮助链接的外观。

标签: ruby-on-rails helper link-to


【解决方案1】:

把它放在你的 routes.rb 中:

resources :tasks do
  member do
    get :change
  end
end

它将添加辅助路径 change_task 传递任务 ID。
并将您的链接更改为:

<td><%= link_to t.name, change_task_path(:id => t.id) %>

和控制器:

def change

已编辑:

要使其成为 ajax 调用,您做对了,将 :remote =&gt; true 添加到您的链接中,如下所示:

<%= link_to t.name, change_task_path(:id => t.id), :remote => true %>    

这样,控制器上的响应应该是js 格式。

def change
  # do your thing
  respond_to do |format|
    format.js
  end
end

执行此操作时,您的视图文件夹中应该有一个 change.js.erb 文件,该文件会对页面进行所有更改。像这样的:

$('#tasks_list').children().remove();
$('#tasks_list').html(
"<%= j(render('tasks_list')) %>"
);

请记住,如果您这样做,您将需要一个部分(_tasks_list.html.erb)。

【讨论】:

  • 是的,把方法名改成def change。忘记了,抱歉。 =]
  • 你就是男人!谢谢!!最后一个请求。有没有一种简单的方法可以使它成为在后台运行的 ajax 调用?我知道使用 :remote => true 会有所帮助,但不确定链接格式的位置。
  • 完美!那成功了。我实际上省略了respond_to,因为我只需要向链接添加一个类以显示它已被更改,我使用jquery onClick 执行此操作。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多