0

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'
Community
  • 1
  • 1
dora
  • 1,314
  • 2
  • 22
  • 38
  • Is this link constant or will it change ? If yes than which part of the url? – sjain Feb 28 '13 at 08:51
  • I just edited the question. Check in the heading Model . Thanks :) – dora Feb 28 '13 at 08:55
  • try to have the link as: `match '/GitRepos'=>'GitRepos/bootstrap/Readme.md'` – sjain Feb 28 '13 at 08:56
  • It doesn't work that way. It will look for a controller that is being matched to, in this case 'GitRepos/bootstrap/Readme.md' Where as GitRepos/bootstrap/Readme.md is a path address on the local computer – dora Feb 28 '13 at 08:58

1 Answers1

0

Try

match '/GitRepos/:github_link'=>'markdowns#view'
Alper Karapınar
  • 2,694
  • 1
  • 25
  • 36
  • That does not go to Markdown controller. It directly goes to local computer location. – dora Feb 28 '13 at 10:16