I always get routing errors in this Ruby file on Rails to project. It shows me routing error as you can see in below screenshot.
All gems are installed properly it shows me the home page but do not show me the check file or show file page.
Here is my Routes.rb code:
Rails.application.routes.draw do
get 'home/submission3'
post 'home/checkFile'
get 'home/showFiles'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
Here is an image of the error shown:
Here is my controller code:
# C:\Users\attari\RubymineProjects\submission3\app\controllers\home_controller.rb
require 'docx'
require 'find'
class HomeController < ApplicationController
def submission3
end
def checkFile
word = params[:folder][:word]
path = params[:folder][:path]
@files = checkWord(word, path)
puts "FILES ARE #{@files}"
redirect_to home_showFiles_path(@files)
# puts "WORD IS #{word} PATH IS #{path}"
end
def save
@submissions = submission3.new
end
def checkWord(userInput, folderInput)
count = 0
if (Dir.exist?(folderInput))
docx_file_paths = []
filtered_files = []
Find.find(folderInput) do |path|
docx_file_paths << path if path =~ /.*\.docx$/
end
(0..docx_file_paths.length - 1).each { |i|
d = Docx::Document.open(docx_file_paths[i])
d.each_paragraph do |p|
string_ = p.to_s()
if (string_.include? userInput)
puts docx_file_paths[i]
filtered_files.append(docx_file_paths[i])
count = count 1
puts count
else
puts "No word Matched"
end
end
puts count
}
return filtered_files
else
puts "No Directory exist"
end
end
def create
@submission3 = submission3.new(params[:word])
if @submission3.save
redirect_to new_submission_path
end
end
private
def book_params
params.require(:book).permit(:title)
end
end
How can I fix this routes.rb error?
CodePudding user response:
If you look at the error message, you can see that the filename is being appended directly to the route name (/home/showFiles.C:\Users...
). You need to add an id to the route so that Rails knows to expect something there. Your route should look something like this:
get 'home/show_files/:file_name'
[Note that you need to define :file_name
in the context where this will be accessed (your controller).]
Even better would be to use a model as the basis for your route. So, if you have (for example) a File model, you would use:
resources :files
and Rails will generate a whole pile of routes for you automatically (or you can limit the routes by doing something like resources :files, only: [:show, :index, :create, :delete]
).
Basically, I think you need to read up more on how Rails, and routes specifically, are supposed to work. RailsGuides is probably a good place to start. If you work with Rails and its conventions, you'll find your life a lot easier! Rails prefers convention over configuration!
Also, on a side issue, you're not following Rails convention in naming your methods and variables. CamelCase is used in object names (like class HomeController
) but not method names or variable names - Rails uses snake_case for these. Again, if you stick to Rails convention (show_files
not showFiles
, for example), you'll find things go a lot smoother. Following the Rails conventions means Rails will seamlessly stitch things together for you; fail to follow them and Rails will be a complete nightmare.
CodePudding user response:
Based on the error, it looks like you are trying to access /submission3
route even though you have defined /home/submission3
route. Updating the route should fix your routing error.