xps15z
xps15z

Reputation: 1829

NoMethodError - undefined method path

I have a NoMethodError undefined method `avatar_path' that comes from my view on line <%= button_to('Set as Avatar', [:avatar, @photo])%>

I am assuming the problem is from the routes, though the changes I have made still did not fix the issue.

The photo it should be making a avatar is /uploads/photo/image/8/IMAG0090.jpg

Routes:

  resources :photos do
    member do
      post :avatar
    end
  end

Photos Controller:

  def new 
    @photo = Photo.new
  end

  def create
    @photo = Photo.new(params[:photo])
    @photo.user = current_user
    if @photo.save
      flash[:notice] = "Successfully created photos."
      redirect_to :back
    else
      render :action => 'new'
    end
  end



  def resize(width, height, gravity = 'Center')
    manipulate! do |img|
      img.combine_options do |cmd|
        cmd.resize "#{width}"
        if img[:width] < img[:height]
          cmd.gravity gravity
          cmd.background "rgba(255,255,255,0.0)"
          cmd.extent "#{width}x#{height}"
        end
      end
      img = yield(img) if block_given?
      img
    end
  end

  def edit
    @photo = Photo.find(params[:id])
  end

  def update
    @photo = Photo.find(params[:id])
    if @photo.update_attributes(paramas[:photo])
      flash[:notice] = "Successfully updated photo."
      redirect_to @photo.gallery
    else
      render :action => 'edit'
    end
  end

  def destroy
    @photo = Photo.find(params[:id])
    @photo.destroy
    flash[:notice] = "Successfully destroyed photo."
    redirect_to @photo.gallery
  end

  def avatar
    if current_user.update_attribute(:avatar_id, params[:id])
      flash[:notice] = "Successfully made Avatar."
        else
          flash[:notice] = "Avatar failed"
        end
        redirect_to root_url, notice: "Set as avatar!"
      end
end

View (users/show.html):

<div class="parent-container">
<% @user.photos.each do |photo| %>
    <%= link_to image_tag(photo.image_url(:thumb)), photo.image_url%>
    <%= button_to('Set as Avatar', [:avatar, @photo])%>
    <% end %>
    </div>

Upvotes: 0

Views: 1046

Answers (3)

Kirti Thorat
Kirti Thorat

Reputation: 53028

Update the users/show view as below:

   <div class="parent-container">
   <% @user.photos.each do |photo| %>  ## Block variable name is photo
      <%= link_to image_tag(photo.image_url(:thumb)), photo.image_url%>
      <%= button_to('Set as Avatar', [:avatar, photo])%>  ## photo not @photo
    <% end %>
    </div>

There is no @photo variable, It should be just photo as the Block variable name is photo.

Upvotes: 2

Mikhail Chuprynski
Mikhail Chuprynski

Reputation: 2493

you pass symbol :avatar instead of variable

button_to('Set as Avatar', [:avatar, @photo])

so if you pass variable, rails will take its id by itself.

button_to('Set as Avatar', [@avatar, @photo])

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51151

This should work:

<%= button_to('Set as Avatar', avatar_photo_path(@photo)) %>

Upvotes: 0

Related Questions