ruby on rails - model validation errors not rendered with render :action => "index" -
i working on simple youtube list app user can add videos list. validating presence of video_id field.
class track < activerecord::base attr_accessible :title, :thumbnail_url, :video_id validates :video_id, :presence => true end
i have following create function defined in controller:
def create #fetches video info, stores in @trackinfo if is_url(params[:track][:query]) @trackinfo = gettrackinfo(params[:track][:query]) else @trackinfo = youtubequery(params[:track][:query]) end #use @trackinfo create track object @track = track.new(@trackinfo) @tracks = track.all @video_ids = track.pluck(:video_id) if @track.save else render :action=>"index" end
end
in index.erb.html have following block:
<%= render partial: "error_message" %>
the corresponding _error_message.erb.html contains error messages validation:
<% if @track.errors.any? %> <% @track.errors.full_messages.each |msg| %> <%= msg %><br> <% end %> <% end %>
the problem when validation fails, not able see error message rendered view. logged messages right before entered render index, , able see messages:
from controller:
if @track.save else puts "#{@track.errors.full_messages}" #i able see render :action=>"index" end
i dont know happens during render causing error messages not displayed, on server logs _error_messages.erb.html has been rendered, dont see happen page. feel have missed obvious. knows should do?
thanks
i think resolved issue, im not sure if fix proper. forgot mention on main index.erb.html have search bar , submit buttom embedded in ajax form calls create function inside controller
<%= form_tag(@track, :remote=>true, :method => :post, :class => 'new_track') %> <%= text_field_tag "track_query", nil, :placeholder => 'enter query or link', :id => "search_bar", :name => "track[query]", :size => 30, :required => true %> <%= submit_tag "add" , :name => 'commit'%> </p> <% end %>
i have error div in same page (i deleted render partial , stuck empty div populated in index.erb.html):
<div id = "error_message"> </div>
in file create.js.erb, added following lines:
<% elsif @track.errors.any? %> if($.trim($("#error_message").html())==''){ <% @track.errors.full_messages.each |msg| %> $('#error_message').hide().append('<%= msg %>'); <% end %> $('#error_message').fadein(); } $('#search_bar').val('');
it seems when remove render :action=>"index" create function in controller, error messages able displayed on index page. hoping not put processing on client javascript side , more on controller side. there way this? still wondering why render did not render partial html. because form ajax , wont render whole page? apologize if dont know im talking :)
Comments
Post a Comment