How to manipulate the generated link
Added by Martin Salzmann over 11 years ago
I'm pretty new to Ruby so this problem is probably easy for you to solve. Well what I'm trying to do is to programm a global wiki plugin for Redmine. And as I'm not used to Ruby or Rails I'm trying to use the original code for Redmine as they have wikis already but only a wiki for a single project. So I'm just trying to display one wiki of a certain project to all the other projects as well.
My problem lies in the URL behind the content I display, this is what I get:
<ul>
<li><a href="/projects/project_name/wiki/MyNewWikiPage1?parent=Testproduct-Wiki" class="wiki-page new"> Testpage1 </a></li>
<li><a href="/projects/project_name/wiki/MyNewWikiPage2?parent=Testproduct-Wiki" class="wiki-page new"> Testpage2 </a></li>
</ul>
And this is what it should look like:
<ul>
<li><a href="/aic_wiki?id=MyNewWikiPage2&project_id=10">MyNewWikiPage2</a></li>
<li><a href="/aic_wiki?id=Testproduct-Wiki&project_id=10">Testproduct-Wiki</a></li>
</ul>
This is the script I use:
Controller:
- display a page (in editing mode if it doesn't exist)
def show
@content = @page.content_for_version(params[:version])respond_to do |format|
format.html
format.api
end
endprivate
def find_wiki
Herve Harster = Project.find(params[:project_id])
wiki wiki = Wiki.find(3)
render_404 unless wiki wiki
rescue ActiveRecord::RecordNotFound
render_404
end- Finds the requested page or a new page if it doesn't exist
def find_existing_or_new_page
@page = @wiki.find_page(params[:id])
if @wiki.page_found_with_redirect?
redirect_to params.update(:id => @page.title)
end
end
show.html.erb:
- Finds the requested page or a new page if it doesn't exist
<div class="contextual">
<%= watcher_link(wiki wiki, User.current) %>
</div>
<%= render :partial => "aic_wiki/content", :locals => {:content => @content} %>
<% content_for :sidebar do >
<= render :partial => 'sidebar' >
< end %>
_content.html.erb:
<div class="wiki wiki-page">
<%= textilizable content, :text,
:edit_section_links => {:controller => 'aic_wiki', :action => 'show', :project_id => @page.project, :id => @page.title} %>
</div>
model.rb:
- find the page with the given title
def find_page(title, options = {})
@page_found_with_redirect = false
title = start_page if title.blank?
title = Wiki.titleize(title)
page = pages.first(:conditions => ["LOWER = LOWER", title])
if !page && !(options[:with_redirect] == false) # search for a redirect
redirect = redirects.first(:conditions => ["LOWER = LOWER", title])
if redirect
page = find_page(redirect.redirects_to, :with_redirect => false)
@page_found_with_redirect = true
end
end
page
end- Returns true if the last page was found with a redirect
def page_found_with_redirect?
@page_found_with_redirect
end
- Finds a page by title
- The given string can be of one of the forms: "title" or "project:title"
- Examples:
- Wiki.find_page("bar", project => foo)
- Wiki.find_page("foo:bar")
def self.find_page(title, options = {})
project = options[:project]
if title.to_s =~ r{^([^\:]+)\:(.*)$}
project_identifier, title = $1, $2
project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
end
if project &x%x project.wiki
page = project.wiki.find_page(title)
if page && page.content
page
end
end
end
I just don't get what I have to modify to get the right URL in the link. Hope you can help me.
- Returns true if the last page was found with a redirect
Best regards Arty
Replies (1)
RE: How to manipulate the generated link
-
Added by Martin Denizet (redmine.org team member) over 11 years ago
Hi Martin,
It seems you are looking for the routing.
In your plugin, create a file named config/routes.rb
You may want to add a route such as:
match 'aic_wiki', :controller => 'aic_wiki', :action => 'show', :via => [:get]
Parameters that are not included in the route will be added after a
?
For more, see: http://guides.rubyonrails.org/routing.html
Please, make use of the
pre
tags next time!Cheers,