This could possibly be a really stupid question, but I have a home page
which calls a partial to show the latest entry in a table called ‘news’.
All I want to know is how to put the ‘news.content’ into a text box.
Partial is as follows:
Title:
<%= news.title %>
Details:
<%= news.content %>
The bit that calls this on my home page is as such:
Do you mean that what you have works, but you want it in a text field
instead of a paragraph? If so then one has to ask the question why?
One would normally use a text_field inside a form.
This could possibly be a really stupid question, but I have a home page
which calls a partial to show the latest entry in a table called ‘news’.
All I want to know is how to put the ‘news.content’ into a text box.
Partial is as follows:
Title:
<%= news.title %>
Details:
<%= news.content %>
The bit that calls this on my home page is as such:
Can I make it just shows a blank entry if there is no record?
Just test for News.last nil, either in the render call or inside the
partial (inside the partial you would test for news nil of course.
The fact that you needed to ask this makes me think you would benefit
from working right through a good tutorial such as railstutorial.org
(which is free to use online), including doing all the exercises. A
few days spent doing that would very soon be recovered.
Colin
Hi Colin
Thanks for that, but could you be a bit more specific please? If all I
need is a couple of words in a single line of code it would be better
for someone to just tell me exactly what is required.
I’ve worked through quite a few tutorials and have learned a lot from
them, but when wee things like this crop up I just want an answer
straight away instead of working my way through a tutorial on the off
chance that it may mention the thing I want to know about.
Can I make it just shows a blank entry if there is no record?
Hi Colin
Thanks for that, but could you be a bit more specific please? If all I
need is a couple of words in a single line of code it would be better
for someone to just tell me exactly what is required.
I’ve worked through quite a few tutorials and have learned a lot from
them, but when wee things like this crop up I just want an answer
straight away instead of working my way through a tutorial on the off
chance that it may mention the thing I want to know about.
Are you saying you have worked through tutorials but don’t know how to
test for news being nil or not nil?
By the way, I think that having a model called news is probably not a
good idea. Is that singular or plural? If singular then what is the
plural, and vice versa? It is best to choose model names that can be
easily interpreted as singular or plural as it makes the code much
easier to understand. Possibly NewsItem for example. Then a variable
holding one item would be news_item, and for an array it would be
news_items.
Another, is that you do similar test in the partial itself
And, Colin is entirely correct that you should rename your models(.rb)
to
singular, while table names are plural on your database. Then model
class
name should be singular as well, however proper case, replacing event of
“" in model name with ommitted "” and then first letter capitalized.
Another consideration is that primary keys should be ‘id’, while foreign
keys should be ‘lower-case_singular model name’_id. This will make a
huge
difference if you attempt to use table associations.
Can I make it just shows a blank entry if there is no record?
Just test for News.last nil, either in the render call or inside the
partial (inside the partial you would test for news nil of course.
The fact that you needed to ask this makes me think you would benefit
from working right through a good tutorial such as railstutorial.org
(which is free to use online), including doing all the exercises. A
few days spent doing that would very soon be recovered.