How the Embeded ruby code produces the HTML? [closed] - ruby-on-rails-3

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
How the above code Produces the HTML like below ? Please Explain it.
<div class="alert alert-success">Welcome to the Sample App!</div>

Embedded ruby code does not produces HTMl. It just o/p string which gets embedded into the HTML. Anything within:
<%= "text" %>
gets into the output.
In your case
<div class="alert alert-<%= key %>"><%= value %></div>
key variable is a string "success"
value variable is a string "Welcome to the Sample App!"
So, value of key variable get substituted with "success" and so on..

Related

RoR: why arent some of my microposts columns showing?

Here is the relevant part of the micropost view. .
<li>
<span class="content"><%= micropost.content %></span>
<span class="product"><%= micropost.product %></span>
<span class="price"><%= micropost.price %></span>
<span class="location"><%= micropost.location %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
I recently added product, price, and location to the columns in the micropost table. I checked in the database and these aspects are being recorded/written but for some reason content is the only one that gets displayed on the app. Why are the other ones left out?
Here is the generated HTML. Im assuming that this is the relevant section. as you can see the html for product, price and location is never generated.
<li id="1">
<img alt="alex" class="gravatar" src="http://gravatar.com/avatar/a6759dcef8e5aad4e77509e5479b9823.png?s=50" />
<span class="user">
alex
</span>
<span class="content">first</span>
<span class="timestamp">
Posted less than a minute ago.
</span>
delete
</li>
I dont think its the difference between attr_accessor and attr_accessible because content is attr_accessor and it shows up. also, I read the difference and it doesnt seem to be relevant here. I believe attr_accessible is the right one. plus I tried changing it to attr_accessor and it didnt work. Any other ideas? please and thank you!
Does the micropost that you are trying to see (ID = 1) have data populated on those fields (:product, :price, :location) ?
Since the generated HTML doesn't even have the span tags for those fields, the issue seems to be that Rails isn't loading the newest version of your view file. Are you running in production or in development? Have you re-started the Rails server?
Also, you are correct that attr_accessible is correct rather than attr_accessor if those are database columns.
You might also check if you have any kind of caching enabled that would be displaying a cached copy of the generated HTML. For example, you could change the content value in the database and see if that is reflected in the generated HTML.

How to display text in erb file?

I started rails a week ago, I want to know that how to display an unmodifiable text using erb file,
<%= f.label :email %>
<%= f.text_field :email %>
Here, instead of a text-box, I need a simple text displaying email id.
Any clues?
By "unmodifiable text" in a text field, I'm assuming you don't just want to have the email displayed on screen (if so, then Yuri Barbashov is correct) but also want it inside an input text field, but be unmodifiable.
In that case, you might be best off using javascript. Have a look at this StackOverflow answer for some JQuery examples.
Edit:
Actually, the solution here may be easier if you never plan on re-enabling the text field:
<%= f.text_field :email, disabled: true %>
<%= #your_model.email %>
But to be more precise you have to show your view code

Rails3 form_for hidden_field undefined method 'merge'

My attempt to place a hidden_field within a form_for is crashing within cucumber on an ActionView helper error. Something also about FixNum which escapes me since I haven't dug through the source code. My prices_controller shows this:
#price = Price.new
#commodity = Commodity.find(params[:id])
I want to make the link between price and commodity with this hidden_field:
<%= form_for (#price), :url => prices_path do |f| %>
<% f.hidden_field :commodity_id, #commodity.id %>
.
.
<div class="actions">
<%= f.submit "Submit" %>
</div>
Looked at the form_for api and the above should work. Reading other replies on stackoveflow, I have put the hidden_field in its own div within the form, added a Hidden_field_tag, and placed it within the actions div before the submit line. Looking at the merge msg, I guess it doesn't like something about the line, but it appears OK to me. The commodity_id field is the match field, sam
If you could paste the error message itself, and the relevant lines of the trace, it could help us. Right now, the only thing I see is that the ERB tag before f.hidden_field should be <%=, and I'm not sure about it since I don't use ERB. For what it's worth, merge is usually used with Hash objects. Maybe it can point you in the right direction
EDIT Ok I get it. You have to write f.hidden_field :commodity_id, :value => #commodity.id.

tiny mce display tags in rails 3

I am trying to store content of tinyMCE into "detail" coloumn.
Now when I display the content it displays wit all the <p> tags <i> tags etc.
This Is a security feature in rails3 .
But I don't want the <p> tags to be displayed , I want it to be rendered as HTML.
One way I found was <%= something.detail.html_safe %>
the other way I thought was to create a function in model like
def detail_safe
return self.detail.html_safe
end
and display using <%= something.detail_safe %>
Either ways I need to change the <%= %> tag at many places. Is there an easier solution? Or should I manually change at every place?
Thank you.
In the model:
def detail
self[:detail].html_safe if self[:detail]
end
Please note that you will always get html_safe output in this case when you do model_object.detail.
Not matter how you do it, you will have to change all of your <%= %>.
Your options are:
<%= something.detail_safe %>
<%= something.detail.html_safe %>
<%= raw something.detail %>
The only other option I can think of is turning off XSS protection - but don't do that!

Allowing a Post to show datetime without user input (Ruby on Rails)

I want to be able to show a datetime stamp ( I want it to appear like - "Posted 5 days ago" ), but i dont want the date field to appear when an user is making a post.
I just want to post to add it automatically. Any help? (Im just learning Rails so its all new to me...)
Thanks in advance.
For a Forum Application I did this:
<td class="right">
<% if forum.most_recent_post %><%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %> ago by <%= link_to forum.most_recent_post.user.login, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %> no posts <% end %>
</td>
Here <%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %> ago by "username" so, you may try this. I think that can work in this scenario too. Just use distance_of_time_in_words_to_now post.time here post.time is the time when user made a post.