Siddharth Ram
Siddharth Ram

Reputation: 576

how does a button_to in rails request a JSON response?

Im a rails noob, so bear with me.

I have the following in new.html.erb:

<h1>New page</h1>

<%= render 'pages/form' %>

<% if @page != 1 %>
<%= button_to 'New Page', pages_path %>
<%= button_to 'Done!', :action=> 'generate' %>

<% end %>

my controller looks like this

def generate
@presentation = current_presentation
respond_to do |format|
format.json { render json: @presentation.pages}
format.html {render :text => "html"}
end

end

I want the response to be JSON, but the code goes to format.html.

How do I instruct the button_to that my requested response is JSON?

Upvotes: 4

Views: 1339

Answers (2)

Jon Garvin
Jon Garvin

Reputation: 1188

Using a path helper to build the path worked for me. eg.

<%= button_to "Create Widgit", widgits_path(format: 'json'), remote: true %>

Upvotes: 1

Pedro Nascimento
Pedro Nascimento

Reputation: 13886

Not sure if just one of them or both, but try :remote => true and data-type:

<%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>

Upvotes: 2

Related Questions