Error when calling posts.content - class

I keep getting this error
undefined method`content'for"Post::ActiveRecord_Associations_CollectionProxy:0x00000104e87060"
When I do <%= #user.posts.content %>
but when I do <%= #user.posts.count %> everything shows up normal and it works... I don't understand why I can call count and it show's the number of posts in the users show.html.erb page but when I try and show the content it throws and error.
Any ideas?

Instead of <%= #user.posts.content %>
Use this
<% #user.posts.each do |post| %>
<%= post.content %>
<% end %>
#user.posts return a collection of posts. So, when you call count you get the count of all records returned by #user.posts. BUT in order to access content attribute of each post, you will need to iterate over the collection and display the relevant data.

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).

HTMLHelper.Beginform query

I'm learning about HTMLHelpers in ASP.NET MVC.
To render the form HTML tag you would write something like
<% using(Html.BeginForm("HandleForm", "Home")) {%>
<!--Form content goes here-->
<% } %>
or
<% Html.BeginForm(); %>
… Form Contents …
<% Html.EndForm(); %>
To render the a checkbox you would use
<%= Html.CheckBox("bookType") %>
What I would like to know is is why we need to use <% when we use BeginForm whereas we need to use <%= when we use other HTMLHelper methods
Cheers,
Html.CheckBox returns a string of HTML containing an <input> tag.
You need to print this string to the page by writing <%= ... %>.
Html.BeginForm prints the HTML inside the method (by calling Response.Write), and doesn't return HTML. (instead, it returns an IDisposable, so that you can use it in a using block)
Since you aren't printing its return value, you put it in a <% ... %> block, which executes code without printing its results.
<% %> wraps a code block
<%="string" %> is equivalent to <% Response.Write("string") %>
and in ASP.NET MVC 3 you can automatically HtmlEncode with <%: "<htmlTag>" %>
You can definitely write <%=Html.BeginForm() %> but you will also need to write <%=Html.EndForm() %>. Wrapping Html.BeginForm() within a using block will just render the closing </form> tag for you.
Because <%= means "Print this for me", pretty much the same as doing:
<% Response.Write("content"); %>
When <% means that you have a code-block that might do more than just print the value you have nested in it.

ASP.NET MVC2 Checkboxes in a table

I'm currently trying to create a view in asp.net MVC2 which includes a checkbox in each row. This will be a "mark" box, and there will be a button on the view which can then be used for multiple deletion.
I have a model which contains a list of the objects being listed, so I have some code which reads as:-
<% foreach (CancelledCard item in Model.CancelledCards) { %>
I've then tried to use
<%: Html.CheckBoxFor(item >= item.Checked) %>
but I'm getting an error.
What am I missing? What's the right way to do this in MVC2?
In C# a lambda expression uses => and not >=:
<%: Html.CheckBoxFor(item => item.Checked) %>
Also instead of saying that you get an error, you could have posted the error message. It would have been much more clear.
<%: Html.CheckBoxFor(item >= item.Checked) %>
is wrong. It should be:
<%: Html.CheckBoxFor(item => item.Checked) %>