为模型实现编辑/更新功能的问题



当我尝试编辑工作模型的实例时,我要更新的属性设置为nil。

我尝试使用常规form_for助手而不是simple_form,因为我不知道Simple_form是否需要额外的信息,例如使用哪种操作和方法,但它不起作用。

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'

没有错误,但是属性为nil,并且在索引视图中显示url而不是 @job.title(因为其nil(

我相信表单数据包裹在params中的键:job中,因此Job的属性需要白色列入

  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

最新更新