Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4ruby-on-rails-5

Is there a way I can have only action without view


In my view, there is a link_to to a named route like random_path. So when the user clicks on that link, it will go to a particular controller with an action, in which I am writing to a DB. It really does not need to render the view. But having the view template seems to be mandatory. Is there a way to avoid having the view.

<%= link_to bla_path do %>

<% end %>

in routes.rb

get 'bla' => 'contr#act'

In controller

in cont_controller.rb

def act 

Model.create(name: "bla")

# I don't need the view for this. 

end 

Solution

  • The way http protocol works is that you have to respond to each request. However, you can respond with a blank message, in rails the easiest way to do this is to add:

    head :ok
    

    anywhere in your action.