简体   繁体   中英

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM