2

I am working on a simple youtube list app where the user can add videos to the list. I am validating for the presence of the video_id field.

class Track < ActiveRecord::Base
  attr_accessible :title, :thumbnail_url, :video_id
  validates :video_id, :presence => true
end

i have the following create function defined in my controller:

def create

#fetches the video info, stores it in @trackinfo
if is_url(params[:track][:query])   
    @trackinfo = getTrackInfo(params[:track][:query])
else
    @trackinfo = youTubeQuery(params[:track][:query])

end
#use @trackinfo to 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 my index.erb.html i have the following block:

<%= render partial: "error_message" %>

the corresponding _error_message.erb.html just contains the error messages from the validation:

    <% if @track.errors.any? %>
        <% @track.errors.full_messages.each do |msg| %>
            <%= msg %><br>
        <% end %>
    <% end %>

the problem is when the validation fails, i am not able to see the error message from the rendered view. I logged the messages right before it entered the render index, and was able to see the messages:

from the controller:

if @track.save

else
    puts "#{@track.errors.full_messages}" #i am able to see this
    render :action=>"index"
end

i dont know what happens during the render causing the error messages not to be displayed, on the server logs it does say that the _error_messages.erb.html has been rendered, but i just dont see anything happen to the page. I feel like i have missed something really obvious. anyone knows what i should do?

Thanks

1
  • how your index action looks like? Commented Aug 10, 2013 at 7:09

1 Answer 1

1

i think i resolved this issue, but im not sure if my fix is proper. I forgot to mention that on the main index.erb.html i have a search bar and a submit buttom embedded in an ajax form that calls the create function inside the controller

<%= form_tag(@track, :remote=>true, :method => :post,  :class => 'new_track') do %>
    <%= text_field_tag "track_query", nil, :placeholder => 'enter a query or a link', :id => "search_bar", :name => "track[query]", :size => 30, :required => true %>
    <%= submit_tag "add" , :name => 'commit'%>
</p>
<% end %>

i also now have the error div in the same page (I deleted the render partial and just stuck an empty div to be populated in the index.erb.html):

<div id = "error_message">
</div>

in the file create.js.erb, I added the following lines:

<% elsif @track.errors.any? %>
if($.trim($("#error_message").html())==''){
    <% @track.errors.full_messages.each do |msg| %>
        $('#error_message').hide().append('<%= msg %>');
    <% end %>

        $('#error_message').fadeIn();
}
$('#search_bar').val('');

it seems that when i remove the render :action=>"index" completely from my create function in the controller, the error messages were able to be displayed on the index page. I was hoping to not put so much processing on the client javascript side and more on the controller side. Is there another way to do this? Still wondering why the render did not render that partial html. is it because the form is ajax and wont render the whole page? i apologize if i dont exactly know what im talking about :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.