簡體   English   中英

在rails中更新記錄

[英]update record in rails

我想在我的“Place”模型中添加新圖像。 向控制器添加新方法/操作並從Place show page添加新表單是一種好習慣嗎?

如果沒有那么什么是最佳做法?

在控制器中

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

#DO THE CHANGES HERE

@palce.save;
redirect_to places_path
end

在路線上

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

在視圖中

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

使用新表單:form_for @place with image file_field,在控制器中使用update action

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

如果我理解你想以某種方式修改你的模型,這些情況的好習慣是添加一個遷移,閱讀有關遷移rails指南以獲取更多信息。

運行遷移並更新數據庫后,可以根據需要編輯控制器/視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM