ruby on rails - (How) Can I use a form object for the edit/update routines? -
couldn't resist try out relaunched railsforum , crossposted question here.
i have following form object (in rails 4)
class stationform include virtus include activemodel::model # think 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
i can use form object in new/create methods
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
however, don't succeed use edit/update procedures...
is possible use form object edit/update , if yes, how done?
you have use "initialize" method in stationform object use editing. if pass id, assume, object exist , there can treat persisted object. add "update" method update attributes of object.
class stationform include virtus.model include activemodel::model # think unnecessary # extend activemodel::naming # include activemodel::conversion # include activemodel::validations attr_reader :station attribute :station_name, string attribute :station_description, string attr_reader :address attribute :address_url, string attribute :address_priority, integer attribute :address_is_active, boolean def initialize(attr = {}) if !attr["id"].nil? @station = station.find(attr["id"]) @address = @station.addresses.first self[:station_name] = attr[:station_name].nil? ? @station.name : attr[:station_name] self[:station_description] = attr[:station_description].nil? ? @station.description : attr[:station_description] self[:address_url] = attr[:address_url].nil? ? @address.url : attr[:address_url] self[:address_priority] = attr[:address_priority].nil? ? @address.priority : attr[:address_priority] self[:address_is_active] = attr[:address_is_active].nil? ? @address.is_active : attr[:address_is_active] else super(attr) end end def persisted? @station.nil? ? false : @station.persisted? end def id @station.nil? ? nil : @station.id end def save if valid? persist true else false end end def update if valid? update_form 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 def update_form @station.update_attributes( :name => self[:station_name], :description => self[:station_description] ) @address.update_attributes( :url => self[:address_url], :priority => self[:address_priority], :is_active=> self[:address_is_active] ) end end
and controller like
def new @station = stationform.new end def edit @station = stationform.new("id" => params[:id]) end def create @station = stationform.new(station_params) respond_to |format| if @station.save format.html { redirect_to stations_path, notice: 'station created.' } format.json { render :show, status: :created, location: @station } else format.html { render :new } format.json { render json: @station.errors, status: :unprocessable_entity } end end end def update @station = stationform.new(station_params.merge("id" => params[:id])) respond_to |format| if @station.update format.html { redirect_to stations_path, notice: 'station updated.' } format.json { render :show, status: :ok, location: @station } else format.html { render :edit } format.json { render json: @station.errors, status: :unprocessable_entity } end end end
use "persisted" , "id" method stationform in _form.html.erb
<%= form_for(@station, :url => @station.persisted? ? station_path(@station.id) : stations_path, :method => @station.persisted? ? "put": "post") |f| %> <% if @station.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@station.errors.count, "error") %> prohibited station being saved:</h2> <ul> <% @station.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :station_name %><br> <%= f.text_field :station_name %> </div> <div class="field"> <%= f.label :station_description %><br> <%= f.text_field :station_description %> </div> <div class="field"> <%= f.label :address_url%><br> <%= f.text_field :address_url %> </div> <div class="field"> <%= f.label :address_priority%><br> <%= f.text_field :address_priority%> </div> <div class="field"> <%= f.label :address_is_active %><br> <%= f.text_field :address_is_active %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
Comments
Post a Comment