2014-03-24 78 views
0

我有一个NoMethodError undefined method `avatar_path'是来自我的看法上线<%= button_to('Set as Avatar', [:avatar, @photo])%>NoMethodError - 未定义的方法路径

我假设的问题是从路线,虽然改变我已经做仍然没有解决问题。

应当制作头像的照片是/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 

视图(用户/ 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> 
+1

分享按钮呈现视图的动作。 –

+1

分享呈现这个视图的'controller action'?你分享的观点不是行动。 –

+0

@KirtiThorat我从来没有为照片控制器创建显示动作,因为我不认为需要设置阿凡达。 – xps15z

回答

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

+0

啊,谢谢你的新鲜眼睛。我在这里疯了。感谢您的指出! – xps15z

0

这应该工作:

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

我从类似的东西开始,但没有工作,这就是为什么我切换到我在这里发布的内容。它不适用于我的操作,这就是为什么我不能使用它。这将我发送到/ photos //头像,它应该设置照片ID并将我重定向回根网址。所以它消除了错误,但它也改变了我的代码,使它不与我的控制器连接。我添加了控制器到帖子,因为这可能有所帮助。 – xps15z

0

传递符号:化身,而不是变量

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

所以如果传递变量,轨道将自行承担其ID。

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

我之前做过这个,但是我把它与照片,头像相反。确切的格式发送给我一个错误,提供了无位置。无法建立URI.'正确的照片变量正在加载...'<照片ID:6,created_at:“2014-02-20 14:34:33”,updated_at:“2014-02-20 14:34 :33“,图片:”IMAG0334.jpg“,名称:”“,gallery_id:”“,user_id:1,头像:无>。我假设错误显示,因为它试图拉起@avatar,因为用户将试图从照片设置他们的头像应该不存在。 – xps15z

相关问题