Question

Im trying to see why this custom route for this method throws an exception:

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 model 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

tasks/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 %>

Exception

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
Was it helpful?

Solution

Rewrite route like this

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

upd: And change

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

to

<td><%= link_to 'Run', run_tasks_path(taks)  %></td>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top