Topic: Create a button for submitting values

I need to create a button "submit" for submitting scope_id to the application which should set it globally across the application.

The script I wrote in views/index.rhtml which shows me all the scope Ids, How do I add a button?

<%= start_form_tag :action => 'set_scope' %>
Scope Id: <%= collection_select :subreq, :scope_id, Scope.find(:all), :id, :scope, {:include_blank => false} %><br><br>

Re: Create a button for submitting values

Try:

<%= submit_tag 'Set Scope' %>

Railscasts - Free Ruby on Rails Screencasts

Re: Create a button for submitting values

Thanks Ryan, now if I write a method in my controller, what should I set the value to

def set_scope
  @subreq = Subreq.new
  @subreq.scope_id = scope_id
end

Is this the right way to do it? I have a "new" method in which a new Subreq is created, so I guess the first statement would be a clash. I just wanna set the scope id selected by the user across the application.

Re: Create a button for submitting values

You probably want to store it in a session so you can fetch it again at a later time:

def set_scope
  session[:scope_id] = params[:subreq][:scope_id]
end

Railscasts - Free Ruby on Rails Screencasts

Re: Create a button for submitting values

This works Ryan except for the fact that it stores the scope.id (value 2)in the subreqs.scope field instead of scope.scope (value 5037). The controller script is below:

class SubreqsController < ApplicationController

  before_filter :login_required, :only=>['index','new','search','create','update','edit']

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }


  def index
  end

  def index_by_scope_id
  end
 
  def index_by_req_id
  end

  def index_by_br_tr_no
  end

  def index_by_title
  end

 
  def list
    @subreq_pages, @subreqs = paginate :subreqs, :per_page => 10
  end

  def show
    @subreq = Subreq.find(params[:id])
  end

  def set_scope
      session[:scope_id] = params[:subreq][:scope_id]

  end


  def new
    @subreq = Subreq.new
    @resources = Resource.find_all
    @bfields = Bfield.find_all
    @itfields = Itfield.find_all
    @user = session[:user]
    @subreq.user_id = @user.id
    @scope = session[:scope_id]
    @subreq.scope_id = @scope.scope
  end

  def search
      @query = request.raw_post
      @q = @query[7...@query.length]
      @phrase = "%" + @q + "%"
      @results = Subreq.find(:all, :conditions=> ["Title like ?", @phrase])
      @total = Subreq.count(:conditions=> ["Title like ?", @phrase] )

  end

  def search_by_scope_id
      @results = Subreq.find(:all, :conditions=> ["Scope_id LIKE ?", "%#{params[:search]}%" ] )
      @total = Subreq.count(:conditions=> ["Scope_id like ?", "%#{params[:search]}%" ] )
      render :layout=>false
  end


  def search_by_req_id
      @results = Subreq.find(:all, :conditions=> ["Req_id LIKE ?", "%#{params[:search]}%" ] )
      @total = Subreq.count(:conditions=> ["Req_id like ?", "%#{params[:search]}%" ] )
      render :layout=>false
  end
       
  def search_by_br_tr_no
      @results = Subreq.find(:all, :conditions=> ["Br_tr_no like ?", "%#{params[:search]}%" ] )
      @total = Subreq.count(:conditions=> ["Br_tr_no like ?", "%#{params[:search]}%" ] )
      render :layout=>false
  end

  def search_by_title
      @results = Subreq.find(:all, :conditions=> ["Title like ?", "%#{params[:search]}%" ] )
      @total = Subreq.count(:conditions=> ["Title like ?", "%#{params[:search]}%" ] )
      render :layout=>false
  end


  def create
    @subreq = Subreq.new(params[:subreq])
    @subreq.resource_ids = Resource.find(params[:resource_ids]) if params[:resource_ids]
    @subreq.bfield = Bfield.find(params[:bfield_ids]) if params[:bfield_ids]
    @subreq.itfield = Itfield.find(params[:itfield_ids]) if params[:itfield_ids]
    #These are added in case there are validation errors when saving and the new method is re-rendered
    @resources = Resource.find_all
    @bfields = Bfield.find_all
    @itfields = Itfield.find_all
    @user = session[:user]
    @subreq.user_id = @user.id
    @scope = session[:scope_id]
    @subreq.scope_id = @scope.scope
       
       
    if @subreq.sub_function.blank?
       @subreq.real_number = @subreq.br_tr_no + "." + @subreq.scope_id + "." + @subreq.req_extension
    else
       @subreq.real_number = @subreq.br_tr_no + "." + @subreq.sub_function + "." + @subreq.scope_id + "." + @subreq.req_extension
    end
   
    #@subreq.real_number = @subreq.real_num

    if @subreq.save
      flash[:notice] = 'Subreq was successfully created.'
      redirect_to :action => 'edit', :id=>@subreq
    else
      render :action => 'new'
    end
    @su
  end

  def edit
    @subreq = Subreq.find(params[:id])
    @resource = @subreq.resource
    @bfield = @subreq.bfield
    @itfield = @subreq.itfield
    @user = @subreq.user
    @scope = @subreq.scope
    @resources = Resource.find_all
    @bfields = Bfield.find_all
    @itfields = Itfield.find_all

  end

  def update
    @subreq = Subreq.find(params[:id])
    @subreq.resource_ids = Resource.find(params[:resource_ids]) if params[:resource_ids]
    @subreq.bfield = Bfield.find(params[:bfield_ids]) if params[:bfield_ids]
    @subreq.itfield = Itfield.find(params[:itfield_ids]) if params[:itfield_ids]
    @user = session[:user]
    @subreq.user_id = @user.id
    @scope = session[:scope_id]
    @subreq.scope_id = @scope.scope
    @subreq.last_updated_by_id = @user
    #These are added in case there are validation errors when saving and the new method is re-rendered
    @resources = Resource.find_all
    @bfields = Bfield.find_all
    @itfields = Itfield.find_all

    if @subreq.sub_function.blank?
       @subreq.real_number = @subreq.br_tr_no + "." + @subreq.scope_id + "." + @subreq.req_extension
    else
       @subreq.real_number = @subreq.br_tr_no + "." + @subreq.sub_function + "." + @subreq.scope_id + "." + @subreq.req_extension
    end

    if @subreq.phase_id.to_s != params[:subreq][:phase_id]
       @phase_tracking = PhaseTracking.new
          @phase_tracking.phase_id = @subreq.phase_id
          @phase_tracking.subreq_id = @subreq.id
          @phase_tracking.req_id = @subreq.req_id
          if @phase_tracking.save
             flash[:notice] = 'PhaseTracking was successfully created.'
          end
    end

    if @subreq.status_id.to_s != params[:subreq][:status_id]
       @status_tracking = StatusTracking.new
          @status_tracking.status_id = @subreq.status_id
          @status_tracking.subreq_id = @subreq.id
          @phase_tracking.req_id = @subreq.req_id
          if @status_tracking.save
             flash[:notice] = 'StatusTracking was successfully created.'
          end
    end
   


    if @subreq.update_attributes(params[:subreq])
      flash[:notice] = 'Subreq was successfully updated.'
      redirect_to :action => 'edit', :id => params[:id]
    else
      render :action => 'edit'
    end
  end

  def destroy
    Subreq.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

Re: Create a button for submitting values

To fetch the scope from the session you need to do this:

    @scope = Scope.find(session[:scope_id])

Railscasts - Free Ruby on Rails Screencasts