简体   繁体   English

Rails助手方法问题

[英]Rails helper method issue

I'm new to rails and am having what I think is a very simple issue. 我是Rails的新手,遇到了我认为非常简单的问题。

I have a list of "tasks". 我有一个“任务”列表。 When you click on a task, I want to update its row in the database to mark it as complete (changing the "status" column form 0 to 1). 当您单击任务时,我想更新它在数据库中的行以将其标记为已完成(将“状态”列的形式从0更改为1)。

Here is what the links look like in my view: 在我看来,链接如下所示:

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

And here is what's in my tasks_controller.rb: 这是我的task_controller.rb中的内容:

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

I cannot figure out how to format the link correctly! 我不知道如何正确设置链接的格式! I get this error when loading the view: 加载视图时出现此错误:

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

EDIT rake routes shows: EDIT 耙路线显示:

       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

Put this in your routes.rb: 把它放在你的routes.rb中:

resources :tasks do
  member do
    get :change
  end
end

It will add the helper path change_task passing the task id. 它将添加传递任务ID的帮助程序路径change_task
And change your link to this: 并更改您的链接:

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

And the controller: 和控制器:

def change

EDITED: 编辑:

To make it an ajax call, you got it right, add :remote => true to your link like this: 要使它成为一个ajax调用,您做对了,将:remote => true添加到您的链接中,如下所示:

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

This way, the response on your controller is expected to be on a js format. 这样,控制器上的响应就应该是js格式。

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

When you do this, you are expected to have a change.js.erb file on your views folder that make all the changes to the page. 执行此操作时,您将在views文件夹中拥有一个change.js.erb文件,该文件对页面进行了所有更改。 Something like this: 像这样:

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

Remember that if you do things this way, you will need a partial( _tasks_list.html.erb ). 请记住,如果您以这种方式进行操作,则将需要一个partial( _tasks_list.html.erb )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM