使用ISBN更改用户的目的地

时间:2013-12-05 07:19:14

标签: ruby-on-rails ruby-on-rails-3 amazon-ec2

我想让我的用户只为每本书创建一个页面(基于ISBN)。但我不知道如何实现这一点。你能给我一些建议吗?

现在,在我的系统中,首先,用户使用关键字(如书名)搜索书籍。 索引页面显示搜索结果(亚马逊API)。

由于我在每本书(搜索结果)之外设置了“注册按钮”,因此用户可以为书籍创建页面并与页面中的其他用户进行通信。

☆index.html(索引控制器)

<% if @items.present? %>
  <ul style="list-style: none;">
   <% @items.each do |i| %>
     <li style="clear: both;">
      <div style="width: 150px;float: left;text-align:center;">
        <%= link_to i.get('DetailPageURL') do %>
          <%= image_tag i.get('SmallImage/URL'), {:style => 'border: none;'} %>
        <% end %>
      </div>
      <div style="width: 150px; float: left;"><%= i.get('ItemAttributes/Title') %><br/>
        <%= i.get('ItemAttributes/Author') %><br/><%= i.get('ItemAttributes/PublicationDate')%>
      </div>
      <div style="width: 150px; float: left;">|<%= link_to 'Register', {:controller => 'groups', :action => 'new', :name => i.get('ItemAttributes/Title'),:author => i.get('ItemAttributes/Author'), :publish => i.get('ItemAttributes/Publisher'), :published => i.get('ItemAttributes/PublicationDate'), :isbn => i.get('ItemAttributes/ISBN'), :page => i.get('ItemAttributes/NumberOfPages'), :imageurl=>i.get('MediumImage/URL')}%>|</div>
      </li>
    <% end %> 
   </ul>
  <% else %>
 見つかりませんでした。
 <% end %>

☆index_controller

def index
@keyword = params[:keyword]
 if @keyword.present?
  Amazon::Ecs.debug = true
  res = Amazon::Ecs.item_search(params[:keyword], 
      :search_index => 'All', :response_group => 'Medium')
  @items = res.items
 end
end

但正如我所说,我不希望用户为一本书制作多个页面。一本书(ISBN)有一页。因此,我希望用户在搜索遇到现有页面时移动到现有页面。否则,将允许用户在new.html.erb(Groups)上创建新页面。

如何实现这一目标?当用户按下我系统上没有现有页面的书旁边的注册按钮时,我希望用户转到new.html.erb。另一方面,当用户按下我系统上现有页面的书旁边的注册按钮时,我希望用户移动到现有的书页。我想利用ISBN。

☆new.html.erb(组)

<%= form_for(@group) do |f| %>
  <% if @group.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@group.errors.count, "error") %> prohibited this group from being saved:</h2>

      <ul>
      <% @group.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

 <%= image_tag @group.imageurl %>
<p></p>

<table class="table">
<thead>
 <tr>
   <th>著者</th>
   <th>出版年</th>
   <th>出版社</th>
   <th>ページ数</th>
   <th>ISBN</th>
 </tr>
 </thead>
 <tbody>
 <tr>
  <td><%= @group.author %></td>
  <td><%= @group.published %></td>
  <td><%= @group.publish %></td>
  <td><%= @group.page %></td>
  <td><%= @group.isbn %></td>
 </tr>
 </tbody>
</table>

  <div class="field">
    <%#= f.label :name %><br />
    <%#= f.text_field :name %>
  </div>
  <div class="field">
    <%#= f.label :memo %><br />
    <%#= f.text_area :memo ,:class=>"span6", :size=>"5x8"%>
  </div>

  <div class="actions">
    <%= f.submit value: 'この本のページをつくる'%>
  </div>
<% end %>

☆Groups_controller

def new
  @group = Group.new
  @group.imageurl = params[:imageurl]
  @group.name = params[:name]
  @group.author = params[:author]
  @group.publish = params[:publish]
  @group.published = params[:published]
  @group.isbn = params[:isbn]
  @group.page = params[:page]
  @group.save

 respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @group }
 end
end

1 个答案:

答案 0 :(得分:1)

更新:items.map现在应该可以使用Amazon Search API结果项。

似乎在您设置@items的搜索索引控制器中,您还需要发送一个数组,该数组是该搜索结果已存在的组的isbns的子集。

因此,您的搜索索引控制器将包含类似

的内容
search_isbns = @items.map{|i| i.get('ItemAttributes/ISBN')}
# the WHERE clause passes an array of ISBN values, which will 
# generate a WHERE isbn IN [isnb values] query
@existing_groups_isbns = Group.select(:isbn).where(:isbn => search_isbns).map(&:isbn)

在搜索索引视图代码中,将“注册”链接展开为

<% if @existing_groups_isbns.include? i.get('ItemAttributes/ISBN') %>
   # render link_to existing page
<% else %>
   # render link_to register
<% end %>