Abdulaziz Alsubaie
Abdulaziz Alsubaie

Reputation: 720

update record in rails

I want to add new image to my "Place" model. is it good practice to add new method/action to the controller and add new form to from Place show page?

if no then what is the best practice?

in the controller

def add_images
@place = Place.find(params[:id])

#DO THE CHANGES HERE

@palce.save;
redirect_to places_path
end

in the route

match 'places/:id/add_images' => 'places#add_images'

in the view

<%= form_tag @place.id.to_s+'/add_images', :method => 'post', :multipart => true do %>
 <!-- just test -->
 <div class="actions">
 <%= submit_tag "submit new images" %>
 </div>
<% end %>

Upvotes: 0

Views: 189

Answers (2)

If I understand well you want to modify your model in some way, the good practice for these cases is to add a migration, read the rails guide about migration for more information.

Once you have run the migration and your DB is updated you can edit the controllers/views as you want.

Upvotes: 1

gayavat
gayavat

Reputation: 19398

use new form: form_for @place with image file_field, use update action in controller

def update
  @place.update_attributes params[:place]
end

Upvotes: 1

Related Questions