Greetings,
I’m trying to leverage the “request.post?” information to use one method
for both insert and update. This Rails Recipes # 33 with a minor twist
to the first line based on another online snippet.
=================================
def edit
@company = params[:id] ? Company.find(params[:id]) : Company.new
if request.post?
@company.attributes = params[:company]
redirect_to :controller => "main", :action => "home" and return
if @company.save
end
end
The above action is called from a button as follows:
<%= button_to “Add Company”, { :controller => “company”, :action =>
“edit” }%>
=================================
But when the edit method runs, request.post? is true and the save method
fires, causing the Company#validates_presence_of code to detect a
missing company name.
Result: The form opens, but it already has an error shown through the
inclusion of an “error_messages_for(:company)” display.
Please help me understand the right way to combine these elements so
that I don’t have to write add and edit methods for every model.
Thanks!
Michael