cq get currentpage's jcr:content/image node property->fileReference - iterator

i m working on a piece of cq component, what i need is to retrieve an image filereference path and render the image on the page.
e.g this is my page structure as shows on picture.
say i m current on index page, i know how to retrieve index page's children page and get their value map of jcr:content, and get all the properties out from jcr:content...
but i dont know how to retrieve it's jcr:content/image node-> the image, and how to retrieve it's filereference property...
here is my code, it crashes...
<%
boolean includeTitle = properties.get("includeTitle", false);
boolean includeImage = properties.get("includeImage", false);
boolean includeSubTitle = properties.get("includeSubTitle", false);
boolean includeDescription = properties.get("includeDescription", false);
String type = currentStyle.get("type", "plain");
%>
<%
Iterator<Page> currentPageChildren = currentPage.listChildren();
while(currentPageChildren.hasNext()){
Page childPage = currentPageChildren.next();
ValueMap childPageProperties = childPage.getProperties();
//trying to retrieve the image node
Node imageNode = childPage.getContentResource("image").adaptTo(Node.class);
String childPageTitle = childPageProperties.get("jcr:title", String.class);
String childPageSubTitle = childPageProperties.get("subtitle", String.class);
String childPageDescription = childPageProperties.get("jcr:description", String.class);
%>
<div>
<% if (includeTitle) { %>
<p><%= childPageTitle%></p>
<% }
if (includeSubTitle) { %>
<p><%= childPageSubTitle%></p>
<% }
if (includeDescription) { %>
<p><%= childPageDescription%></p>
<% } %>
//test to print image's filereference path in string on page
<p><%=imageNode.getProperty("fileReference") %></p>
</div>
<%
}
%>
please help me with some code example, thx

I suggest using the Image class, which takes care of null checks. I think in your code you stumble over a page where imageNode is null or has no property fileReference.
Code snippet:
String fileReference = "";
Resource imgRes = childPage.getContentResource("image");
if (imgRes != null) {
Image image = new Image(imgRes);
fileReference = image.getFileReference();
}

Related

How to pass a singleton object to a partial in rails?

I have the following code in my controller
#current_chat = current_user.sent_messages
.where("created_at > ? and receiver_id = ?", current_user.current_sign_in_at, current_chat[:receiver_id].to_i)
.select("body, created_at").each { |message| message.instance_eval { def type; #type end; #type = 'sent' } }
And I'm passing the #current_chat object to a partial like so:
<%= render partial: 'shared/chat_form', locals: { messages: #current_chat } %>
But I'm getting the following error:
singleton can't be dumped
At the first line in ActiveSupport::MessageVerifier#generate
def generate(value)
data = ::Base64.strict_encode64(#serializer.dump(value))
"#{data}--#{generate_digest(data)}"
end
Any ideas on how to fix this?. Thanks in advance.
You can not use this
#serializer.dump(value)
This is causing the error. Read this link, its all about using singletons in ruby.
link

Learning to Search in Rails

I'm trying to create a search form in my rails application. I've looked up various solutions but they make little sense to me.
I'm getting the following error when I run a search through a form in my rails app. Right now my concern (other than the error) is my instance variable #computers in my index action. I'm pretty sure it's not 'the rails way' to get a search done properly and would love some advice.
Error
undefined method `%' for #<Array:0x5780460>
Parameters after Search
http://localhost:3000/computers?utf8=%E2%9C%93&direction=&sort=&search=bob
Search Form
<%= form_tag computers_path, method: "get" do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Go", name: nil, class: "btn btn-primary" %>
<% end %>
Call to Method
def index
#computers = Computer.where(school_id: current_user.school_id).search(params[:search]).category(params[:category]).order(sort_column + " " + sort_direction)
end
Method
def Computer.search(search)
if search
search = search.downcase
params = []
values = {}
column_names.each do |c|
params << "#{c} LIKE #{c.to_sym}"
values[c.to_sym] = search
end
params.join (' OR ')
where(params,values)
else
all
end
end
You've got the right idea, but invoking the .join method does not change the object on which it is called, it merely returns a string representation. You need to store the return in a variable, something like this: paramsStr = params.join(' OR '). Then simply pass paramsStr to the where clause.
Ultimately, that is what is causing your unidentified method % for Array .... error; this version of the where method is expecting the first parameter to be a string. Check out this documentation, the part about placeholder conditions.
Hope that helps.

Rails View Is Returning Whole Hash Instead of Just Key

This is a quick one:
In my rails view I have:
<% h = { "a" => 100, "b" => 200 } %>
<%= h.each_key { |key| puts key } %>
This is returning the following in the view:
{"a"=>100, "b"=>200}
However, according to the ruby api doc (http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-each_key), it should just return the following:
a
b
Why is the whole hash being listed in the view instead of just the key output? I know that this is probably I stupid question, but I have been stuck on this for awhile.
Thanks in advance for your help.
Write this code for correct result:
<% { "a" => 100, "b" => 200 }.each_key do |key| %>
<%= key %>
<br/>
<% end %>
When you write pusts key key is wrote to log (and console). When you write <%= key %> key is wrote to your page.

How do I manually build form data?

I have the following models:
Subject has_many Points
Point belongs_to Subject
Points are created using a form, and the controller is as follows
def create
#subject = Subject.find(params[:subject_id])
#point = #subject.points.build(params[:point])
if #point.save
flash[:success] = "You have created new data"
redirect_to subject_path(#point.subject_id)
else
render 'new'
end
end
At the moment a user can create Points for each Subject using a form. However, I want to also allow the user to upload mass points from a csv file. For this I am using the csv library (ruby 1.9.3)
After uploading the csv file, I put the csv file into a table as follows
thegrid = CSV.table(path, :headers => true, :header_converters => :symbol)
Where path is the path to the csv. The headers for the csv file match the column names in the database (including the subject_id column number)
I want to loop through the rows in the table and add each one to the database as follows
<% point = Hash.new %>
<% thegrid.each do |row| %>
<%
point = {
"name" => row[0],
"total_points" => row[1],
"subject_id" => row[2]
}
%>
<% #point = #subject.points.build(params[point]) %>
<% end %>
But the above doesn't appear to add the rows to the database. What is the correct way to do this loop, I think it may be the params that are causing a problem
I sorted this issue by updating the code as follows:
<%
params[:point] = {
name: row[0],
total_points: row[1],
subject_id: row[2]
}
%>
<% #point = #subject.points.build(params[:point]) %>

How to create own block helper with two or more nested children?

I'd like to to something nested like that in my views:
<%= helper_a do |ha| %>
Content for a
<%= ha.helper_b do |hb| %>
Content for b
<%= hb.helper_c do |hc| %>
Content for c
... and so on ...
<% end %>
<% end %>
<% end %>
To get for example this:
<tag_a>
Content for a
<tag_b class="child_of_tag_a">
Content for b
<tag_c class="nested_child_of_tag_a child_of_tag_b">
Content for c
</tag_c>
</tag_b>
</tag_a>
This means, each level has access to some information of the level above (that's why they are nested and not completely autonomous methods)
I know how to create a simple helper:
def helper_a(&block)
content = capture(&block)
content_tag :tag_a, content
end
And I know I can pass my arguments to the capture to use them in the view, so something like this to get live up the |ha| of my example
def helper_a(&block)
content = capture(OBJECT_HERE, &block)
content_tag :tag_a, content
end
But where do I define this OBJECT_HERE, especially the class for it, and how can this go on nested with multiple levels capturing each block?
I came up with a couple solutions, but I'm far from being an expert in the Rails templating system.
The first one is using an instance variable :
def helper_a(&block)
with_context(:tag_a) do
content = capture(&block)
content_tag :tag_a, content
end
end
def helper_b(&block)
with_context(:tag_b) do
content = capture(&block)
content_tag :tag_b, content
end
end
def helper_c(&block)
with_context(:tag_c) do
content = capture(&block)
content_tag :tag_c, content
end
end
def with_context(name)
#context ||= []
#context.push(name)
content = yield
#context.pop
content
end
which is used this way :
<%= helper_a do %>
Content for a
<%= helper_b do %>
Content for b
<%= helper_c do %>
Content for c
... and so on ...
<% end %>
<% end %>
<% end %>
And the other solution, which passes the context at each step :
def helper_a(context = [], &block)
context = capture(context.push(:tag_a), &block)
content_tag(:tag_a, content)
end
def helper_b(context = [], &block)
context = capture(context.push(:tag_b), &block)
content_tag(:tag_b, content)
end
def helper_c(context = [], &block)
context = capture(context.push(:tag_c), &block)
content_tag(:tag_c, content)
end
which is used this way :
<%= helper_a do |context| %>
Content for a
<%= helper_b(context) do |context| %>
Content for b
<%= helper_c(context) do |context| %>
Content for c
... and so on ...
<% end %>
<% end %>
<% end %>
But I'd really advise against using either of these solutions if all you're doing is CSS styling and/or Javascript manipulation. It really complicates the helpers, is likely to introduce bugs, etc.
Hope this helps.