SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question problem with form submitting

    Hello, I have a problem with a form that is driving me crazy


    I have a 2 models Book and BookAuthor associated as below:

    Code Ruby:
    class BookAuthor < ActiveRecord::Base
     
      belongs_to :book
      ...
    end

    Code Ruby:
    class Supplier < ActiveRecord::Base
      has_one :author, :class_name => "BookAuthor"
       ...
    end

    I'm would like to have a 3 steps process in order to submit a book: first book ID, second author ID, third publisher ID so I created these views

    add_view
    add_author
    add_publisher

    and the form on add_author is:

    Code Ruby:
    <% form_for :book_author do |form| %>
           <label for="book_author_first_name">first</label>
    	<%= form.text_field :first_name%>
    	<label for="book_author_last_name">last</label>
    	<%= form.text_field :last_name,%>
            <%= submit_tag "next" %></div>
    	<% end %>


    and one controller as below:

    Code Ruby:
    ...
    def add_book
        if session[:book].nil? 
          @book = Book.new(params[:book])
          @book.status = 0
        else
          @book = session[:book] 
        end
     
        if request.post? and @book.valid?
          @updated_book = Book.new(params[:book])
          @updated_book.status = 0
     
          session[:book] = @updated_book
          redirect_to :action => "add_author"
        end
      end
     
      def add_author
         @book = session[:book]
     
         if @book.author.nil? 
     
           @author = BookAuthor.new(params[:bookAuthor])
           @book.author = @author
         else
           @author = @book.author
          end
     
     
        if request.post? and @author.valid?
          @updated_author = BookAuthor.new(params[:bookAuthor])
          @book.author = @updated_author
          session[:book] = @book
          redirect_to :action => "add_apublisher"
        end
      end

    My issue is that when I submit add_author the submitted values are not inserted into @author (@author = BookAuthor.new(params[:bookAuthor])) so it is not valid.


    What's wrong with my code ???


    THANK YOU SO MUCH (really SO MUCH)
    Alla prossima ...

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code ruby:
      def add_author
         @book = session[:book]
     
     
    #your problem is here, when you first hit this page with a get request, @book.author is nil, so you set @book.author to a blank BookAuthor.
         if @book.author.nil? 
          @author = BookAuthor.new(params[:bookAuthor])
           @book.author = @author
         else
           @author = @book.author
          end
     
        if request.post? and @author.valid?
          @updated_author = BookAuthor.new(params[:bookAuthor])
          @book.author = @updated_author
          session[:book] = @book
          redirect_to :action => "add_apublisher"
        end

    I would try this:
    Code ruby:
      def add_author
         @book = session[:book]
         @author = BookAuthor.new
     
        if request.post?
          @author = BookAuthor.new(params[:bookAuthor])
          @book.author = @author
          if @book.save
            session[:book] = @book
            redirect_to :action => "add_publisher"
           end #else author didn't pass validation
        end
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello thanks for you idea but there is a problem with it

    if I already have an author (this happens when I go back from publisher) I want to keep it and no to create a new empty one.
    And once I post again the form the new author has to be saved on my session.


    Moreover, I guess the issue is about saving the author (related to the form on the view) because if I try to display it on add_publisher, with your code (flash[:notice] = @book.author.name) it doesn't show anything. and if I try with mine I can not hit the page since the author object is empty so it can not be validated.


    I hope it's clear what I wanna do.


    THANK YOU and HAVE A NICE DAY!
    Alla prossima ...

  4. #4
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why with this
    Code:
    @updated_bookAuthor = BookAuthor.new(params[:bookAuthor])
       session[:book][:bookAuthor]=  @updated_bookAuthor
    having this form

    <&#37; form_for :bookAuthor do |form| %>
    <label for="bookAuthor_first_name">first</label>
    <%= form.text_field :first_name %>
    <label for="bookAuthor_last_name">last</label>
    <%= form.text_field :last_name%>
    <%= submit_tag "next" %>
    <% end %>

    I can not have on session[:book][:bookAuthor] a bookAuthor object with the attributes of the submitted form ????


    PS I have changed the association on Book, now
    class Book < ActiveRecord::Base
    has_one :bookAuthor
    ...
    end

    THANK YOU
    Alla prossima ...

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Update:

    if I save bookAuthor as new session variable (session[:bookAuthor]) instead of save the author as a attribute of book (session[:book]) it works but I want to have only 1 object on my session.


    Maybe this can help you to figure out what's wrong.


    THANK YOU
    Alla prossima ...

  6. #6
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any idea ???

    HAVE A NICE DAY !
    Alla prossima ...

  7. #7
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please I can not figure out what's wrong

    any suggestion ????
    Alla prossima ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •