Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Use a form object for update-procedures

rails4form object update design-pattern;

  • Please log in to reply
4 replies to this topic

#1 speendo

speendo

    Passenger

  • Members
  • 3 posts

Posted 24 August 2013 - 07:56 AM

Hi!

 

Thank you for re-launching this board! As I wanted to try it out, I could not resist to cross-post this question.

 

In my project I generated a form object that handles the connection to multiple models. It looks like this

class StationForm
    include Virtus

    include ActiveModel::Model
    # I think this is unnecessary
    #   extend ActiveModel::Naming
    #   include ActiveModel::Conversion
    #   include ActiveModel::Validations


# Station
    attr_reader :station

    attribute :station_name, String
    attribute :station_description, String

# Address
    attr_reader :address

    attribute :address_url, String
    attribute :address_priority, Integer
    attribute :address_is_active, Boolean

    def persisted?
        false
    end

    def save
        if valid?
            persist
            true
        else
            false
        end
    end

private

    def persist
        @station = Station.create(name: station_name, description: station_description)
        @address = @station.addresses.create(url: address_url, priority: address_priority, is_active: address_is_active)
    end
end

This works well to generate new stations and their addresses. Here is the related code in the controller

class StationsController < ApplicationController
    def new
        @station_form = StationForm.new
    end

    def create
        @station_form = StationForm.new(station_form_params)
        @station_form.save

        redirect_to station_path(@station)
    end

private

    def station_form_params
        params.require(:station_form).permit(:station_name, :station_description, :address_url, :address_priority, :address_is_active)
    end
end

Now, I don't succeed to enhance this form object and controller for the edit and update methods.

 

Can you help me to do so?

 

Thank you in advance!


  • speendo likes this

#2 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 24 August 2013 - 11:10 AM

I am not familiar with virtus and am not sure what it does for you.

What have you tried so far?

How far did you get?


Programming is just about problem solving!


#3 speendo

speendo

    Passenger

  • Members
  • 3 posts

Posted 24 August 2013 - 09:01 PM

Hi james!

 

Thank you for your answer!

 

I don't know virtus very well either - it was recommended in this article: http://blog.codeclim...erecord-models/ (pattern #3).

However, I think it doesn't do much more than providing a simple "bridge" to the original models (see lines 11-22 in StationForm). In the "new"-form I can use the object just like it was an ordinary model.

 

However, my form object lacks an update method... and virtus doesn't provide one.

 

I think the problem is, that the form object just exists until the information is stored in the database. To update an object, I would need to recall the previous state of the object first. This previous state is stored in multiple tables (in the provided example it's stored in Station and in address). I cannot update (or even find) the form object itself because as such it doesn't exist anymore.

 

What I try is to implement a static update method in the StationForm class, but I don't succeed :(



#4 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 24 August 2013 - 10:25 PM

I'm thinking that for an edit action you would need to read in the station record based on the id from the params hash as normal and create a new instance of the form object, passing in the @station variable.

 

That should give your edit template the values you wish to edit, then submitting the form should just work in the update action the same way it works in the create action

 

I'm only guessing as I have never used form objects before but looking through your code that might just work.


Programming is just about problem solving!


#5 speendo

speendo

    Passenger

  • Members
  • 3 posts

Posted 25 August 2013 - 09:36 AM

Thank you!

 

I am about to try this...

 

I am quite new to rails (and ruby) so it takes some time. I will report if it worked.

 

Thanks!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users