Show in text box

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:

<%= render partial: ‘news/news’, locals: { news: News.last } %>.

On 4 June 2015 at 11:10, Euan L. [email protected] wrote:

Details: <%= news.content %>

The bit that calls this on my home page is as such:

<%= render partial: ‘news/news’, locals: { news: News.last } %>.

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.

Colin

Yes, it works, I just think it would stand out better if it was enclosed
in a text box that’s all. Is there a way?

Colin L. wrote in post #1174557:

On 4 June 2015 at 11:32, Euan L. [email protected] wrote:

Yes, it works, I just think it would stand out better if it was enclosed
in a text box that’s all. Is there a way?

Please remember to quote the previous post when replying, otherwise
no-one know what you are saying ‘yes’ to.

If you just want to draw a box round the text then I think a primer on
html and css is in order.

Colin

Thanks, that was a lot easier than I was making it.

On 4 June 2015 at 11:32, Euan L. [email protected] wrote:

Yes, it works, I just think it would stand out better if it was enclosed
in a text box that’s all. Is there a way?

Please remember to quote the previous post when replying, otherwise
no-one know what you are saying ‘yes’ to.

If you just want to draw a box round the text then I think a primer on
html and css is in order.

Colin

On 4 June 2015 at 16:08, Euan L. [email protected] wrote:

Colin

Thanks, that was a lot easier than I was making it.

Glad to be of help.

Cheers

Colin

Euan L. wrote in post #1174549:

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:

<%= render partial: ‘news/news’, locals: { news: News.last } %>.

Hi again

I’ve found another problem with this. It produces an error if there are
no records in the table.

The line in my Index view:

<%= render partial: ‘news/news’, locals: { news: News.last } %>

Can I make it just shows a blank entry if there is no record?

Colin L. wrote in post #1174741:

On 8 June 2015 at 17:02, Euan L. [email protected] wrote:


I’ve found another problem with this. It produces an error if there are
no records in the table.

The line in my Index view:

<%= render partial: ‘news/news’, locals: { news: News.last } %>

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.

On 9 June 2015 at 08:55, Euan L. [email protected] wrote:

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.

Colin

One possibility:
<% test_news_last = News.last %>
<% unless test_news_last .blank? %>
<%= render partial: ‘news/news’, locals: { news: News.last } %>.
<% else %>

Sorry but not results were found.
#However you want to display message <% end %>

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.

On 8 June 2015 at 17:02, Euan L. [email protected] wrote:


I’ve found another problem with this. It produces an error if there are
no records in the table.

The line in my Index view:

<%= render partial: ‘news/news’, locals: { news: News.last } %>

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

correction…
<%= render partial: ‘news/news’, locals: { news: test_news_last } %>.