teresa
teresa

Reputation: 366

Rails update method not saving form data

I'm trying to go from edit form page to preview page which displays the updated information. Currently, I added update method to the controller which re-directs (when the form is submitted) to the preview page. But on the preview page, nothing is updated and the data is not saved to the database.

jobs_controller.rb

def update 
  @job = Job.find(params[:id])
  redirect_to preview_path(id: @job.id), notice: 'Job was successfully updated.'
end  

jobs/preview.html.erb

<span class="col-md-12 col-centered">
  <%= link_to 'Edit', edit_job_path(@job), :class => "jobBack-btn" %>
  <%= link_to "Publish",'#', id:'link_id', class: "btn" %>
</span>

If I remove the update method, the edit form saves and redirects to show page (by default). I have to go to the preview page to see the changes. I'm not sure what I'm missing in the update method to save the form data.

Upvotes: 0

Views: 1606

Answers (2)

teresa
teresa

Reputation: 366

I updated my code and now it updates and redirects successfully thanks to the comments. I am on a older version of Rails (Rails 4.0.8).

jobs_controller.rb

def update 
  @job = Job.find(params[:id])
  if @job.update(params.require('job'))
    redirect_to preview_path(id: @job.id), notice: 'Job was successfully updated.'
  else
    redirect_to '/jobs'
  end
end

Upvotes: 0

James Milani
James Milani

Reputation: 1943

Have you tried actually updating the object in update method?

def update 
  @job = Job.find(params[:id])
  if @job.update(update_params)
    redirect_to preview_path(id: @job.id), notice: 'Job was successfully updated.'
  else
    redirect_to somewhere_path, notice: @job.errors.full_messages.join "\n"
  end
end

private

def update_params
  params.require(:job).permit( ... )
end

Right now it looks like you're simply finding the job and then redirecting.

Upvotes: 1

Related Questions