简体   繁体   中英

rails method :delete not working

I have job model. In action show I have fontawesome icon with method: delete. When I edit job all is ok but when I click on icon with method delete nothing happens :( On the localhost its working but on production only action edit :/ When I click on the trash icon it looks like the page is only refreshing.

show.html.erb

  <% if job_author(@job) %>
 <%= link_to edit_job_path(@job) do %>
     <i class="far fa-edit" style="font-size: 30px; margin-top: 15px"></i>
     <%= link_to @job, method: :delete do %>
  <i class="fas fa-trash-alt" style="font-size: 30px; margin-top: 15px"></i>
     <% else %>

     <% end %>
<% end %>
<% end %>

jobs_controller.rb

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

  def set_job
    @job = Job.find(params[:id])
  end
  def destroy
    @job = Job.find(params[:id])
    @job.destroy
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { head :no_content }
    end
  end
  def index
    @jobs = Job.all
      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
  if(params.has_key?(:job_category))
      @jobs = Job.where(job_category: params[:job_category]).order("created_at desc")
  end
  end

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


  def new
    @job = current_user.jobs.build
    @job = Job.new
  end

  def edit
    if current_user.id == @job.user.id
      @job = Job.find(params[:id])
  else
      flash[:danger] = "You do not have authorization to edit this post"
      redirect_to root_path
    end
  end

  def create
      @job = current_user.jobs.build(job_params)
      job_type = params[:job_type]
      job_salary = params[:salary]
      job_title = params[:title]
      job_category = params[:job_category]
      job_technologies = params[:technologies]
      job_additional_technologies = params[:additional_technologies]
      job_data = params[:data]
      job_godzina = params[:godzina]
      respond_to do |format|
        if @job.save
            format.html { redirect_to 'https://commerce.coinbase.com/checkout/9d7f7bae-db41-4128-b0cd-b2c73b5585d8'  }
        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 }
       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, :technologies, :additional_technologies, :data, :godzina)
  end
end

you are putting the delete link inside the edit link so when clicking it clicks the edit

I think it should be like this

    <% if job_author(@job) %>
      <%= link_to edit_job_path(@job) do %>
         <i class="far fa-edit" style="font-size: 30px; margin-top: 15px"></i>
      <% end %>
      <%= link_to @job, method: :delete do %>
       <i class="fas fa-trash-alt" style="font-size: 30px; margin-top: 15px"> </i>
     <% end %>

    <% else %>

    <% end %>

Maybe take a look at your asset pipeline? If it's working locally but not in production, maybe some JS code is missing.

https://github.com/rails/rails/issues/24459

First, it should be outside the edit tag.

Secondly, I think it should be as follows:

<%= link_to 'Destroy', job_path(@job), method: :delete do %>
    <i class="fas fa-trash-alt" style="font-size: 30px; margin-top: 15px"></i>
<% end %>

ie Try making the path job_path(@job) not just @job .

Kindly, please, I do not understand, why you are putting that line of code in the new action: @job = Job.new . You already defined @job = current_user.jobs.build .I am just little confused about that.Can remove that line and then try again.I think you are defining it two times.

You defined the :destroy method in your controller, not :delete . Plus, your indentation and nesting are incorrect. Try this:

<% if job_author(@job) %>
    <%= link_to edit_job_path(@job) do %>
        <i class="far fa-edit" style="font-size: 30px; margin-top: 15px"></i>
    <% end %>
    <%= link_to destroy_job_path(@job), method: :delete do %>
        <i class="fas fa-trash-alt" style="font-size: 30px; margin-top: 15px"></i>
    <% end %>
<% else %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM