user3560626
user3560626

Reputation: 55

Updating model data from form value ruby on rails

Hello I'm very new to ruby on rails. Here i have form

<%= form_tag do %>
    <%= text_field_tag :fullname, params[:fullname], placeholder: 'fullname' %>
    .
    .
    .

    <a href="/userEdit"> <%= submit_tag "save" %> </a>
<% end %>

Those form is to update model data. In my controller I have

def updateUser
    user = Users.find(session[:user_id])
    user.fullname = params[:fullname]
    user.save
    render 'profile'
end

It's not working (data doesn't updated), but when I tried

def updateUser
    user = Users.find(session[:user_id])
    user.fullname = 'david'
    user.save
    render 'profile'
end

It's working (the data updated). I don't know where did I go wrong, please kindly help me. Sorry for asking such easy question, I'm a newbie to Ruby (and so does Rails), I searched but didn't get a suitable answer for this case. Thank you

Upvotes: 1

Views: 4244

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

I'm very new to ruby on rails

Welcome - let me give you some ideas!

--

Form

Firstly, your form_tag is not created properly. You need to put a route in this (so it knows where to submit the data):

<%= form_tag your_path_here do %>

This is for the form_tag helper, however, as you're editing an object, you'll probably be better using the form_for helper - which takes an actual object (value):

<%= form_for @user do |f| %>
   <%= f.text_field :full_name %>
   <%= f.submit "Submit" %>
<% end %>

This type of form has to be used with the resourceful structure of Rails, which is my second point...

--

Resources

Rails is built around the idea of a resourceful infrastructure (where every data object is a resource). If you're updating / editing an object, the typical explanation is that you'll be editing a resource, and consequently will want to employ rails' resourceful structure to handle it:

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   before_action :set_user, only: [:edit, :update, :show]

   def index
       @users = User.all
   end

   def new
       @user = User.new
   end

   def create
       @user = User.new(user_params)
       @user.save
   end

   def edit
   end

   def update
      @user.update(user_params)
   end

   private

   def user_params
      params.require(:user).permit(:fullname, :etc)
   end

   def set_user
       @user = User.find params[:id]
   end

end

This will allow you to define the resourceful routes for this:

#config/routes.rb
resources :users

Using this setup with form_for will work for you

Upvotes: 4

Farrukh Abdulkadyrov
Farrukh Abdulkadyrov

Reputation: 717

Didn't notice in the beginning. Your form is incorrect. You did not specify a URL for action, and you put your submit tag within a link so basically your link is getting called not your form submitted.

<%= form_tag '/userEdit' do %>
    <%= text_field_tag :fullname, params[:fullname], placeholder: 'fullname' %>
    .
    .
    .

    <%= submit_tag "save" %>
<% end %>

Make sure that you have specified a route for userEdit post method.

Upvotes: 0

Related Questions