Show in text box

50 views
Skip to first unread message

Euan Leslie

unread,
Jun 4, 2015, 6:11:30 AM6/4/15
to rubyonra...@googlegroups.com
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:

<p>
<strong>Title:</strong>
<%= news.title %>

</p>

<p>
<strong>Details:</strong>
<%= news.content %>
</p>


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

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

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jun 4, 2015, 6:22:14 AM6/4/15
to rubyonra...@googlegroups.com
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

Euan Leslie

unread,
Jun 4, 2015, 6:32:50 AM6/4/15
to rubyonra...@googlegroups.com
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 Law

unread,
Jun 4, 2015, 8:31:45 AM6/4/15
to rubyonra...@googlegroups.com
On 4 June 2015 at 11:32, Euan Leslie <li...@ruby-forum.com> 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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/80443a1c6618ae0a5525192604135755%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Euan Leslie

unread,
Jun 4, 2015, 11:09:07 AM6/4/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1174557:
> On 4 June 2015 at 11:32, Euan Leslie <li...@ruby-forum.com> 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.

Colin Law

unread,
Jun 4, 2015, 12:26:09 PM6/4/15
to rubyonra...@googlegroups.com
Glad to be of help.

Cheers

Colin

Euan Leslie

unread,
Jun 8, 2015, 12:03:33 PM6/8/15
to rubyonra...@googlegroups.com
Euan Leslie wrote in post #1174549:
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 Law

unread,
Jun 8, 2015, 12:13:37 PM6/8/15
to rubyonra...@googlegroups.com
On 8 June 2015 at 17:02, Euan Leslie <li...@ruby-forum.com> 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

Euan Leslie

unread,
Jun 9, 2015, 3:56:29 AM6/9/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1174741:
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.

Colin Law

unread,
Jun 9, 2015, 4:12:37 AM6/9/15
to rubyonra...@googlegroups.com
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

Liz McGurty

unread,
Jun 9, 2015, 9:28:23 AM6/9/15
to rubyonra...@googlegroups.com
One possibility:
<% test_news_last  = News.last %>
<% unless test_news_last .blank? %>

<%= render partial: 'news/news', locals: { news: News.last } %>.
<% else %>
<div>Sorry but not results were found.</div>   #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. 

Elizabeth McGurty

unread,
Jun 9, 2015, 10:04:05 AM6/9/15
to rubyonra...@googlegroups.com
correction....
<%= render partial: 'news/news', locals: { news: test_news_last } %>.
Reply all
Reply to author
Forward
0 new messages