0

我正在关注这个 railscast 并立即卡住:http ://asciicasts.com/episodes/244-gravatar 每当我尝试编辑 index.html 文件时,我都会从服务器收到此响应:未定义的局部变量或方法“用户” . 从表面上看,应该不会太难。我只需要在这里和那里交换几行,但我很难过。

这就是我在 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
4

3 回答 3

0
$ rails g scaffold user email:string

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

于 2011-08-25T02:24:54.477 回答
0

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

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

和;

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

我也注意到使用;

除非您将“用户”大写,否则将无法正常工作;

我对此非常陌生,但是在这样做之后,一切都在我的应用程序中运行。希望这可以帮助。

于 2013-01-08T20:01:10.537 回答
0
class Question < ApplicationRecord
  def gravatar
    "http://www.gravatar.com/avatar/#{Digest.MD5.hexdigest(email)}"
  end
end

在通过复数时学到的

于 2017-09-25T13:56:12.350 回答