Ruby on rails RubyonRails中Show视图上的处理按钮

Ruby on rails RubyonRails中Show视图上的处理按钮,ruby-on-rails,ruby,Ruby On Rails,Ruby,我的应用程序上有一个名为Source的应用程序实体。此实体有一个名为url的属性 我想对我的SHOW view进行处理。因此,我在show视图中添加了一个按钮,以调用我的控制器并执行此处理 这是我的路线 get '/process', to: 'sources#read', as: 'read' 这是我的控制器方法: class SourcesController < ApplicationController before_action :set_source, only: [

我的应用程序上有一个名为Source的应用程序实体。此实体有一个名为url的属性

我想对我的SHOW view进行处理。因此,我在show视图中添加了一个按钮,以调用我的控制器并执行此处理

这是我的路线

  get '/process', to: 'sources#read', as: 'read'
这是我的控制器方法:

class SourcesController < ApplicationController
  before_action :set_source, only: [:show, :edit, :update, :destroy, :read]
  access all: [:index, :show, :new, :edit, :create, :update, :destroy, :read], user: :all
def read
    require 'rss'
    require 'open-uri'
    url = @source.url
    open(url) do |rss|
      feed = RSS::Parser.parse(rss)
      puts "Title: #{feed.channel.title}"
      feed.items.each do |item|
        puts "Item: #{item.title}"
        puts "Link: #{item.link}"
        puts "Description: #{item.description}"
      end
    end
    render template: 'rss_reader/home'
  end

我做错了什么

您正在使用
读取路径(@source)
访问路由,该路径应将参数
id
设置为值
@source.id
,但未定义路由以支持路径中的任何参数

我相信
read
是属于
Source
的单个实例的操作。所以,你应该。通过这种方式,您将能够访问控制器中的
params[:id]
,您的before_操作
set_source
将正常工作

将路线定义更改为:

resources :sources, only: [...] do    # Keep the `only` array just as you have now.
  get '/process', to: 'sources#read', as: 'read', on: :member
end

您正在使用
读取路径(@source)
访问路由,该路径应将参数
id
设置为值
@source.id
,但未定义路由以支持路径中的任何参数

我相信
read
是属于
Source
的单个实例的操作。所以,你应该。通过这种方式,您将能够访问控制器中的
params[:id]
,您的before_操作
set_source
将正常工作

将路线定义更改为:

resources :sources, only: [...] do    # Keep the `only` array just as you have now.
  get '/process', to: 'sources#read', as: 'read', on: :member
end

您是否可以显示
rake routes | grep read
的输出?是否有任何用于操作
read
的前置过滤器?如果有问题,请将其包括在内。Jagdeep。我了解你的想法,并改变了问题的描述。错误现在发生了变化。能否显示
rake routes | grep read
的输出?是否有任何用于操作
read
的前置过滤器?如果有问题,请将其包括在内。Jagdeep。我了解你的想法,并改变了问题的描述。错误现在更改。更新了答案并进行了解释。更新了答案并进行了解释。
resources :sources, only: [...] do    # Keep the `only` array just as you have now.
  get '/process', to: 'sources#read', as: 'read', on: :member
end