Topic: two questions: selected value through form and define a variable

Hi!

I have two questions:

1) Newby question: I have chapter select list and new card form. How can I send selected chapter value through new card form? I cannot put chapter select list into new card form because I use ajax form to add new chapters. Should I use some javascript that observe what chapter user picked or is there some other way...? 

2) In a view I'm using select helper were using :selected => nr_chapter (variable). So, when I'm using variable (not @instance_variable) then i don't have to define it in controller? At leas right now it works and I just wonder why? (I haven't define it in controller)

In other way.. you don't have to defined variable and you have to define @instance_variable in controller before using in view. Just correct me if I'm wrong.

I'm using quote because then I can the code bold.

Thanks for any tips!
Priit


cards_controller.rb

def new
    @card = Card.new
  end
 
  def create_remote_chapter
      new_chapter = Chapter.new(params[:chapter])
      if new_chapter.save
        flash[:new_chapter_notice] = 'New chapter ' + new_chapter.name + ' was added!'
       else
        flash[:new_chapter_notice] = 'Chapter was not created because ' + new_chapter.errors["name"]
      end
      render :partial => 'select_chapter', :locals => { :nr_chapter => new_chapter.id }
    end

  def create
    card = Card.new(params[:card])
    chapter = params[:chapter]
    box = 1       
    if card.save
      position = Position.new(:card_id => card["id"],
                              :chapter_id => chapter["chapter_id"],
                              :box_id => box)
      if position.save
        flash[:notice] = 'Card was successfully created'
        redirect_to :action => 'list'
      else
        flash[:notice_create] = 'There is a problem saving a new card (with position)'
        render:action => 'new'
      end
           
    else
      flash[:notice_create] = 'There is a problem saving a new card'
      render :action => 'new'
    end
  end

new.rhtml

<p style="color: green"><%= flash[:notice_create] %></p>
<h1>New Card</h1>

<div id="select_chapter">
    <%= render :partial => 'select_chapter' %>
</div>

<%= form_tag(:action => 'create'), should I add here select_chapter value? %>
  <%= render :partial => 'form' %>
  or here with hidden field and using javascript?
  <%= submit_tag 'Submit' %>
<%= end_form_tag %>

<br />
<%= link_to 'List', :action => 'list' %>

<script type="text/javascript">
  $("card_question").focus();
</script>

_select_chapter.rhtml

<p style="color: green"><%= flash[:new_chapter_notice] %></p>
Chapter: <%= select(:chapter, :chapter_id, Chapter.find_all.collect {|c| [c.name, c.id]}, :selected => nr_chapter ) %>

    <%= link_to_function 'Add a new', 'Toggle.display("add_chapter"); $("chapter_name").focus()', :id => 'add_a_new_link', :style => 'display: inline' %>
   
<%= form_remote_tag( :update => 'select_chapter',
                                         :url => {:action => 'create_remote_chapter'},
                                         :html => {:id => 'add_chapter', :style => 'display: none'},
                                         :before => %(Element.show('loader')),
                                         :success => %(Element.hide('loader')),
                                         :complete => 'Element.hide("add_chapter"); $("card_question").focus()' )%>
                <%= text_field :chapter, :name, :size => 20, :maxlength => 30 %>
                <%= submit_tag 'Add' %>
                <sup><%= link_to_function 'Cancel', 'Toggle.display("add_chapter"); $("card_question").focus()' %></sup>
                <%= image_tag 'loader.gif', :id => 'loader', :style => 'display: none' %>
<%= end_form_tag %>

Estonian ruby coders => http://ruby.ee

Re: two questions: selected value through form and define a variable

1) I moved select into the form but now I have to make some css hacks (a little later). However if anyone knows good way who to "observe" other selected fields... probably using javascript.

2) Several hours of journey... and then I understood why the heck I'm not using sessions.
So with sessions everything worked well.

I just copy here some code what is working for me right now.

Oki, Priit



controller

 def new
    @card = Card.new
    session[:nr_chapter] ||= 0
  end
   
    def create_remote_chapter
      new_chapter = Chapter.new(params[:chapter])     
      if new_chapter.save
        flash[:new_chapter_notice] = 'New chapter ' + new_chapter.name + ' was added!'
       else
        flash[:new_chapter_notice] = 'Chapter was not created because ' + new_chapter.errors["name"]
      end
      session[:nr_chapter] = new_chapter.id
      @nr_chapter = new_chapter.id
      render :partial => 'select_chapter'
    end
       
  def create
    card = Card.new(params[:card])
    chapter = params[:chapter]
    box = 1     
    if card.save
      position = Position.new(:card_id => card["id"],
                              :chapter_id => chapter["chapter_id"],
                              :box_id => box)
       if position.save
         flash[:notice] = 'Card was successfully created'
         session[:nr_chapter] = chapter["chapter_id"].to_i
         redirect_to :action => 'new'
       else
         flash[:notice_create] = 'There is a problem saving a new card (with position)'
         render :action => 'new'
       end           
    else
      flash[:notice_create] = 'There is a problem saving a new card'
      render :action => 'new'
    end
  end

new.rhtml
<p style="color: green"><%= flash[:notice_create] %></p>
<h1>New Card</h1>

<%= form_remote_tag( :update => 'select_chapter',
                                         :url => {:action => 'create_remote_chapter'},
                                         :html => {:id => 'add_chapter', :style => 'display: none'},
                                         :before => %(Element.show('loader')),
                                         :success => %(Element.hide('loader')),
                                         :complete => 'Element.hide("add_chapter"); $("card_question").focus()' )%>
                <%= text_field :chapter, :name, :size => 20, :maxlength => 30 %>
                <%= submit_tag 'Add' %>
                <sup><%= link_to_function 'Cancel', 'Toggle.display("add_chapter"); $("card_question").focus()' %></sup>
                <%= image_tag 'loader.gif', :id => 'loader', :style => 'display: none' %>
<%= end_form_tag %>     

<%= form_tag(:action => 'create') %>
<div id="select_chapter"><%= render :partial => 'select_chapter', :locals => { :nr_chapter => @nr_chapter } %></div>
  <%= render :partial => 'form' %>
  <%= submit_tag 'Submit', 'tabindex' => 5 %>
<%= end_form_tag %>

<br />
<%= link_to 'List', :action => 'list' %>

<script type="text/javascript">
  $("card_question").focus();
</script>


_select_chapter.rhtml
<p style="color: green"><%= flash[:new_chapter_notice] %></p>
Chapter: <%= select(:chapter, :chapter_id, Chapter.find_all.collect {|c| [c.name, c.id]}, :selected => session[:nr_chapter] ) %>

    <%= link_to_function 'Add a new', 'Toggle.display("add_chapter"); $("chapter_name").focus()', :id => 'add_a_new_link', :style => 'display: inline' %>

Estonian ruby coders => http://ruby.ee