views:

96

answers:

2

Hi,

Pretty new to rails. Am doing a simple book store application. We have different subject areas like physics, chem, philosophy, etc...Each subject area has some books under it. The url ending with book/list will list all books irrespective of subject areas. The subject areas also appear in the same page as hyperlink. The url ending with book/show_subject/<subject id> lists all the books with that subject id. So I click on the subject if I want to see the books belonging to that subject area. The book/list page does have a hyperlink to a page where we can add new books (book/new).

Suppose if there are no books for that subject (say physics), I display a link saying would you like to add books here? Clicking on that it takes me to the page where I enter the book info + the subject area the book belongs it (book/new). I have a drop down for the subject area...how do I configure this drop down to show up physics initially, (since I navigated to this page after noticing that there were no physics in the store).

A: 

an option could be to make the link point to

/book/new?book_subject_id_=1

and recieving the selection in the controller with this line:

class BookController < #...
   def new
     @book = Book.new(:subject_id => params[:book_subject_id])
     #...
   end
   #...
end

will create a Book AR object with the subject you chose.

Then in your view the form will be defined to accept the @book, something like:

<% form_for @book, :url => your_create_url, :method => { :post } do %>
  <%= f.collection_select :subject_id, Subject.all.map{|s| [s.name, s.id]} %>
...

the important part is that is calling the subject_id method on the book object so you can retrieve the value you set in the request.

this way the select will select the physics subject

(note: following rails conventions book should be books in controllers and urls)

makevoid
How do u make the link point to /book/new?book[subject_id]=1? Have tried something like <%= link_to 'Would you like to add one?', {:action => 'new'}, {:default_subject => @subject.name} %>
deostroll
sorry I corrected the answer, you have to handle that in your new action in BookController, tell me if you haven't got something
makevoid
getting an error here <%= collection_select :id, Subject.all.map{|s| [s.name, s.id]} %>Its saying wrong number of arguments (2 for 5). PS: I still can't create the link like how you've said...whenever I try to render it it comes like /book/new/1 or book/new/2 depending of the subject I have navigated from...
deostroll
A: 

Usually you would do this by adding a parameter to the GET request. You can add it to your link this way:

link_to new_book_path, :default_subject => @subject.name

Then in the controller action, you assign the parameters to your new object:

class BookController < ActiveRecord
  ...
  def new
     @book = Book.new
     if param[:default_subject]
       @book.subject = Subject.find_by_name(param[:default_subject])
     end
  end
  ... 

You don't need to change your form at all, because it will be using the @book object to assign defaults (if you are using the normal form tag helpers). For example:

<% form_for @book do |form| %>
  <%= form.collection_select :subject_id, Subject.all, :id, :name %>
  .. other fields ..
<% end %>
jamuraa
hi, How do u write the new.html.erb page for this controller?
deostroll
added an example of how to do the form.
jamuraa