Hi,
I’m new in RoR and i wrote this mini-app, which can to add, edit and
delete the item in/from database. I think the edit and add item isn’t
solve very good. I’m making both operations during one form.
CONTROLLER:
def create
if params[:name].length > 0 then
if params[:id_obj].length > 1 then #editing of current record
@update=Person.find(params[:id_obj])
if @update.update_attributes(:name=>params[:name])
flash[:notice] = 'Your record was succesfly edited.'
redirect_to :action => 'index'
else
flash[:warning] = 'ERROR: your record was not edited (no
input)!’
redirect_to :action => ‘index’
end
else #creating the new record
@haha=Person.new(:name=>params[:name])
if @haha.save
flash[:notice] = 'Your record was succesfly saved.'
redirect_to :action => 'index'
else
flash[:warning] = 'ERROR: your record was not succesfly
saved.’
redirect_to :action => ‘index’
end
end
else #input for name is empty
flash[:warning] = ‘ERROR: your record was not succesfly saved (no
input).’
redirect_to :action => ‘index’
end
end
VIEW:
<%= form_tag({ :action => ‘create’ }, { :method => ‘post’ }) do %>
Here put your own idea! :) | |
<%= hidden_field_tag 'id_obj', if @update then @update.id else '' end %> | |
Name: | <%= text_field_tag 'name', if @update then @update.name else '' end %> |
Text of message: | |
So I would to ask you about help, how to make this code more effective
– this solve is not much nice…
Thanks, M.