我有以下表单对象(在Rails 4中)
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 endprivate 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) endend
我可以在new / create方法中使用这个表单对象
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) endprivate def station_form_params params.require(:station_form).permit(:station_name,:station_description,:address_url,:address_priority,:address_is_active) endend
但是,我没有成功将它用于编辑/更新程序……
是否可以使用表单对象进行编辑/更新,如果是,将如何完成?
解决方法 您必须使用StationForm对象中的“initialize”方法将其用于编辑.如果你传递一个ID,它将假设该对象已经存在,并从那里我们可以将它视为一个持久化对象.还添加“update”方法来更新对象的属性.
class StationForminclude Virtus.modelinclude ActiveModel::Model# I think this is unnecessary# extend ActiveModel::Naming# include ActiveModel::Conversion# include ActiveModel::ValIDationsattr_reader :stationattribute :station_name,Stringattribute :station_description,Stringattr_reader :addressattribute :address_url,Stringattribute :address_priority,Integerattribute :address_is_active,Booleandef 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) endenddef persisted? @station.nil? ? false : @station.persisted?enddef ID @station.nil? ? nil : @station.IDenddef save if valID? persist true else false endenddef update if valID? update_form true else false endendprivate def persist @station = Station.create(name: station_name,description: station_description) @address = @station.addresses.create(url: address_url,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] ) endend
和控制器会像
def new @station = StationForm.newenddef edit @station = StationForm.new("ID" => params[:ID])enddef create @station = StationForm.new(station_params) respond_to do |format| if @station.save format.HTML { redirect_to stations_path,notice: 'Station was successfully created.' } format.Json { render :show,status: :created,location: @station } else format.HTML { render :new } format.Json { render Json: @station.errors,status: :unprocessable_entity } end endenddef update @station = StationForm.new(station_params.merge("ID" => params[:ID])) respond_to do |format| if @station.update format.HTML { redirect_to stations_path,notice: 'Station was successfully updated.' } format.Json { render :show,status: :ok,location: @station } else format.HTML { render :edit } format.Json { render Json: @station.errors,status: :unprocessable_entity } end endend
使用_form.HTML.erb中StationForm的“persisted”和“ID”方法
<%= form_for(@station,:url => @station.persisted? ? station_path(@station.ID) : stations_path,:method => @station.persisted? ? "put": "post") do |f| %> <% if @station.errors.any? %> <div ID="error_explanation"> <h2><%= pluralize(@station.errors.count,"error") %> prohibited this station from being saved:</h2> <ul> <% @station.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div > <%= f.label :station_name %><br> <%= f.text_fIEld :station_name %> </div> <div > <%= f.label :station_description %><br> <%= f.text_fIEld :station_description %> </div> <div > <%= f.label :address_url%><br> <%= f.text_fIEld :address_url %> </div> <div > <%= f.label :address_priority%><br> <%= f.text_fIEld :address_priority%> </div> <div > <%= f.label :address_is_active %><br> <%= f.text_fIEld :address_is_active %> </div> <div > <%= f.submit %> </div><% end %>总结
以上是内存溢出为你收集整理的ruby-on-rails – (如何)我可以使用表单对象进行编辑/更新例程吗?全部内容,希望文章能够帮你解决ruby-on-rails – (如何)我可以使用表单对象进行编辑/更新例程吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)