1

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.

1
  • Do you have a 'edit' method? You link to edit_avatar_path. Not to create_avatar_path Commented Aug 10, 2011 at 13:51

1 Answer 1

2

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

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

15 Comments

@macfan_: any comment? don't forget to accept an answer if it fits
sorry i posted wrong link. it is corrected. please have a look.
Ok but same conclusion: you call the new action so the partial should be new.js.erb
i aleady did it. i also used avatars/new.js.erb but no use.
i have both new.js.erb and create.js.erb
|

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.