Théo Capdet
Théo Capdet

Reputation: 1062

Simple form_for in the root page

I would like to do a "simple form" in my root page in order to update a field in a table.

<%= bootstrap_form_for @user do |f| %>
    <%= f.text_field :trig, label: "your Trigram", :required => true %>
    <%= f.submit :class => 'btn btn-primary btn-lg btn-block'%>
<% end %>

My route :

root 'static_pages#cvpage'
put '/' => 'users#update'

In my users controller :

def update
    @user = User.find(current_user.id)
    @user.update_attributes(user_params)

    if @user.save
       redirect_to cvsindex_path
    end
end

def user_params
    params.require(:users).permit(:trig)
end

But that doesn't work. My app goes to the blank page /users/6 Do you have any idea?

Upvotes: 1

Views: 313

Answers (1)

Nitin
Nitin

Reputation: 7366

You need to change <%= bootstrap_form_for @user do |f| %> to <%= bootstrap_form_for @user, :url => '/', :method => 'PUT' do |f| %>

Your resources :users should come after put '/' => 'users#update'

<%= form_for @user %> always make path of new if @user object is new record AND update id object is already exist.

As a good practice you should not use root path like this. Because for updating you can use any path instead of root_path.

Upvotes: 3

Related Questions