开发者

Rails 3 can't find a custom action

开发者 https://www.devze.com 2023-02-15 10:29 出处:网络
I\'ve seen this question answered here for Rails 2, but not Rails 3. I have an app I\'m runnin开发者_开发技巧g on localhost called Skynet, which provides one-click access to the scripts that I use re

I've seen this question answered here for Rails 2, but not Rails 3.

I have an app I'm runnin开发者_开发技巧g on localhost called Skynet, which provides one-click access to the scripts that I use regularly:

I have:

config/routes.rb:

Skynet::Application.routes.draw do
  resources :robots do
    member do
      get "cleaner"
    end
  end
end

app/controllers/robots_controller.rb:

class RobotsController < ApplicationController 
  def index
    respond_to do |format|
      format.html
    end
  end
  def cleaner
    @output = ''
    f = File.open("/Users/steven/Code/skynet/public/input/input.txt", "r") 
    f.each_line do |line|
      @output += line
    end
    output = Sanitize.clean(@output, :elements => ['title', 'h1', 'h2', 'h3', 'h4', 'p', 'td', 'li'], :attributes => {:all => ['class']}, :remove_contents => ['script'])
    newfile = File.new("/Users/steven/Code/skynet/public/output/result.txt", "w")
    newfile.write(output)
    newfile.close
    redirect_to :action => "index"
  end
end

(Will refactor later.)

In app/views/robots/index.html.haml I have:

= link_to "test", cleaner_robot_path

When I type rake routes, I get:

cleaner_robot GET    /robots/:id/cleaner(.:format) {:controller=>"robots", :action=>"cleaner"}

So why, then, when I point my browser at http://localhost:3000/, do I get the following?

ActionController::RoutingError in Robots#index

Showing /Users/steven/Code/skynet/app/views/robots/index.html.haml where line #1 raised:

No route matches {:action=>"cleaner", :controller=>"robots"}
Extracted source (around line #1):

1: = link_to "test", cleaner_robot_path
Rails.root: /Users/steven/Code/skynet

Application Trace | Framework Trace | Full Trace
app/views/robots/index.html.haml:1:in `_app_views_robots_index_html_haml___2129226934_2195069160_0'
app/controllers/robots_controller.rb:4:in `index'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None


you defined cleaner as a member function of the resource robots this means you have to supply an id as you can see in your rake routes message /robots/:id/cleaner(.:format)

So your link should look like

= link_to "test", cleaner_robot_path(some_id)

but

I think you want to use cleaner as a collection function:

Skynet::Application.routes.draw do
  resources :robots do
    collection do
      get "cleaner"
    end
  end
end

then your link has to look like:

= link_to "test", cleaner_robots_path

Notice that robot is now plural!

According to your error message I guess you've tried that but used the collection in plural... Maybe you have to restart your server if you are in production mode.

You can read more about this routing stuff in the Ruby on Rails Guide

0

精彩评论

暂无评论...
验证码 换一张
取 消