1

i have a simple application to snooker league and i have a action add_player in LeaguesController. My client (AngularJS) call this action and i want to respond with json format using JBuilder. This working fine:

 def add_player
   ...

   render :json => @league.players.to_json(:only => [ :id, :firstname, :lastname, :max_break, :email ])
end

But when i delete this line and add:

respond_to do |format|
  format.html 
  format.json 
end

i have this error:

ActionView::MissingTemplate (
  Missing template leagues/add_player, 
  application/add_player 
  with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb,    :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}. 
  Searched in:
* "/home/daniel/SitesWWW/snookerLeague/app/views"
):
app/controllers/leagues_controller.rb:67:in `add_player'

Of course i have file add_player.json.jbuilder:

json.players @league.players do |player|
  json.id player.id
  json.firstname player.firstname.capitalize if player.firstname
  json.lastname player.lastname.capitalize if player.lastname
  json.email player.email
  json.max_break player.max_break
end

So, what i should do?

1 Answer 1

1

Notice in your error message :formats=>[:html] this indicates to me it is trying to render add_player.html.erb and not your jbuilder template.

To verify this, try changing the calling code and append .json to the url which will force the format to be json.

You can also specify a default of json on your routes and not include it in the url:

resources :leagues, defaults: { format: :json } do
  member do
    post :add_player
  end
end

see also: http://guides.rubyonrails.org/routing.html#defining-defaults

Sign up to request clarification or add additional context in comments.

1 Comment

Change routes helps. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.