Ruby on Rails each iteration error when one record -
I feel stuck on what should be a very simple solution, but I think whatever works Can not even get it! I'm changing the result from a table on the index page (which gives the list of states) it works without problems when multiple records come back However, when a single record is returned, I get an error : 'Undefined' method for # 'every'
I know that this is causing the error, when only a record is returned, but I can not find any documents how to solve it , Especially in case that 1 or 0 records can return.
Code page index:
@states = State.find (params [: id]) Code page index: / P>
& lt; Ul & gt; & Lt;% @ states.each do State | & Gt%; & Lt; Li & gt; & Lt;% = state.state_name% & gt; & Lt; / Li & gt; & Lt;% end% & gt; & Lt; / Ul & gt;
Because you are using , when you Paramams have many IDs, many record matches;
To ensure that every time, you get the ActiveRecord :: Relation object which you can call each , turn on Change the controller code to:
@states = State.where (id: params [: id]) # index action You have stated that this The index is visible, so the above change should solve your problem.
If this is the show view, then you need to to stick with and display only a state Change your view for
Comments
Post a Comment