1

显示数据库中的图像时出现问题 我正在一个网站上工作,该网站允许客户帐户用户上传他们的图片。

使用Agile Web Development with Rails一书中提供的配方,我设法使其适用于客户模块,其中我有两个表,客户图像,一对一关系。我用

<img src="<%= url_for(:action => 'picture', :id => @customer.id) %>" alt="Picture" class="photo"/>

生成以下 html 示例

<img src="/customers/picture/986" alt="Picture" class="photo">

现在,我的第二个模块稍微复杂一些,其中涉及 3 个表。
1. Account - 仅存储用户名和密码
2. Basic_profile - 存储诸如姓名、性别、生日等内容。注意 - 我在这里使用编辑页面来上传和显示用户的图片。
3.图片- 存储帐户用户图片。

这里的关系是Picture belongs_to Basic_profileBasic_profile belongs_to Account

在我的路线中,我有一个嵌套资源。

  resources :accounts do
    resource :basic_profile, :contact
  end

注意使路径为的奇异basic_profile

//localhost:3000/accounts/1/basic_profile/edit

上传和保存到数据库都很好。我现在的问题是在编辑页面中显示图片。以下代码不起作用。

<img src="<%= url_for(:action => 'picture', :id => @basic_profile.id) %>" alt="Picture" class="profile_picture" />

我试图解决这个问题,但没有任何效果。毫无意义地玩弄像这样的路径

//localhost:3000/accounts/1/basic_profile/picture

给我路由错误。

请帮忙。

4

1 回答 1

0

解决了!

解决方案很简单。在嵌套路由中添加“get :picture” 。

  resources :accounts do
    resource :basic_profile do
      get :picture
    end
  end
于 2013-05-11T07:53:08.580 回答