Error Jbuilder
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about
Jbuilder Status Code
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask jbuilder rails Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Http Status Codes
Join them; it only takes a minute: Sign up Rails proper way to display error with jbuilder up vote 6 down vote favorite 3 I am looking to display an error message in a jbuilder view. For instance, one route I might have might be: /foos/:id/bars If :id submitted by the user does not exist or is invalid, I'd like to be able to display the error message accordingly in my index.json.builder file. Using Rails, what's the best way to get this done? The controller might have something such as: def index @bar = Bar.where(:foo_id => params[:id]) end In this case, params[:id] might be nil, or that object might not exist. I'm not sure whether the best thing to do here is handle it in the controller and explicitly render an error.json.builder, or handle it in the index.json.builder view itself. What's the correct way to do this and if it's in the index.json.builder, is params[:id] available to check there? I know I can see if @bar.nil? but not sure on the inverse? ruby-on-rails ruby json jbuilder share|improve this question edited Apr 9 '14 at 14:17 Jeff 6,55953573 asked Apr 4 '13 at 20:06 randombits 6,78641149285 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote I would render index.json.builder or just inline json with :error => 'not found' And don't forget to set proper HTTP status: :status => 404 So result could look like this: render :json => { :error => 'not found' }, :status => 422 if @bar.nil? share|improve this answer answered Apr 4 '13 at 22:27 Shkarik 4321411 add a comment| up vote 4 down vote I think you meant show, since index is really for lists/collections. And you should get .first on the where, otherwise you just have a relation, right? Then, use .first! to raise an error, because Rails' Rack middleware in Rails 4 public_exceptions will handle is in a basic fashion, e.g. def show # need to do to_s on params value if affected by security issue CVE-2013-1854 @bar = Bar.where(:foo_id => params[:id].to_s).first! end You can also use @bar = Bar.find(par
Sign in Pricing Blog Support Search GitHub This repository Watch 86 Star 2,747 Fork 258 rails/jbuilder Code Issues 19 Pull requests 8 Projects 0 Pulse Graphs New issue Weird error reporting when an error occur in a JBuilder partial #40 Closed Sephi-Chan opened this Issue Apr 28, 2012 · 13 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 9 participants Sephi-Chan commented Apr 28, 2012 Hi, I noticed two errors in the reporting of errors with jbuilder partials rendered from a view. The first is weird but not a big deal, http://stackoverflow.com/questions/15820714/rails-proper-way-to-display-error-with-jbuilder the second is really problematic for debugging. I have a view and two partials. In views/application/home.html.haml, I have: - json = render(partial: 'users/user', formats: [:json], locals: { user: @user }) = javascript_tag { "window.data = {}; window.data = #{raw(json)};" } In views/users/_user.html.haml, I have: = user.undefined_method_called_from_haml_template In views/users/_user.json.jbuilder, I have: json.name user.undefined_method_called_from_jbuilder_template Displaying the page raises: NoMethodError in Application#home Showing ~/MyTest/app/views/users/_user.html.haml where line # raised: undefined method `undefined_method_called_from_jbuilder_template' for https://github.com/rails/jbuilder/issues/40 #User:0x007fb2bea96de8 The reported file is wrong: it should be the JSON version. So now, the second error is visible when there is no other version of the same partial (in my case, by removing _user.html.haml). ActionView::MissingTemplate in Application#data Showing ~/MyTest/app/views/application/home.html.haml where line #1 raised: Missing partial users/user with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :jbuilder, :coffee, :haml]}. What the hell? Why does any errors occurring in the partial are ignored and reported as a missing template (plus, the formats and handlers are not correct? If I fix the error (not easy with this weird report) in the template, it works! Any idea? brandoniscool commented Apr 28, 2012 +1 ariera commented Oct 16, 2012 +1 fsainz commented Oct 16, 2012 +1 seanbehan commented Nov 16, 2012 Ouch. Just got bit by this one! rwz commented Dec 25, 2012 This is a rails bug. I'm trying to fix it here: rails/rails#8406 rwz closed this Jan 1, 2013 earnold commented May 2, 2015 I am seeing this again in rails 3.2.21 - is there any known workaround? earnold commented May 2, 2015 I've created a small demo app to precisely demonstrate the problem: https://github.com/earnold/error-demo When there is an error in the jbuilder template, you get a missing te
Sign in Pricing Blog Support Search GitHub This repository Watch 86 Star 2,747 Fork 258 rails/jbuilder Code Issues 19 Pull requests 8 Projects 0 Pulse Graphs New issue https://github.com/rails/jbuilder/issues/225 rescue in .json.jbuilder file #225 Closed muichkine opened this Issue Oct 30, 2014 https://github.com/rails/jbuilder/issues/76 · 2 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 2 participants muichkine commented Oct 30, 2014 Hello, I have written some generic logic to render my json objects inside some .json.jbuilder files. Problem is sometimes, it calls a template that I status code have not defined willingly. In such cases, of course render throws an ActionView::Template::Error for not finding the template. I wish I could do in my .json.jbuilder files: begin json.partial! "api/v1/#{node}", object: object rescue ActionView::Template::Error => e logger.debug e.message end but the error keeps being thrown for reasons I fail to understand. For my purpose, I changed the writing to if Rails.root.join('app', 'views', "api/v1/_#{node}.json.jbuilder").exist? json.partial! jbuilder status code "api/v1/#{node}", object: object end which yields the desired overall behavior. I simply wish I could instead rescue the error. :) rwz commented Nov 2, 2014 The error being thrown is not ActionView::Template::Error, it's ActionView::MissingTemplate Also, you have full access to view helpers, which you could use to abstract the logic to. Something like this: # app/helpers/json_api_helper.rb module JsonApiHelper def render_partial_if_exists(json, partial_name, *args) if lookup_context.exists?(partial_name, [], true, [], formats: [:json], handlers: [:jbuilder]) json.partial! partial_name, *args else logger.debug "attempted to render #{partial_name}, which doesn't exists" end end end and then use it from Jbuilder template: # app/views/my_view.json.jbuilder render_partial_if_exists json, "api/v1/#{node}", object: node rwz closed this Nov 2, 2014 muichkine commented Nov 3, 2014 Thanks a lot @rwz, that'll do perfect. Much appreciated. :) Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Sign in Pricing Blog Support Search GitHub This repository Watch 86 Star 2,747 Fork 258 rails/jbuilder Code Issues 19 Pull requests 8 Projects 0 Pulse Graphs New issue Error with jbuilder 0.8.2 : "missing attribute: photo_updated_at" #76 Closed youboox opened this Issue Nov 5, 2012 · 1 comment Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 2 participants youboox commented Nov 5, 2012 Hi, After updating my Rails 3.1 to 3.2 i have this error in one of my jbuilder partial : Failure/Error: get :show, :id => @user.to_param, :format => :json ActionView::Template::Error: missing attribute: photo_updated_at # ./app/views/api/user_ipads/_user_ipad.json.builder:11:in block in _app_views_api_user_ipads__user_ipad_json_builder__4322258879011176669_70322613825420'
# ./app/views/api/user_ipads/_user_ipad.json.builder:1:in_app_views_api_user_ipads__user_ipad_json_builder__4322258879011176669_70322613825420' # ./app/views/api/user_ipads/show.json.jbuilder:1:in _app_views_api_user_ipads_show_json_jbuilder___1496169294239287635_70322552739840'
# ./spec/controllers/api/user_ipads_controller_spec.rb:56:inblock (4 levels) in ' This is very strange since my model has the photo_updated_at column, and i can access it without problem outside the jbuilder partial. Here is the line that create the issue : Line 11 : json.photo @user_ipad.photo.url json.user_ipad do |json| json.id @user_ipad.id json.is_premium @user_ipad.is_premium json.email @user_ipad.email json.authentication_token @user_ipad.authentication_token json.pseudo @user_ipad.pseudo json.first_name @user_ipad.first_name json.last_name @user_ipad.last_name json.male @user_ipad.male json.birthday @user_ipad.birthday json.photo @user_ipad.photo.url end Do you have any idea where it could come from ? Thanks, Vincent Ruby on Rails member dhh commented Nov 27, 2012 I don't see it in your code example. I don't think this relates to jbuilder itself. But if you can replicate it in a failing test, please reopen. dhh closed this Nov 27, 2012 Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh