Topic: Inline RJS

This code was working before I created the update_link_list method in
the helper below (everything was in the controller). I can successfully
create a new link but I get a TypeError when I leave all form fields
blank -- I expect to see validation error messages.

#link_controller.rb:
  def create
    link = Link.new(params[:link])
    saved = link.save
    num_links = Link.find(:all).size
    num_cats = Category.find(:all).size
    if request.xhr?
      render:update do |page| page.update_link_list(link, saved,
num_links, num_cats) end
      return
    end
    if saved
      flash[:message] = 'Link was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

#link_controller helper:
  def update_link_list(new_link, was_saved, num_links, num_cats)
      if was_saved
        page['listing-status'].replace_html "You've created #{num_links}
links in #{num_cats} categories"
        page['no-links'].remove if num_links == 1 && page['no-links']
        page.insert_html :after, "category-#{new_link.category_id}",
:partial => 'link', :object => new_link
        page.visual_effect :highlight, "link-#{new_link.id}"
        page.replace_html 'new_link_error', ''
        page['link_url'].value = 'http://'
        page['link_name'].value = ''
        page['link_description'].value = ''
      else # not saved, present error messages
        page.replace_html 'new_link_error', error_messages_for(new_link)
        page.visual_effect :appear, 'new_link_error', :duration => 0.5
        page.visual_effect :highlight, 'new_link_error', :duration =>
0.5
      end
  end

I'm receiving this error:
TypeError in Publish/linksController#create
ActionView::Helpers::JavaScriptProxy#to_str should return String

No clue how to fix this. Any ideas? Many thanks...

Re: Inline RJS

Not sure if this is what is causing the problem or not, but "error_messages_for" expects to be passed the name of an instance variable, not the model object itself. Try setting it to an instance variable then passing the name of that instance variable (as a string or symbol) to error_messages_for.

You may want to try moving the code into a create.rjs file rather than a helper method.

Railscasts - Free Ruby on Rails Screencasts