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 %> <table> <tr> <td colspan="2">Here put your own idea! :)</td> </tr> <tr> <td colspan="2"><%= hidden_field_tag 'id_obj', if @update then @update.id else '' end %></td> </tr> <tr> <td>Name: </td> <td><%= text_field_tag 'name', if @update then @update.name else '' end %></td> </tr> <tr> <td>Text of message: </td> <td><textarea name="text"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" name="send" value="Save this post!" /></td> </tr> </table> <% end %>
So I would to ask you about help, how to make this code more effective -- this solve is not much nice...
Thanks, M.