Display records in refinery page - refinerycms

I need to get the record from database and i want to display that record value on home page. My table name is refierny_about. I have a model. It was created while I generating the engine.
Thanks for your advise.

My guess is "About" is your custom extension do this.
The syntax here is: The Refinery Namespace::Your Extension Namespace::Extension Model Name
so you can use the controller is Refinery::Abouts::About.all
app\decorators\controllers\refinery\pages_controller_decorator.rb here past this code
Refinery::PagesController.class_eval do
def home
#posts = Refinery::Abouts::About.all
end
end
and its your html code.
<% #posts.each do |a| %>
<p><%= link_to about.title, refinery.abouts_about_path(a) %></p>
<% end %>

Related

Displaying Rails Required Fields Form Errors All At Once

I'm using the Rails form and I'm also using Bootstrap tabs. I have 4 tabs that a user fills out and on the 4th tab is the submission button. I have a number of required: true fields in the form, and when I go to click on the submit button leaving any of these fields blank, the pop-up saying "Please fill out this field" appears.
Question: How can I just have a box appear with ALL the error messages, instead of each individual error message appearing one at a time?
I've read a number of posts, and have tried most suggestions (with exception to ones that include JS, as I'm hoping there is a strong solution not including JS). I put below some code below my submit button that I tried but it doesn't display anything as the individual box error message I think overrides it.
Any help is appreciated.
_Form:
<%= form_for(#property, html: { multipart: true }) do |p| %>
...
<%= p.file_field :picture, :multiple => true, name: "property_attachments[picture][]", size: 2 %>
<%= p.submit "Submit", class: 'btn btn-primary' %>
<% if #property.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(#property.errors.count, "error") %>.
</div>
<ul>
<% #property.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
Figured it out and learning a lot about Rails Forms. I was using required: true for a number of fields and this created individual pop-ups on each error one by one. I removed all of the required: true fields and then added validations in the model (eg. validates: :price, presence: true) and then the error message I used above in my question displayed any missing fields.

how to include a template with parameters in EJS?

As a real beginner in EJS, I have two charts in my html page, so I want to use my partial twice:
<% include partials/spider-chart.ejs %>
But I need to pass some parameters inside the ejs to differentiate between graphs.
What is the best way?
#Naeem Shaikh solution works.
Though include also gives you more intuitive way of including a partial template and also passing context variables to that as found in documention section of ejs.
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }); %>
</ul>
I think you want to render two different charts using same partial ejs template, just by providing different data(within the main ejs file).
You can just define a variable, which will be assigned to the data, which the first chart will use, than include the chart.ejs file, again change the data, and include the partial ejs file(chart.ejs) again, so now you have two files which can use same variable(data), but can plot different chart based on value assigned to data.
For Example:
<% var data= 'data to be used by first chart(parameter)'; %>
<% include partials/spider-chart.ejs %>
// re-initializing data for second chart
<% data= 'data to be used by second chart(parameter)'; %>
<% include partials/spider-chart.ejs %>
where your spider-chart.ejs file could be something which will use data
spider-chart.ejs
<li>
<%= data %> // just an example
</li>
here, as you use data, the data variable accessed by both charts will be different because you are reassigning values for data before every chart.
You can pass single as well as multiple data here is how to do it
In render funtions
We can pass multiple data as an object like this
app.get("/account", function(req, res) {
res.render("account", {
name: 'Jon Snow',
age: 35
});
});
And then can access the data inside account using ejs simple template tags like this
<h2> hello <%= name %> </h2>
<p> your age is <%= age %> </p>
In Partial views
Pass the data like this
<%- include('partials/logout', {name='triyon'}) %>
And access it like we did above
<h2> logged out <%= name %> </h2>
This is the best workaround by just passing the view file name as a context while rendering the base ejs file.
/base.ejs:
<html>
<%- include(content) %>
</html>
/extra.ejs:
<div> some content which is to be added in base ejs file </div>
/controller.js:
res.render('base', { content: 'extra' })

Which code should I use to show related posts/docs on a docpad index-website?

Unfortunately I am no scripting-guru. So this might be obvious for the most of you.
I installed the docpad-plugin-related plugin and it works on individual posts/docs when I include the following code (see instructions)
Now, I would like to show posts/docs sorted by tag on my index-website. For example posts/docs tagged with webdesign should show up as a list. How do I have to change the following code for this purpose?
<ul>
<% for document in #document.relatedDocuments: %>
<li><%= document.title %></li>
<% end %>
</ul>
Is there a solution or does the plugin only work on individual posts/docs?
The docpad-plugin-related plugin is all about getting the related documents on an individual document which is not what you want. You don't need a plugin to do what you want.
Assuming your documents all have a tags property, you can get a list of documents that match a specific tag (e.g. webdesign) with code on your index page like this:
<ul>
<% for doc in #getCollection('documents').findAll({tags: '$in': `webdesign`}).toJSON(): %>
<li><%= doc.title %></li>
<% end %>
</ul>
If you want tag specific indexes for all of your tags, you probably want to look at docpad-plugin-tagging (link).

How exactly does Rails prefills forms?

I have a (simple) question for my own curiosity:
I'd like to find out how Rails prefill forms with posted values like... you know, when there's a validation error on some models' attributes then you do something like "render :edit" and the form is magically prefilled.
What exactly are the mechanisms that Rails use to do such a thing? I didn't manage to find any documentation on this subject and I'd like to understand the magic.
So if someone can give me some explanations on this subject, I'll be glad to read that!
Thanks!
[Edit] And a subsidiary question: when a model inherits from another (STI) do we have to do something in particular to prefill forms?
You are mostly using the form_for helper in this style:
<%= form_for #person do |f| %>
<!-- Some more stuff here -->
<%= f.text_field :first_name %><br />
<!-- Some more stuff here -->
<% end %>
What this essentiall does is, it generates a text field that is filled with the value of #person.first_name.to_s. When an error happens, #person.first_name is filled with the errornous value. If you create a person (#person = Person.new), then #person.first_name.to_s is "".
So rails just fills the text field with the value, the attribute has.
f by the way is a rails FormBuilder. It's methods are documented here, if you want to take a closer look at the source.

How do I change the text_area default size for a form in rails 3?

I'm going through Agile Web Development with Rails and I'm having some trouble with the form helper text_area. Specifically, I want to make the text area smaller (the form submits correctly and everything goes into the database correctly). According to the book this code should work:
<%= form_for(#request) do |f| %>
<div class="actions">
...
<div class="field">
<%= f.label :quote_details, "*Items required:" %>
<%= f.text_area :quote_details, :rows=>5, :cols=>40 %>
</div>
It seems that no matter what numbers I put for :rows or :cols, the box stays the same default size. Instead of :rows and :cols, I used :size=>"3x40" and size=>"5x8" etc.. but the box still always stays the same size.
As an experiment I tried
<%= f.text_field :quote_details, :size=>"300*39" %>
That changed the number of columns, but removing the :size and putting :rows or :cols has no effect (it goes back to a default size for a text_field).
I did see this:
Change default Rails text_area helper rows/cols
I tried answer 1, but the answer given didn't work for me. I don't really understand what the second and third answers mean. I might be doing something else wrong or maybe it's a different problem.
I'm just stumped. Any help or ideas on what's going on would be greatly appreciated. Thanks for any responses.
Oh, I'm using rails version 3.0.0 and ruby 1.9.2p0 on vista.
your first code segment has :cols => 40% instead of 40?
I would also consider using CSS to do it, as that can make changing the look of the webpage isolated to the CSS presentation layer.
try do it with form_with. For me it works.
<%=form_with, local: true do |form| %>
<%= form.label :comment %>
<%= form.text_area :body, :rows=>10, :cols=>60 %>
<% end %>