How to streamline this a bit of code

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:
<% end %>

So I would to ask you about help, how to make this code more effective
– this solve is not much nice…

Thanks, M.

On Wed, Mar 9, 2011 at 6:51 AM, Manny 777 [email protected] wrote:

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.

try generating a model using a scaffold and see how the view and
controller actions talk with each other. there you’ll see what you need
to change in your mini app. good luck!

   else
     redirect_to :action => 'index'
 redirect_to :action => 'index'
 <td colspan="2"><%= hidden_field_tag 'id_obj', if @update then

[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

What about using an CRUD controller?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs