【问题标题】:custom routing in rails 4: how to set routing for this custom method to be invokedrails 4 中的自定义路由:如何为要调用的自定义方法设置路由
【发布时间】:2014-01-01 21:59:03
【问题描述】:

更新后的帖子

我有一个带有自定义方法“refresh_table”的“uploads_controller.rb”文件

 class UploadsController < ApplicationController
   before_action :set_upload, only: [:show, :edit, :update, :destroy]

   # GET /uploads
   def index
     @uploads = Upload.all
 update_file_status
 @uploads = Upload.all
   end

   # GET /uploads/1
   def show
   end

   # GET /uploads/new
   def new
 puts "Running uploads/new"
   @upload = Upload.new()
   end

   # GET /uploads/1/edit
   def edit
   end

   # POST /uploads
   def create
@upload = Upload.new(upload_params)
if @upload.save
    @upload.update!(:status => "1")
    @upload.update!(:f_path => "#{@upload.sourcedata.path}")
    redirect_to uploads_url, notice: "Upload for #{@upload.task.name} was successfully created with file #{@upload.sourcedata_file_name}."
     else
         redirect_to tasks_url, alert: "*** ERROR *** Upload for #{@upload.task.name} did not go through successfully. #{@upload.errors.messages}"
     end
   end

   # PATCH/PUT /uploads/1
def update
    puts "Update method in Uploads controller received params = #{params.inspect}"
    puts "params[:upload][:job] = #{params[:upload][:job].inspect}"
    if (params[:upload][:job] == "parse")
        puts "Passed params[:job]== \"parse\" "

        redirect_to uploads_url, notice: "Parsing data from #{@upload.sourcedata_file_name}....."
        @upload.delay.add_data_to_DB()
    else
        if @upload.update(upload_params)
            redirect_to uploads_url, notice: "#{@upload.sourcedata_file_name} was updated"
        else
            redirect_to uploads_url, notice: "ERRRO #{@upload.sourcedata_file_name} could NOT be updated"
        end
    end
end


   # DELETE /uploads/1
   def destroy
    @upload.destroy
     redirect_to uploads_url, notice: 'Couldnt parse file #{@upload.sourcedata_file_name}'
   end

# GET /uploads/refresh_table
def refresh_table
    @uploads = Upload.all
    update_file_status
    @uploads = Upload.all
    respond_to do |format|
        format.html {redirect to 'index'}           
        format.js # template marked refresh_table.js.erb will be evoked
    end
end


   private
   # Use callbacks to share common setup or constraints between actions.
   def set_upload
     @upload = Upload.find(params[:upload][:id])
   end

   # Only allow a trusted parameter "white list" through.
   def upload_params
    params.require(:upload).permit(:sourcedata, :task_id, :status, :f_path, :job)
   end

def update_file_status
@uploads.each do |u|
    if(File.exist?(u.f_path))
        puts "File #{u.sourcedata_file_name} exists"
    else
        puts "File #{u.sourcedata_file_name} has been removed"
        u.update!(:status => "-1")
    end
end
end

  end 

routes.rb:

 blah
 blah
 blah

  resource :uploads do
  member do
   post "parse"
  end
  collection do
   get "refresh_table", to: "refresh_table"
  end
 end

耙路线:

  blah
  blah
          parse_uploads POST   /uploads/parse(.:format)          uploads#parse
  refresh_table_uploads GET    /uploads/refresh_table(.:format)  refresh_table#refresh_table
                uploads POST   /uploads(.:format)                uploads#create
            new_uploads GET    /uploads/new(.:format)            uploads#new
           edit_uploads GET    /uploads/edit(.:format)           uploads#edit
                        GET    /uploads(.:format)                uploads#show
                        PATCH  /uploads(.:format)                uploads#update
                        PUT    /uploads(.:format)                uploads#update
                        DELETE /uploads(.:format)                uploads#destroy

然后我重新启动服务器并尝试通过将其输入为 url 来测试路由是否有效

  http://localhost:3000/uploads/refresh_table

这让我在浏览器中出现错误:

 ActionController::RoutingError at /uploads/refresh_table
 uninitialized constant RefreshTableController

 Local Variables

 params 
 {:action=>"refresh_table", :controller=>"refresh_table"}
 default_controller 
 true
 controller_param   
 "refresh_table"

 #<NameError: uninitialized constant RefreshTableController>

服务器日志说:

   Started GET "/uploads/refresh_table" for 127.0.0.1 at 2014-01-02 20:28:43 -0500
   ActiveRecord::SchemaMigration Load (0.5ms)  SELECT `schema_migrations`.* FROM   `schema_migrations`

   ActionController::RoutingError - uninitialized constant RefreshTableController:

还注意到“索引”方法从用于上传的 routes.rb 中消失了。

我该如何解决这个问题?谢谢

【问题讨论】:

  • 你能显示 uploads_controller.rb 文件吗?
  • 当然。我用新信息更新了完全更新的帖子。请看
  • 您的路由似乎调用了 refresh_table_controller 而不是 uploads_controller。请您重新更新您的 rake 路线。

标签: ruby-on-rails routes url-routing actiondispatch


【解决方案1】:

itsnikolay 响应正常,但生成此网址:' http://localhost:3000/upload/refresh_table(上传而不是上传)

如果你想使用http://localhost:3000/uploads/refresh_table,你可以在你的路由文件中添加这一行:

match 'uploads/refresh_table', to: 'upload#refresh_table', via: :get

希望对您有所帮助。

【讨论】:

  • 在尝试了您的解决方案后,我更新了上面的帖子。仍然出现错误
【解决方案2】:
resource :uploads do
  member do
    post "parse"
  end
  collection do
    get "refresh_table"
  end
end

【讨论】:

  • 感谢您的回答,不幸的是还是同样的错误:(
【解决方案3】:

你的 respond_to 块是罪魁祸首。

试试:

respond_to do |format|
    format.html { redirect_to uploads_path }           
    format.js # template marked refresh_table.js.erb will be evoked
end

redirect 不是你要找的,你要的是redirect_to

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 2011-06-05
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    相关资源
    最近更新 更多