NoMethodError - 未定义的方法路径

时间:2014-03-24 18:42:32

标签: ruby-on-rails

我有一个NoMethodError undefined method `avatar_path'来自我在<%= button_to('Set as Avatar', [:avatar, @photo])%>

行的观点

我假设问题来自路线,虽然我所做的更改仍未解决问题。

应该制作头像的照片是/uploads/photo/image/8/IMAG0090.jpg

路线:

  resources :photos do
    member do
      post :avatar
    end
  end

照片控制器:

  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

查看(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>

3 个答案:

答案 0 :(得分:2)

更新users/show视图,如下所示:

   <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>

没有@photo变量,它应该只是photo,因为块变量名称是photo

答案 1 :(得分:0)

这应该有效:

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

答案 2 :(得分:0)

你传递了符号:avatar而不是变量

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

因此,如果您传递变量,则rails将自行获取其ID。

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