1

When I try to edit an instance of my Job model, the attributes I'm trying to update are set to Nil.

I've tried using the regular form_for helper instead of simple_form because I didn't know if simple_form required extra info such as what action and method to use, but it didn't work.

edit.html.erb

<h1>Edit Job:</h1>
<br>
<%= simple_form_for @job do |f| %>
  <%= f.input :title, label: "Job title" %>
  <%= f.input :description, label: "Description" %>
  <%= f.button :submit %>
<% end %>

jobs_controller.rb

  def edit
    @job = Job.find(params[:id])
  end

  def update
    @job = Job.find(params[:id])
    @job.update(title: params[:title], description: params[:description])

    if @job.save
      redirect_to jobs_path(@job)
    else
      render "edit"
    end
  end

routes.rb

  resources :candidates
  resources :tenants, constraints: { subdomain: 'www' }, except: :index
  resources :jobs, path_names: { new: 'add' }
  get 'candidates/index'
  get 'candidates/new/:id' => 'candidates#new', :as => 'apply'
  get 'candidates/single/:id' => 'candidates#single', :as => 'view_candidate'
  get 'jobs/single/:id' => 'jobs#single', :as => 'single_job'
  get 'add-job' => 'jobs#new'
  get 'listings' => 'jobs#listings', :as => 'career_page'
  get 'listing/:id' => 'jobs#listing', :as => 'view_job'
  get 'welcome/index', constraints: { subdomain: 'www' }
  get 'dashboard' => 'tenants#dashboard', as: 'dashboard'
  constraints SubdomainConstraint do
    devise_for :users, path_names: { edit: 'account' }
    root 'tenants#dashboard'
  end

  root 'welcome#index'

No error, but attributes are Nil, and URL is displayed instead of @job.title in the index view (because its Nil)

1 Answers1

1

I believe the form data are wrapped into key :job in params so attributes for Job need to be whitelisted

  def update
    @job = Job.find(params[:id])
    @job.update(job_params)

    if @job.save
      redirect_to jobs_path(@job)
    else
      render "edit"
    end
  end

Private
  def job_params
    params.require(:job).permit(:title, :description)
  end
Anand
  • 6,457
  • 3
  • 12
  • 26
  • @user2570506 Feel free to upvote in it helped you as well as for future SO searchers. Thanks – Anand May 14 '19 at 03:33