A custom Block for the MyPage

Added by frank guthorel almost 7 years ago

Hello,

upfront apologies if I'm reiterating a question that popped up earlier on. Not that proficient in RoR yet, and could not immediately find something online or in the forum, neither in the wiki pages, etc.

Is there somebody who could help me write a snippet, or point me into the right direction for the following:

  • I'd like to add a Block that lists all the projects that are active.
  • I'm able to add Blocks, with some basic rendering towards the screen.
  • However, from the moment I try to mimic behaviour from my/blocks/_documents.rhtml or my/blocks/_news.html - I get errors that the page could not be rendered - probably because the database is not queried properly.

If I'm not mistaken, the folder my/blocks is the only place where I should the definition of an additional Block, or am I wrong?

I have added for instance my/blocks/_projects.rhtml - which works fine.

Any idea how I can query the Projects table, and get all the unarchived Projects in there?

A code snippet from _news.rhtml:

<%= render(:partial => 'news/news', 
            :collection => News.find(:all,
                                     :limit => 10,
                                     :order => "#{News.table_name}.created_on DESC",
                                     :conditions => "#{News.table_name}.project_id in (#{@user.projects.collect{|m| m.id}.join(',')})",
                                     :include => [:project, :author])) unless @user.projects.empty? %>

I thought that using the same logic, but with News and news replaces by Projects and project should do the trick, but that seems to be too simplistic.

As I said - not that proficient with Rails, although I get a good grip on the abstracts of the folders etc.
Any tip is welcome, and much appreciated.

I should probably also note that I'm running Redmine 1.0.3.devel (MySQL), and that plugins like http://www.redmine.org/plugins/my_page_queries are not helping (this plugin seems to break MyPage functionality, as the queries in there are not returning any results, and fail).

Thanks a lot, already.

Cheers,
Frank

Replies (5)

RE: A custom Block for the MyPage - Added by Etienne Massip almost 7 years ago

I thought that using the same logic, but with News and news replaces by Projects and project should do the trick, but that seems to be too simplistic.

Basically, that's it, but replace News with Project instead of Projects. You can query this way any object of the model (see app/model/*.rb) deriving from ActiveRecord::Base.

You can get some help here or here.

RE: A custom Block for the MyPage - Added by frank guthorel almost 7 years ago

Hello Etienne,

thanks!

Unfortunately, I tried that already - I have something quite simple, as follows:

<%= render( :partial => 'projects/project', 
            :collection => Project.find(:all,
                                        :limit => 10)) %>

I tried this Projects, Project etc - to no avail.
I'll check out the links you're providing as well - but if I'm not mistaken, I'm still missing a layout file for the content that is queried, to be rendered properly.

If I look at /views/documents/_documents.rhtml and /views/documents/_news.rhtml - these are the layout templates where the data is actually shown, in HTML code - if I'm correct.

However - there does not seem to be a /views/projects/_project.rhtml file by default - does that make any sense?

I'll check the links you provided first - if you would have more information or if I'm talking silly, thanks for letting me know!

Thanks a lot!

Kind regards,
Frank

RE: A custom Block for the MyPage - Added by frank guthorel almost 7 years ago

Hello,

maybe also something that needs some clarification, where possible:
I'm adding the following in the file views/my/blocks/_projects.rhtml, which is properly rendered to the screen as Block I can choose from the dropdown menu:

<h3>Ongoing projects</h3>
Hello World!

<% 
    active_projects = Project.find(:all) 
    active_projects.each do |active_project|
        print 'test#{active_project}'
    end    
%>

Bye World!

This results in a screen showing

Ongoing Projects
Hello World! Bye World!

which seems weird, as there are projects that should be available here.

Kind regards,
Frank

RE: A custom Block for the MyPage - Added by Etienne Massip almost 7 years ago

That should be "test#{active_project}" instead of 'test#{active_project}' if you want your string to be parsed.
Try

...
<%= Project.find(:all).size %>
...

?

RE: A custom Block for the MyPage - Added by frank guthorel almost 7 years ago

Hello Etienne,

thanks for the tip - I managed to get at least somewhere, with the following in views/my/blocks/_projects.rhtml

<% active_projects = Project.find(:all) %>

<% active_projects.each do |active_project| >
<p><
= active_project.name ></p>
<
end %>

<%= debug active_projects %>

I'll give it another spin this evening - there's a couple of custom fields for a Project that we use here, like start date and end date, that I need to join in the Find query.

Thanks for helping out, really appreciate it.

Kind regards,
Frank

(1-5/5)