如何在我的应用中显示Gravatar图像?

时间:2011-08-25 01:51:14

标签: ruby-on-rails authentication routing gravatar

我正在关注此视频直播并立即陷入困境:http://asciicasts.com/episodes/244-gravatar 每当我尝试编辑index.html文件时,我都会从服务器获得此响应: undefined局部变量或方法`user'。 从它的外观来看,它似乎不应该太难。我只需要在这里和那里交换几行,但我很难过。

这就是我在index.html.erb中所拥有的:

     <h1>Listing posts</h1>

    <% @posts.each do |post| %>
      <tr>
        <td><%= post.name %></td>
        <td><%= post.title %></td>
        <td><%= post.content %></td>
        <td><%= link_to 'Show', post %></td>
        <td><%= link_to 'Edit', edit_post_path(post) %></td>
        <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

我想将其更改为:

  <% for user in @users %>  
      <tr>  
        <td><%= image_tag avatar_url(user) %></td>  
        <td><%= user.email %></td>  
        <td><%= link_to "Show", user %></td>  
        <td><%= link_to "Edit", edit_user_path(user) %></td>  
        <td><%= link_to "Destroy", user, :confirm => 'Are you ↵  
          sure?', :method => :delete %></td>  
      </tr>  
    <% end %>  

我的应用程序帮助程序代码:

module ApplicationHelper  
  def avatar_url(user)  
    gravatar_id = Digest::MD5::hexdigest(user.email).downcase  
    "http://gravatar.com/avatar/#{gravatar_id}.png"  
  end  
end  

我的帖子控制器代码:

 class PostsController < ApplicationController
  # GET /posts
  # GET /posts.xml

  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.xml
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.xml
  def new
    @post = Post.new

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

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.xml
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.xml
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.xml
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end

3 个答案:

答案 0 :(得分:0)

$ rails g scaffold user email:string

在应用程序的根文件夹中的控制台中运行它,然后在app / views / users / index.html.erb中进行必要的更改

答案 1 :(得分:0)

在我使用Devise的情况下,我跟着同样的事情而陷入困境,因为它会给我带来错误。所以我换了;

模块ApplicationHelper   def avatar_url(用户)
    gravatar_id = Digest :: MD5 :: hexdigest(user.email).downcase
    “http://gravatar.com/avatar/#{gravatar_id}.png”
  结束 端

用;

模块ApplicationHelper   def avatar_url(用户)
    gravatar_id = Digest :: MD5 :: hexdigest(current_user.email).downcase
    “http://gravatar.com/avatar/#{gravatar_id}.png”
  结束 端

我也注意到了;

除非你将“用户”大写,否则不会工作;

我对此非常新鲜,但在这之后一切都在我的应用程序中运行。希望这会有所帮助。

答案 2 :(得分:0)

null

通过复数视觉学习