开发者

How to get a Gravatar image appear on my app?

开发者 https://www.devze.com 2023-03-30 07:15 出处:网络
I\'m following this railscast and got stuck immediately: http://asciicasts.com/episodes/244-gravatar Whenever I try to edit the index.html file I get this response from the server:

I'm following this railscast and got stuck immediately: http://asciicasts.com/episodes/244-gravatar Whenever I try to edit the index.html file I get this response from the server: undefined local variable or method `user'. By the looks of it, it shouldn't seem to开发者_如何学编程o difficult. I just need to swap a few lines here and there, but I am having a tough time.

This is what I have in 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>

I would like to change it to this:

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

My application helper code:

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

My post controller code:

 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


$ rails g scaffold user email:string

run it in console in the root folder of your application, then make needed changes in app/views/users/index.html.erb


In my case using Devise, I followed the same thing and got stuck as it would give me errors. So I replaced;

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

with;

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

I've also noticed that using;

wont work unless you capitalize the "User" looking like this;

I'm very fresh and new to this but after doing that everything works in my app. Hope this helps.


class Question < ApplicationRecord
  def gravatar
    "http://www.gravatar.com/avatar/#{Digest.MD5.hexdigest(email)}"
  end
end

Learnt while going through pluralsight

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号