为什么此方法的Rails自定义路由不起作用

时间:2012-07-14 08:23:29

标签: ruby-on-rails routes rake custom-routes

我试图了解为什么此方法的自定义路由会引发异常:

tasks_controller.rb
    def run
        @task = Task.find(params[:id])
        @task.run_task
        flash[:notice] = "Running Task #{@task.task_name}in the background, please click     show to see results after a few seconds"
    redirect_to tasks_url
    end

任务模型 task.rb

  def run_task
    if task.task_name == "game_state_count"
        task.description =  "Running task game_state_count"
    elsif task.task_name == "file_reader"
        task.description"Running task file_reader"
    elsif task.task_name == "copy_dupes_from_listings"
        task.description "Running task copy_dupes_from_listings"
    elsif task.task_name == "remove_dupes_from_listings"
        task.description "Running task remove_dupes_from_listings"
    end
  end

的routes.rb

     #For Admin Tasks run method
     match "tasks/run/(params[:id])" => "tasks#run", :as => :run_tasks_path

任务/ index.html.erb

<h1>Tasks</h1>

    <table>
      <tr>
       <th>Name</th>
       <th>Description</th>
       <th>Run</th>
        <th>Status</th>
        <th>Start Time</th>
      </tr>

    <% @tasks.each do |task| %>
      <tr>
       <td><%=task.name %></td>
       <td><%=task.description %></td>
       <td><%= link_to 'Run', run_tasks_path  %></td>
       <td><%=task.Status %></td>
       <td><%=task.start_time %></td>
       <td><%= link_to 'Show', task %></td>

     </tr>
    <% end %>
    </table>

    <br />

 <%= link_to 'New Task', new_task_path %>

异常

NameError in Tasks#index

Showing /Users/AM/Documents/RailsWS/appXXX/app/views/tasks/index.html.erb where line #16 raised:

undefined local variable or method `run_tasks_path' for #<# <Class:0x000001028b9ce8>:0x000001028b4450>

Extracted source (around line #16):

13:   <tr>
14:     <td><%=task.name %></td>
15:     <td><%=task.description %></td>
16:     <td><%= link_to 'Run', run_tasks_path  %></td>
17:     <td><%=task.Status %></td>
18:     <td><%=task.start_time %></td>
19:     <td><%= link_to 'Show', task %></td

1 个答案:

答案 0 :(得分:1)

像这样重写路线

match "tasks/run/:id" => "tasks#run", :as => :run_tasks

更新:并更改

<td><%= link_to 'Run', run_tasks_path  %></td>

<td><%= link_to 'Run', run_tasks_path(taks)  %></td>