ruby on rails - Accessing Nested Attributes When Doing Error Checking In Controller -


i have ruby on rails 3.2.13 application have 1 model related several models. give information 1 of them related models explain scenario. have new , edit views use in maintenance screen. still pretty new working nested attributes. maintenance screen allows maintenance of row associated attributes. manually grab selected rows , write them out associated parent row. display them in list on top of collection_select drop down list box. doing recent checking after problems 500 errors noticed had software problems need correct.

i have error checking code in update method render edit after finding error. when attempts display edit view 500 error trying find array of 1 of nested attributes. here error got.

completed 500 internal server error in 24ms  actionview::template::error (undefined method `topic_id' {"topic_id"=>[""]}:activesupport::hashwithindifferentaccess):     56:     <p style="padding-left: 150px; color: #f8ee5f;"><%= display_topics @media_library %></p>     57: <% end %>     58: <%= fields_for :media_topics |media_topic| %>     59:   <%= media_topic.label :topic, "topics" %><%= media_topic.collection_select(:topic_id, topic.order("name_en"), :id, :name_en, {}, {multiple: true}) %>     60: <% end %><br>     61:      62: <% if @media_library.media_targets.count > 0 %>   app/views/media_libraries/_media_library_fields.html.erb:59:in `block in _app_views_media_libraries__media_library_fields_html_erb___4316101037373251257_70115028003400'   app/views/media_libraries/_media_library_fields.html.erb:58:in `_app_views_media_libraries__media_library_fields_html_erb___4316101037373251257_70115028003400'   app/views/media_libraries/edit.html.erb:7:in `block in _app_views_media_libraries_edit_html_erb__2448603971022600459_70115028733680'   app/views/media_libraries/edit.html.erb:6:in `_app_views_media_libraries_edit_html_erb__2448603971022600459_70115028733680'   app/controllers/media_libraries_controller.rb:115:in `update' 

here how have medialibrary model defined related mediatopic model. i'm including portions of models relate question.

class medialibrary < activerecord::base   attr_accessible :media_topics_attributes   has_many :media_topics, dependent: :destroy   accepts_nested_attributes_for :media_topics  class mediatopic < activerecord::base    belongs_to :media_library 

here edit method:

def edit   @media_library = medialibrary.find(params[:id])   @edit_page     = true end 

this statement grab array of selected media_topics collection_select topic model (where media_topics gets topic_id value related medialibrary model)

@media_topics  = params[:media_topics] 

here sample of error checking in update method.

case when params[:link].blank?         && params[:link_video].blank? && params[:link_pdf].blank? && params[:resource].blank? && params[:resource2].blank? && params[:resource3].blank?   flash.now[:error] = "you must enter @ least 1 link before creating media library item."   render 'edit' else   update logic here end 

here code edit view. display_topics helper displays list of related topic rows above drop down list of topics.

<% if @media_library.media_topics.count > 0 %>   <p style="padding-left: 150px; color: #f8ee5f;"><%= display_topics @media_library %></p> <% end %> <%= fields_for :media_topics |media_topic| %>   <%= media_topic.label :topic, "topics" %><%= media_topic.collection_select(:topic_id, topic.order("name_en"), :id, :name_en, {}, {multiple: true}) %> <% end %><br> 

here display_topics helper:

  def display_topics(media_library)      media_library_topics = mediatopic.where("media_library_id = ?", media_library.id).all     topic_list = nil;     if media_library_topics       = 1       media_library_topics.each |mltopic|         topic_row = topic.find(mltopic.topic_id)         case i18n.locale.to_s         when "fr"           topic_name = topic_row.name_fr         when "pt"           topic_name = topic_row.name_pt         when "es"           topic_name = topic_row.name_es         else           topic_name = topic_row.name_en         end         if == 1           if media_library_topics.size == 1              topic_list = "#{t :media_label_topic}: "+topic_name           else             topic_list = "#{t :media_label_topics}: "+topic_name           end         else             topic_list = topic_list+" | "+topic_name         end         += 1       end       "#{topic_list}"     end    end 

i not know of way work nested_attributes manually add them or delete/re-add them.

what not know when render 'edit' second time happens nested attributes form? seeing in error seems disappear, hence undefined method :topic_id.

i have done lot of searching off , on since developed function have not seen error mentioned. of examples remember not show situations nested attributes person rendering edit view back, first display records show errors.

how nested attributes view second time?

any appreciated.

i have been doing more research , noticed when using fields_for in form designated f.fields_for, not fields_for have them in application. changed view use f.fields_for , noticed selected options appearing in drop down list topic model. take care of problem having in other question. @ least no longer getting 500 error. rewrite code have updating mediatopic rows in controller since using media_topic_attributes in view.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -