macfan_
macfan_

Reputation: 13

"Template is missing" error using jQuery with Rails

I am trying to add avatars to user profile with a "add user avatar" link in edit account page.

this is avatars_controller.rb:

def new
  @avatar = Avatar.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @avatar }
    format.js
  end
end

def create
  @avatar = @user.avatars.create(params[:avatar])
  respond_to do |format|
    if @avatar.save
      format.html { redirect_to(edit_account_path, :notice => 'Avatar was successfully created.') }
      format.xml  { render :xml => @avatar, :status => :created, :location => @avatar }
      format.js
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @avatar.errors, :status => :unprocessable_entity }
      format.js
    end
  end
 end

this is my link:

<%= link_to "add a new avatar", new_avatar_path%>

routes:

resources :avatars
resources :users do
  resources :avatars
end

views/avatars/create.js.erb:

alert('whoaaa!!!')

i'm using rails 3.0.9 and getting:

Template is missing

Missing template avatars/new with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/ugur/rails_projects/deneme/app/views", "/home/ugur/rails_projects/deneme/app/views", "/home/ugur/rails_projects/deneme/flag_promotions/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree-0.60.1/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree_sample-0.60.1/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree_promo-0.60.1/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree_dash-0.60.1/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree_api-0.60.1/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree_auth-0.60.1/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/devise-1.3.3/app/views", "/home/ugur/.rvm/gems/ruby-1.9.2-p180/gems/spree_core-0.60.1/app/views"

i'm about to go crazy. please help.

Upvotes: 1

Views: 1373

Answers (1)

apneadiving
apneadiving

Reputation: 115521

You're calling the edit action and hoping the create template to be rendered. That's the point.

Given your output, I guess the edit action is rendering the new.js.erb template which doesn't exist.


Change:

<%= link_to "add a new avatar", new_avatar_path%>

with:

<%= link_to "add a new avatar", new_avatar_path, :remote => true %>

To actually make an ajax request.


Se my commit here: https://github.com/apneadiving/avatars/commit/f88ebf3f65e2ad88176cd28f09fd9dc91448cb98

It works at url /avatars

Upvotes: 2

Related Questions