Ruby block printing/displaying issue

Ruby block printing/displaying issue



I am a newbie. The tutorial I am following says anything entered between blocks will be printed/displayed. However, I have not been able to do it.



https://www.youtube.com/watch?v=0OzDgi0zqJU



at 3:49, he prints out hello or any word between the block. What am I doing wrong that it does not print out anything I enter between block.


<h1>All Posts</h1>

<% @posts.each do |post| %>
Hello or something else
<% end %>



RESULT: ALL POSTS





You don't have any post in your database I guess (means @posts variable has no object). Otherwise, for each post Hello or something else will print in the html.
– Emu
Aug 20 at 4:38


@posts


Hello or something else





That was my mistake, to say it should be enclosed within <%= %>. I assumed something else. Thank you.
– Kedarnag Mukanahallipatna
Aug 20 at 4:45





What does your actual code contains: 'Hello or ...' OR something else?
– Jagdeep Singh
Aug 20 at 4:45





@JagdeepSingh, I just wondered why it doesn't print as I am following tutorial step by step( No code yet ). Thank you for the response.
– Emin
Aug 21 at 0:01




1 Answer
1



You don't have any post in your database I guess (means @posts variable has no object). Otherwise, for each post Hello or something else will print in the html.


@posts



when you want to print something in .erb, you will have to enclose it within <%= %> only when you want to show something fetch from the database/from the controller instance variable/from the helper file.


.erb


<%= %>



If you loop through like below code then you will get the difference:


<% 5.times do |t| %>
Hello or something else
<% end %>



It'll print Hello or something else five times in the html.


Hello or something else



Let's say, Your Post table in database contains some data. SO, we can do that like below:


<% @posts.each do |post| %>
<%= post.title %> # I assumed your post model contains a title attribute
<% end %>





Thank you for the detailed response, @Emu! Much appreciated! Now, I have to learn how to create a post!
– Emin
Aug 20 at 23:58







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ