ruby-on-railsrubyruby-on-rails-3rjs

render :update do |page| is not working correctly with Rails3.X


I have the following in my routes.rb :

  match 'admins/approve' => 'admins#approve', :as => :admins_approve, :method => :post

In my admins_controller.rb I have :

def approve
  render :update do |page|
      page << "alert(" + params[:note] + ");"  
  end           
end

in my admins/index.html.erb :

<% @users.each do |user| %>
  <tr>
    <td class= "action_add_note">
        <%= form_tag admins_approve_path, :id => "form_note_#{user.id}", :remote=> true  do %> 
          <%= text_area_tag 'note' %>
        <% end %>
        <%= link_to_function content_tag(:i, "", :class => 'icon-ok' ), 
                  "$('#form_note_#{user.id}').submit();", :class => "btn"%>
    </td>
  </tr>
<% end %>

I know I can use button instead of function but it is not the case here , anyways whenever I submit it I get wrong path somehow and the following error :

ActionView::MissingTemplate (Missing template admins/update, application/update with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/Users/Apple/code/rails_pp/app/views"
* "/Users/Apple/.rvm/gems/ruby-1.9.2-p180/gems/twitter-bootstrap-rails-2.1.1/app/views"
app/controllers/admins_controller.rb:11:in `approve'

Rendered /Users/Apple/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.6ms)

Solution

  • Take out the link_to_function and add this to your form_tag

    <%= text_area_tag 'note' %>
    <%= submit_tag "Submit %>
     <% end %>
    

    In your admin controller

    def approve
      @note = params[:note]
      current_user.update_attribute(:note, @note)
      respond_to do |format|
        format.js
      end
    end
    

    Then create an approve.js.erb file

     alert("<%= j(@note) %>");