Ive currently created an app that is meant to mimic fiverr, im currently trying to create a registration form for users who are currently active on the site, as freelancers, this is being handled using the Devise gem on ruby. Im having a problem when posting my form however, as it keeps coming back saying it couldnt find the user without an ID. Here below is my form
```ruby
<%= form_with model: @user do |form|%>
<%= form.text_field :first_name, placeholder: "First Name"%>
<%= form.text_field :last_name, placeholder: "Last Name"%>
<%= form.text_field :bio, placeholder: "Bio"%>
<%= form.number_field :cost, placeholder: "Cost per hour in $'s"%>
<%= form.submit %>
<%end%>```
and then here is my pages_controller
```ruby
class PagesController < ApplicationController
def home
end
def registration_form
end
def edit
@user = User.find(params[:id])
end
def register_freelancer
@user = User.find(params[:id])
@user.update(seller_params)
current_user.freelancer!
redirect_back(fallback_location: root_path)
end
private
def seller_params
params.require(:user).permit(:first_name, :last_name, :bio, :cost)
end
end
```
Here is my routes
```ruby
Rails.application.routes.draw do
root 'pages#home'
devise_for :users
resources :pages
get '/home' => 'pages#home'
get '/registration_form' => 'pages#registration_form'
post 'registration_form' => 'pages#register_freelancer'
end
and here is a screenshot of my table
[table][4]
Im using ruby on rails 7.0, and a postgres database, please can someone advise me what step im missing out on to update these database?
[1]: https://i.stack.imgur.com/z4cnn.png
[2]: https://i.stack.imgur.com/O5zDc.png
[3]: https://i.stack.imgur.com/Xrxc8.png
[4]: https://i.stack.imgur.com/hwxyw.png
CodePudding user response:
Here you need to find the user in registration_form
action, I think this should work
class PagesController < ApplicationController
before_action :authenticate_user!
before_action :set_user, only: [:edit, :registration_form]
def home; end
def registration_form; end
def edit; end
def register_freelancer
@user.update(seller_params)
current_user.freelancer!
redirect_back(fallback_location: root_path)
end
private
def set_user
@user = current_user
end
def seller_params
params.require(:user).permit(:first_name, :last_name, :bio, :cost)
end
end
Then update the form to this
<%= form_with(model: @user, url: registration_form_path), method: :post do |form|%>
<%= form.text_field :first_name, placeholder: "First Name"%>
<%= form.text_field :last_name, placeholder: "Last Name"%>
<%= form.text_field :bio, placeholder: "Bio"%>
<%= form.number_field :cost, placeholder: "Cost per hour in $'s"%>
<%= form.submit %>
<%end%>
CodePudding user response:
Though this is not correct approach how you are trying to update user fields, but here is what you should do to make it works:
Since you have defined these two routes:
get 'registration_form' => 'pages#registration_form'
post 'registration_form' => 'pages#register_freelancer'
These routes don't suppose to have user ID in the URL as a fragment so that you need to pass it explicit:
# add ?id=1 to your route
http://localhost:3000/registration_form?id=1
And then change your action and form:
def registration_form
@user = User.find(params[:id])
end
<%= form_with model: @user, url: registration_form_path(id: @user.id), method: :post do |form|%>
<%= form.text_field :first_name, placeholder: "First Name"%>
<%= form.text_field :last_name, placeholder: "Last Name"%>
<%= form.text_field :bio, placeholder: "Bio"%>
<%= form.number_field :cost, placeholder: "Cost per hour in $'s"%>
<%= form.submit %>
<%end%>
Update:
So that your routes support user ID as a fragment you need to change your routes:
resources :users do
member do
get 'registration_form' => 'pages#registration_form'
post 'registration_form' => 'pages#register_freelancer'
end
end
After this change your will have these URLs:
registration_form_user GET /users/:id/registration_form(.:format)
registration_form_user POST /users/:id/registration_form(.:format)
So you need to change your paths from registration_form
to registration_form_user(USER_INSTANSE)
. You need to replace USER_INSTANSE
to user instance you want to fill out registration_form
And change your form
<%= form_with model: @user, url: registration_form_user_path(@user), method: :post do |form|%>
<%= form.text_field :first_name, placeholder: "First Name"%>
<%= form.text_field :last_name, placeholder: "Last Name"%>
<%= form.text_field :bio, placeholder: "Bio"%>
<%= form.number_field :cost, placeholder: "Cost per hour in $'s"%>
<%= form.submit %>
<%end%>