The action 'update' could not be found for JobsController + missing params

59 views Asked by At

I have been working on my startup for a week, after implementing the braintree (sandbox) I can not load params from the form http://0.0.0.0:3000/jobs/new. In the show and index action nothing is simply displayed. When I try to edit the offer http://0.0.0.0:3000/jobs/4/edit I get the error

"The action 'update' could not be found for JobsController". 

In the index (http://0.0.0.0:3000/jobs/) in the place title and description is the address from routes. Also missing is an avatar from AvatarUploader which was previously. offer from http://0.0.0.0:3000/

jobs_controller.rb

class JobsController < ApplicationController
  before_action :set_job, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

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

    end

  def set_job
     @job = Job.find(params[:id])
  end
  def index
    @jobs = Job.all
    fresh_when last_modified:
    @jobs.maximum(:updated_at)
    if params[:search]
    @jobs = Job.search(params[:search]).order("created_at DESC")
    else
    @jobs = Job.all.order("created_at DESC")
 end
 if(params.has_key?(:job_type))
    @jobs = Job.where(job_type: params[:job_type]).order("created_at desc")
  end

  # GET /jobs/1
  # GET /jobs/1.json
  def show
     @job = Job.find(params[:id])
  end

  # GET /jobs/new
  def new
    @job = current_user.jobs.build
    @job = Job.new
  end



  # GET /jobs/1/edit
  def edit
 @job = Job.find(params[:id])
  end
  # POST /jobs
  # POST /jobs.json
  def create
    @job = current_user.jobs.build(@job_params)
    job_type = params[:job_type]
    job_salary = params[:salary]
    job_title = params[:title]



       respond_to do |format|
      if @job.save
        format.html { redirect_to @job, notice: 'Your job listing was purchased successfully!' }
        format.json { render :show, status: :created, location: @job }
      else
        format.html { render :new }
        format.json { render json: @job.errors, status: :unprocessable_entity }
      end
    end


  def update
     @job = Job.find(params[:id])
   respond_to do |format|
     if @job.update(job_params)
       format.html { redirect_to @job, notice: 'Job was successfully updated.' }
       format.json { render :show, status: :ok, location: @job }
     else
       format.html { render :edit }
       format.json { render json: @job.errors, status: :unprocessable_entity }
     end
   end
 end



    def job_params
      params.require(:job).permit(:title, :description, :requirements, :url, :job_type, :location, :job_author, :remote, :apply_url, :avatar, :salary, :multisport_card, :medical_care, :cold_drinks, :parking, :job_category)
    end
end
end
end

_form.html.erb

<%= simple_form_for @job, html: { multipart: true } do |f| %>

      <div class="field">
        <div class="control">
        <%= f.input :title, required: true,input_html: { class: "input is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Tytuł stanowiska" %>
        </div>
      </div>

      <div class="field">
        <div class="control">
          <%= f.input :apply_url, required: true, input_html: { class: "input is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Adres do rekrutacji" %>
        </div>
      </div>
   <div class="number_field">
        <div class="control">
          <%= f.input :salary, required: true, input_html: { class: "input is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Wynagrodzenie ($) / rok" %>
        </div>
      </div>
<br>

    <div class="columns">
      <div class="field column is-4">
        <div class="control">
          <label class="label">Typ zatrudnienia:</label>
          <div class="control has-icons-left">
            <span class="select">
              <%= f.input_field :job_type, required: true, collection: Job::JOB_TYPES, prompt: "Wybierz typ zatrudnienia", class: "input is-rounded" %>
            </span>
            <span class="icon is-small is-left">
              <i class="fa fa-briefcase"></i>
            </span>
          </div>
        </div>
      </div>
      <div class="field column is-4">
        <div class="control">
          <label class="label">Kategoria</label>
          <div class="control has-icons-left">
            <span class="select">
              <%= f.input_field :job_type, required: true, collection: Job::JOB_CATEGORY, prompt: "Wybierz kategorie", class: "input is-rounded" %>
            </span>
            <span class="icon is-small is-left">
              <i class="fa fa-briefcase"></i>
            </span>
          </div>
        </div>
      </div>

      <div class="column">

        <div class="field">
          <div class="control">
            <%= f.input :location, required: true, input_html: { class: "input is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Lokalizacja" %>
          </div>
        </div>

      </div>
    </div>
<div class="field column">

      <div class="field">
        <div class="control">
          <%= f.input :description, required: true, input_html: { class: "textarea is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Opis" %>
        </div>
      </div>
       <div class="field">
         <div class="control">
          <%= f.input :requirements, required: true, input_html: { class: "textarea is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Wymagania" %>
        </div>
      </div>


    <div class="columns">

      <div class="field column">
        <div class="control">
          <%= f.input :job_author, required: true, input_html: { class: "input is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Firma" %>
        </div>
      </div>

      <div class="field column">
        <div class="control">
          <%= f.input :url, required: true, input_html: { class: "input is-rounded" }, wrapper: false, label_html: { class: "label" }, label: "Strona Firmy" %>
        </div>
      </div>

    </div>

    <div class="columns">
      <div class="field column is-4">
        <div class="control">
          <label class="label">Logo firmy</label>
          <div class="file">
            <label class="file-label">
              <%= f.input :avatar, as: :file, required: false, input_html: { class:"file-input is-rounded job-avatar" }, label: false, wrapper: false %>
                <span class="file-cta">
                  <span class="file-icon"><i class="fa fa-upload"></i></span>
                  <span class="file-label">Wyślij na serwer</span>
                </span>
            </label>
          </div>
        </div>
      </div>

        <div class="field">
          <div class="control">
            <label for="job[remote]">
              <%= f.input :remote, required: false, input_html: { class: "checkbox is-rounded"}, wrapper: false, label: false %>
              Praca zdalna


          </div>
           <div class="control">
            <label for="job[multisport_card]">
              <%= f.input :multisport_card, required: false, input_html: { class: "checkbox is-rounded"}, wrapper: false, label: false %>
              Karta multisport


          </div>
           <div class="control">
            <label for="job[medical_care]">
              <%= f.input :medical_care, required: false, input_html: { class: "checkbox is-rounded"}, wrapper: false, label: false %>
              Opieka medycnza


          </div>
           <div class="control">
            <label for="job[cold_drinks]">
              <%= f.input :cold_drinks, required: false, input_html: { class: "checkbox is-rounded"}, wrapper: false, label: false %>
              Zimne napoje


          </div>
           <div class="control">
            <label for="job[parking]">
              <%= f.input :parking, required: false, input_html: { class: "checkbox is-rounded"}, wrapper: false, label: false %>
              Parking


          </div>
        </div>
      <div class="column">
        <output id="list"></output>
      </div>
    </div>

    <hr />
<%= link_to 'Subscribe to add offer', new_subscription_path %>


<%= f.button :submit, class: "button is-link is-fullwidth is-rounded", value: "Dodaj ofertę pracy" %>

<% end %>

routes.rb

require 'sidekiq/web'

Rails.application.routes.draw do
  resources :jobs
  resource :subscription
  resources :meetups
  resources :posts do
    resources :comments
  end
  resources :meetups do
    resources :discussions
  end

  devise_for :users
  root to: 'welcome#index'
  get 'posts', to: 'posts#index'
  get '/jobs',   to: 'jobs#index'
  get '/team', to: 'team#index'
  get '/meetups', to: 'meetups#index'
end

repository here <---------------------------

1

There are 1 answers

2
spickermann On

I suggest that you format and indent your code properly (some IDEs even do this automatically). Because if you did, you would have noticed that you nested all methods below the index method into the body of the index method.

class JobsController < ApplicationController
  before_action :set_job, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

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

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

  def index
    @jobs = Job.all
    fresh_when last_modified: @jobs.maximum(:updated_at)

    if params[:search]
      @jobs = Job.search(params[:search]).order("created_at DESC")
    else
      @jobs = Job.all.order("created_at DESC")
    end

    if(params.has_key?(:job_type))
      @jobs = Job.where(job_type: params[:job_type]).order("created_at desc")
    end

    # GET /jobs/1
    # GET /jobs/1.json
    def show
      @job = Job.find(params[:id])
    end

    # GET /jobs/new
    def new
      @job = current_user.jobs.build
      @job = Job.new
    end

    # GET /jobs/1/edit
    def edit
      @job = Job.find(params[:id])
    end

    # POST /jobs
    # POST /jobs.json
    def create
      @job = current_user.jobs.build(@job_params)

      job_type = params[:job_type]
      job_salary = params[:salary]
      job_title = params[:title]

      respond_to do |format|
        if @job.save
          format.html { redirect_to @job, notice: 'Your job listing was purchased successfully!' }
          format.json { render :show, status: :created, location: @job }
        else
          format.html { render :new }
          format.json { render json: @job.errors, status: :unprocessable_entity }
        end
      end

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

        respond_to do |format|
          if @job.update(job_params)
            format.html { redirect_to @job, notice: 'Job was successfully updated.' }
            format.json { render :show, status: :ok, location: @job }
          else
            format.html { render :edit }
            format.json { render json: @job.errors, status: :unprocessable_entity }
          end
        end
      end

      def job_params
        params.require(:job).permit(:title, :description, :requirements, :url, :job_type, :location, :job_author, :remote, :apply_url, :avatar, :salary, :multisport_card, :medical_care, :cold_drinks, :parking, :job_category)
      end
    end
  end
end

Just fix this to the following version and your create method will be found.

class JobsController < ApplicationController
  before_action :set_job, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

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

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

  def index
    @jobs = Job.all
    fresh_when last_modified: @jobs.maximum(:updated_at)

    if params[:search]
      @jobs = Job.search(params[:search]).order("created_at DESC")
    else
      @jobs = Job.all.order("created_at DESC")
    end

    if(params.has_key?(:job_type))
      @jobs = Job.where(job_type: params[:job_type]).order("created_at desc")
    end
  end

  # GET /jobs/1
  # GET /jobs/1.json
  def show
    @job = Job.find(params[:id])
  end

  # GET /jobs/new
  def new
    @job = current_user.jobs.build
    @job = Job.new
  end

  # GET /jobs/1/edit
  def edit
    @job = Job.find(params[:id])
  end

  # POST /jobs
  # POST /jobs.json
  def create
    @job = current_user.jobs.build(job_params)

    job_type = params[:job_type]
    job_salary = params[:salary]
    job_title = params[:title]

    respond_to do |format|
      if @job.save
        format.html { redirect_to @job, notice: 'Your job listing was purchased successfully!' }
        format.json { render :show, status: :created, location: @job }
      else
        format.html { render :new }
        format.json { render json: @job.errors, status: :unprocessable_entity }
      end
    end
  end

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

    respond_to do |format|
      if @job.update(job_params)
        format.html { redirect_to @job, notice: 'Job was successfully updated.' }
        format.json { render :show, status: :ok, location: @job }
      else
        format.html { render :edit }
        format.json { render json: @job.errors, status: :unprocessable_entity }
      end
    end
  end

  def job_params
    params.require(:job).permit(:title, :description, :requirements, :url, :job_type, :location, :job_author, :remote, :apply_url, :avatar, :salary, :multisport_card, :medical_care, :cold_drinks, :parking, :job_category)
  end
end