I have an application, that takes a path of a file from the database and renders it to html and displays. The controller, view and routes look like the following:
project/index.html.erb
<p><%= link_to project.title,{:controller => "markdowns" , :action => 'view', :github_link=>project.github_link},:target=>"_blank"%></p>
markdown_controller
class MarkdownsController < ApplicationController
def view
@markdown=MarkdownsHelper.markdown path
end
def path
File.open(Rails.public_path.to_s+params[:github_link].to_s, "rb").read
end
end
MarkdownsHelper.markdown method in the above controller renders the .md.
markdown/view
<%=raw @markdown %>
routes
resources :projects
match '/GitRepos/'=>'markdowns#view'
The problem:
The link that is being displayed after clicking on the title in the prokects/index is localhost:3000/GitRepos?github_link=%2FGitRepos%2Fbootstrap%2FREADME.md
But I want the link to be displayed as localhost:3000/GitRepos/bootstrap/Readme.md
where bootstrap is Project.title and Readme.md is the file that is being read. Also, my Project database has a github_link attribute which stores GitRepos/bootstrap/Readme.md
Model
The link will change according to the project. The Project model has these attributes-> title and github_link. So there are many projects and github_link stores the path addresses of Readme.md file of each project located in the local system.
My Approach
Here is the solution which I have, with my minimal knowledge in Ruby on Rails. Please let me know if can improve on this.
project/index.html.erb
<p><%= link_to project.title,{:controller => "markdowns" , :action => 'view',:title=>project.git_name,:id=>project.git_file},:target=>"_blank" %></p>
routes
match 'GitRepos/:title/:id' =>'markdowns#view'