On 9 June 2015 at 08:55, Euan Leslie <li...@ruby-forum.com> wrote: > Colin Law wrote in post #1174741: >> 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 > > 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 -- 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-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs1vgPZqO184X_gXjyRVNLcGw2iLX7UPxBRHv_kg9%3D3Jw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.