Skip to main content

Multiple objects in form and controller, hitting a wall.

General • Asked by shakycode

So I'm working on a old version of a legacy app Chris and I paired on, trying to refactor some functionality and am hitting a wall.

I currently have some basic paging functionality which creates a "page" (message), and then sends it out to each unit (vehicle) thusly. It's a very simple implementation and works well, but I'm trying to expand it so I can page units within a specific region instead of one by one or all. Here's what my models, views, and controllers look like so far.

unit.rb

class Unit < ActiveRecord::Base
belongs_to :region
end

paging.rb

class Paging < ActiveRecord::Base
  attr_accessible :message, :unit_id, :region_id  
  belongs_to :unit
  attr_accessor :region_id
end

region.rb

class Region < ActiveRecord::Base
  has_many :units
end

pagings_controller.rb (excerpt)

  def index
    @pages = Paging.order('created_at DESC').paginate(:per_page => 5, :page => params[:page])
    @page = Paging.new
  end

def create
    message = params[:paging][:message]

    units = if params[:paging][:unit_id] == "all"
        Unit.active
    else
        [Unit.find(params[:paging][:unit_id])]
    end

    units.each do |unit|

      @page = Paging.new(params[:paging])
      @page.unit_id = unit.id

      if @page.save
        PagingsMailer.paging(unit.incharge, unit, message).deliver
        PagingsMailer.paging(unit.attendant, unit, message).deliver
        send_paging_sms
      end
    end
    @page = Paging.new
    @pages = Paging.limit(5).order('id desc')
    respond_to do |format|
      format.html {redirect_to pagings_path, notice: "Page was successfully sent"}
      format.js {render "index", notice: "Page was successfully sent"}
    end
  end

app/views/pagings/_form.html.erb

<%= form_for(@page) do |f| %>
    <div class="control-group">
        <div class="controls">  
      <%= f.label :message %>
      <%= f.text_area(:message, size: "40x2", :maxlength => 254, placeholder: "Enter your message here", required: true) %>

      <%= f.label 'Unit'%>

      <%= f.select :unit_id, options_for_select(Unit.active.order(:unit_name).map{ |unit| [unit.unit_name, unit.id] } + [["All Units", "all"]]), {}, {:class => 'select'} %>

      <%= f.select :region_id, options_for_select(Region.order(:area).map{ |region| [region.area, region.id]}, {:class => 'select'}) %>
         </div>
        </br>
        <%= f.button  'Send Page', class: 'btn btn-info', data: {disable_with: "<i class='icon-spinner'></i>Sending..."} %>
    </div>
<% end %>

I've already refactored the pagings controller create method to make it cleaner and more DRY, but I'm having a hard time figuring out how to send pages to either a unit, units (all), or units by region.

I'm right in the middle of working on this code so things are not working properly and could use some guidance. As you can see, in the form, I'm mapping the Regions to get the :region_id so that I can pass it into the params of the request to the controller. Since the paging model does not need to populate the region_id per say (not sure if I need this or not) I've set an attr_accessor for :region_id in the paging model.

So two problems I'm facing right now.
In the params request from the form the :region_id does get passed into the request, but once it hits the controller, I'm unsure of how to deal with it. My initial thought was to assign it to a variable like so:

region = Region.find(params[:paging][:region_id])

This will allow me to grab the :region_id and use it within the controller action. From there I should be able to do something like this:

region.units.active #pull all units for the specific region_id and scope off of active from the unit model

This is where I'm stuck. I'm unsure as to how to handle the region variable inside my existing create method to create a new page and page it out to each unit within that region without breaking the existing architecture.

Second, once I do figure out how to do this, I need to figure out in the form how to choose whether or not to select by :unit_id or :region_id so there form does not pass the params to both.

Sorry if this is a bit scattered, but I'd like to discuss how I might be able to do this. If my question is not clear, please let me know and I'll be happy to elaborate and/or add more sample code.

Cheers!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.