3

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 
techdreams
  • 5,371
  • 7
  • 42
  • 63
gates
  • 4,465
  • 7
  • 32
  • 60
  • Possible duplicate of these : http://stackoverflow.com/questions/9533310/ruby-on-rails-action-without-view, http://stackoverflow.com/questions/12919631/rails-controller-action-without-a-view, http://stackoverflow.com/questions/8830017/rails-3-having-action-in-controller-without-view. I haven't just marked as such as it'll close it immediately. – Shadwell Feb 18 '16 at 12:25
  • I think there is a misunderstanding. The links you have given are not answering the question – gates Feb 18 '16 at 12:37
  • It totally depends what you intend to do after running this query. Whether to refresh page or not or anything else. – techdreams Feb 18 '16 at 12:43
  • @gates That's fine if they don't and that's why I posted them as suggestions instead of just closing it myself. I think the suggested answers in those to use `render nothing: true` or that you might need to redirect elsewhere are perfectly valid in this case but you obviously don't! – Shadwell Feb 18 '16 at 13:22

4 Answers4

5

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.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
5

You can also write your action.

def act 
  Model.create(name: "bla") 
  render :nothing => true
end 

In case you need to return back to the same action just write

def act 
  Model.create(name: "bla") 
  redirect_to :back
end
techdreams
  • 5,371
  • 7
  • 42
  • 63
4

Yes you can just pass render json: nil, status: :ok example code:

def act 
  Model.create(name: "bla") 
  render json: nil, status: :ok
end 
kajal ojha
  • 1,248
  • 8
  • 20
2

The controller's action needs to send back some kind of http response to the browser. If you reach the end of your action method (and any filters) without calling a method that sends a response, such as 'render' or 'head', the default behaviour is to look for a view template to render, with the same name as the action.

What @kajal suggested is to call 'render' to produce a response with minimal content and a 200 (OK) status code. That seems like a reasonable approach, although you could equally well render an empty string or a string saying that the operation was successful, by doing:

render plain: "Ok"

You said "But having the view template seems to be mandatory", but you don't explain why you think this. Are you getting some kind of error message? If so, could you say what it is?

sheltond
  • 1,877
  • 11
  • 15