Gugubaight Gugubaight - 2 months ago 20
Ruby Question

Custom controller action

I've got a button to change a database value in my rails project:

Item Show View (haml)

= link_to("", :controller => "items", :action => "set_active", :id => @item.id) do
.btn Publish


ItemsController

def show
@item = Item.find(params[:id])

def set_active
@item.is_active = true
@item.save!

flash[:notice] = "Item got published"
end
end


The View works properly, apart from the button.
If I click the button, the page just refreshes but neither the value does not change nor the flash message pops up.

Any idea why?

Thanks in advance for each answer! Please tell me if you need additional information.

Answer

Something along those lines:

# controller
def set_active
  @item.is_active = true
  @item.save!

  flash[:notice] = "Item got published"
end

# routes
resources :jobs do
  get :set_active, on: :member
end

# view
link_to 'Activete', set_active_job_path(job)
Comments