Rails 4 Form - Adding a new input to an existing record -
i have form users enter profile info saved in user model. need collect piece of information users @ later stage (on different page). how collect info , append existing user record?
here form code 1 additional input need user. when hit submit, runs without error new field not saved in model. ran migration add field model.
<%= form_for @user, html: { method: :put }) |f| %> <div class="form-group"> <%= f.label :what_is_your_favorite_color %> <%= f.text_field :color, class:"form-control" %> </div> <div class="form-group"> <%= f.submit "submit", class:"btn btn-primary" %> </div> <% end %>
my controller update method blank .. can tell me update method should like? user logged in need find user record (probably id column?) , write or update new input model.
def update ?? end
you first need fetch record, pass params hash, , rails rest.
def update @record = record.find(params[:id]) if @record.update(record_params) redirect_to @record else render :edit end end
if using rails 4, need account strong_parameters
. add new attribute permitted attributes.
def record_params params.require(:record).permit(:color, :and, :other, :attributes, :go, :here) end
the above code assumes record id in params hash, or in other words, using restful routes. if not, may want pass in id session (if is, say, user record, , using devise).
def update @record = record.find(current_user) # ... rest should same end
Comments
Post a Comment