订阅会员可以解锁未发行的图书

时间:2014-05-13 21:46:42

标签: ruby ruby-on-rails-3 models has-many-through subscription

我创建了一个应用程序,注册会员可以访问我们的图书馆。如果Book的release_date大于今天的日期。会员有能力购买未发行的书籍和解锁它(早鸟特别)。

但如果尚未购买未发行的书籍,我想将其显示为即将推出。

我无法创建方法/视图,以帮助我显示常规图书,未发布的图书,以及将书籍购买到current_user。

我的代码在下面,我们将非常感谢任何帮助。

视图

<% if current_user.member? %>
  <% @books.each do |book| %>

    <% if book.unreleased_book %>

      ###I am having trouble getting this conditional or loop to work
      <% if current_user has purchased the book %>
         LIST ALL PURCHASED BOOKS
      <% end %>
      <% if current_user has not purchased the book %>
         LIST ALL UNRELEASED BOOKS
      <% end %>

    <% elsif book.release_date <= Date.today %>
         LIST ALL AVAILABLE BOOKS
    <% end %>

<% else %>
  DISPLAY ALL BOOKS AS STATIC
<% end %>

模型

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :member

    has_many :books, through: :orders
    has_many :orders, :dependent => :destroy

end

class Order < ActiveRecord::Base
  attr_accessible :book_id, :order_date, :user_id

  belongs_to :user
  belongs_to :book

end

class Book < ActiveRecord::Base
  attr_accessible :description, :pages, :title, :available_in, :price, :release_date

  has_many :orders
  has_many :users, through: :orders

  ###Definition of unreleased books
  def unreleased_book
    Date.today < self.release_date
  end

end

CONTROLLER

class BooksController < ApplicationController

def index
  @books = Book.all
    @orders = current_user.orders
    respond_to do |format|
      format.html
      format.json  { render :json => @issues }
    end
  end
end

SCHEMA

create_table "users", :force => true do |t|
  t.string   "name"
  t.string   "email"
  t.boolean  "member",             :default => false ###Becomes Member on SignUp
  t.datetime "created_at",         :null => false
  t.datetime "updated_at",         :null => false
end

create_table "orders", :force => true do |t|    ###Created when Purchased a BOOK
  t.integer  "user_id"
  t.integer  "book_id"
  t.date     "order_date"
  t.datetime "created_at",            :null => false
  t.datetime "updated_at",            :null => false
end

create_table "books", :force => true do |t|
  t.string   "title"
  t.string   "pages"  
  t.text     "description"
  t.decimal  "price",              :precision => 8, :scale => 2
  t.date     "release_date"   ###If Release_Date > Time.Now then Book is Unreleased
  t.datetime "created_at",         :null => false
  t.datetime "updated_at",
end

1 个答案:

答案 0 :(得分:1)

我认为你的模特关系有点偏。这是我的建议:

class User < ActiveRecord::Base
   attr_accessible :name, :email, :password, :member

  has_many :orders # a user typically can have many orders
end

class Order < ActiveRecord::Base
   attr_accessible :book_id, :order_date, :user_id

   belongs_to :user #each order belongs to a single user
   has_many :books # one order can have consist of many books

end


 class Book < ActiveRecord::Base
   attr_accessible :description, :pages, :title, :available_in, :price, :release_date

   belongs_to :order 
   has_many :users, through: order # since a book belongs to an order and an order belongs to a user, a book has many users goes the logic

 end

将视图中的逻辑移动到帮助中。考虑使用case expression。这个助手会给你一个大纲。

module BooksHelper 
  ActionView::Helpers::UrlHelper #this will give access to Url helpers 

  def purchases(book)
    case book
    when book.unreleased_book
      link_to path_to_new_action #configure this path depending on how you set up your routes. 
    when  current_user has purchased the book
      #LIST ALL PURCHASED BOOKS
    when current_user has_not purchased the book
      #LIST ALL UNRELEASED BOOKS
    when book.release_date <= Date.today
      #LIST ALL AVAILABLE BOOKS
    else
      #DISPLAY ALL BOOKS AS STATIC
    end
end

然后在你的VIEWS中,像这样渲染帮助器。

<% if current_user.member? %>
 <% @books.each do |book| %>
    <%= purchases(book) %>
 <% end %>
   

这样可以保持您的观点清洁。

books controller

因为您更改了模型关联,所以需要调整控制器。 首先,按ID加载订单(特定订单)。然后,从order has_many :books开始,查找与该订单相关联的所有图书。

def index    
  @order = Order.find(params[:order_id])
  @books = @orders.books
end

或者如果您已定义current_user.orders,则可以执行@orders = current_user.orders。但是你现在的方式,第一行没有做任何事情