# Buying book from library

**URL:** <https://www.ruby-forum.com/t/buying-book-from-library/180345>\
**Category:** Rails\
**Created:** [December 18, 2009, 12:45am UTC](https://www.ruby-forum.com/t/buying-book-from-library/180345 "2009-12-18T00:45:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xytk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/e36b37/32.png) [@xytk](https://www.ruby-forum.com/u/xytk)\
**Post date:** [December 18, 2009, 12:45am UTC](https://www.ruby-forum.com/t/buying-book-from-library/180345/1 "2009-12-18T00:45:32Z")

</div>

hi i’m very new to ROR.  
i must do a basic library system its my homework.  
i have models, user.rb (created by restful\_authentication plugin) and  
book.rb (created by scaffolding)  
controllers: sessions\_controller and users\_controller(created by  
restful\_authentication plugin) and books\_controller(created by  
scaffolding)

**user.rb,sessions\_controller,users controller have the same codes  
restful\_authentication screencast and its working correct**\*\*\*\*\*\*

and i want to have a buy link at books page  
for example i login as ‘Jack’  
when i login i’m showing the books page its OK. but also i wanna see  
buy link and when i click it and return to the books page i wanna see  
the books Jack bought  
then logut and login again as’michael ’ i wanna see books page and  
idont wanna see jack’s book i just wanna see michael’s book  
how can i do it?  
here is my books\_controller.rb  
i wanna have an buy action here(user will buy a book)

class BooksController \< ApplicationController

# GET /books

# GET /books.xml

def index  
@books = Book.all

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @books }
end

```

end

# GET /books/1

# GET /books/1.xml

def show

```
@book = Book.find(params[:id])
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @book }
end

```

end

# GET /books/new

# GET /books/new.xml

def new  
@book = Book.new  
respond\_to do |format|  
format.html # new.html.erb  
format.xml { render :xml =\> @book }  
end  
end

# GET /books/1/edit

def edit  
@book = Book.find(params[:id])  
end

# POST /books

# POST /books.xml

def create  
@book = Book.new(params[:book])  
respond\_to do |format|  
if @book.save  
flash[:notice] = ‘Book was successfully created.’  
format.html { redirect\_to(@book) }  
format.xml { render :xml =\> @book, :status =\> :created,  
:location =\> @book }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @book.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

# PUT /books/1

# PUT /books/1.xml

def update  
@book = Book.find(params[:id])  
respond\_to do |format|  
if @book.update\_attributes(params[:book])  
flash[:notice] = ‘Book was successfully updated.’  
format.html { redirect\_to(@book) }  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @book.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

# DELETE /books/1

# DELETE /books/1.xml

def destroy  
@book = Book.find(params[:id])  
@book.destroy  
respond\_to do |format|  
format.html { redirect\_to(books\_url) }  
format.xml { head :ok }  
end  
end  
end  
and here is my index.html.erb  
i wanna see buy link and also see which book user bought

\< **Welcome \<%=current\_user.login %\>** \>\<%= link\_to  
“logout”, logout\_path %\>

# Listing books
\<% @books.each do |book| %\> 

```
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?',

```

:method =\> :delete %\>

\<% end %\>

| Name |
| --- |
| \<%=h book.name %\> |

  
\<%= link\_to 'New book', new\_book\_path %\> can anyone help? thanks

---

<div class="post-metadata">

**Author:** ![xytk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/e36b37/32.png) [@xytk](https://www.ruby-forum.com/u/xytk)\
**Post date:** [December 18, 2009, 2:41pm UTC](https://www.ruby-forum.com/t/buying-book-from-library/180345/2 "2009-12-18T14:41:07Z")

</div>

Ali Okur wrote:

Forget all that code and get a piece of paper.

Draw out your data model (you need Users, Books, something to track  
which books a user bought, and maybe some other models). That will lead  
you to what migrations, models, controllers, forms and specific actions  
you need to create in your application.

Advice is free, responses to technical questions are free, the actual  
homework is yours to do.

---

<div class="post-metadata">

**Author:** ![xytk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/e36b37/32.png) [@xytk](https://www.ruby-forum.com/u/xytk)\
**Post date:** [December 18, 2009, 11:17pm UTC](https://www.ruby-forum.com/t/buying-book-from-library/180345/3 "2009-12-18T23:17:23Z")

</div>

Ar Chron wrote:

> Ali Okur wrote:
> 
> Forget all that code and get a piece of paper.
> 
> Draw out your data model (you need Users, Books, something to track  
> which books a user bought, and maybe some other models). That will lead  
> you to what migrations, models, controllers, forms and specific actions  
> you need to create in your application.
> 
> Advice is free, responses to technical questions are free, the actual  
> homework is yours to do.

okay.  
but it’s not my homework  
its about my homework but homework is more complex then it.it’s just a  
simple program i wanna practise myself and i just wonder how i will make  
user based action.

i add these codes to my program

\*\*\*\*books\_controller action  
def buy  
@book = Book.find(params[:id])  
@book.user = current\_user  
end

\*\*\*\*routes.rb  
map.resources :books, :collection =\> { :buy =\> :get }

\*\*\*\*books\_ index.html.erb  
\<%= link\_to ‘buy’, buy\_book\_path(book) %\>

but when i am logging to books\_index page

NoMethodError in Books#index  
undefined method `buy\_book\_path’ for #ActionView::Base:0x64a45b0  
why i’m getting this error?

---

<div class="post-metadata">

**Author:** ![xytk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/e36b37/32.png) [@xytk](https://www.ruby-forum.com/u/xytk)\
**Post date:** [December 19, 2009, 7:26am UTC](https://www.ruby-forum.com/t/buying-book-from-library/180345/4 "2009-12-19T07:26:19Z")

</div>

Patience grasshopper… you’re trying to put the roof on the house when  
the foundation is not laid yet.

Where are you storing the information about who purchased which book?

To see what you’ve created with your changes to routes.rb, at the OS  
prompt, try:

rake routes \>routes.lst

Then peruse that routes.lst file. You’ll see all the paths that are  
available. Perhaps you don’t have what you think you have…
