Rails Bad Request Error
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 actioncontroller::badrequest Overflow the company Business Learn more about hiring developers or posting ads with us Stack rails raise bad request Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community rails raise 400 error of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Trying to get a POST to return 400 bad request up vote 14 down vote favorite 2 rails 400 bad request I have a create method that builds a new model through an association and I was expecting it to return a 400 response with some text if no params were in the POST request. However, I get an error. This is in Rails 4.0.2 controller methods: def create @cast_profile = current_user.build_cast_profile(cast_profile_params) if @cast_profile.save redirect_to cast_profile_path else render :edit end end def cast_profile_params params.require(:cast_profile).permit(:name, :email, :public) end If I pass the params
Actioncontroller Exceptions
its all fine but I'm trying to test the bad request scenario. Here's the error: ActionController::ParameterMissing: param not found: cast_profile I could rescue it explicitly but I thought strong parameters was supposed to do that automatically. ruby-on-rails-4 strong-parameters share|improve this question asked Dec 28 '13 at 3:21 kjs3 1,18931332 add a comment| 1 Answer 1 active oldest votes up vote 26 down vote accepted The behaviour is as follows: Handling of Unpermitted Keys By default parameter keys that are not explicitly permitted will be logged in the development and test environment. In other environments these parameters will simply be filtered out and ignored. Additionally, this behaviour can be changed by changing the config.action_controller.action_on_unpermitted_parameters property in your environment files. If set to :log the unpermitted attributes will be logged, if set to :raise an exception will be raised. (source) I would suggest rescuing from this exception with 400 status: rescue_from ActionController::ParameterMissing do render :nothing => true, :status => 400 end share|improve this answer answered Dec 28 '13 at 3:28 Bartosz 1,6601019 I'm not having an issue with the handling of unpermitted keys. I'm having an issue with required keys. Further up in the doc you linked it says… –kjs3 Dec 28 '13 at 15:04 In addition, parameters can be marke
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
Rails Render Bad Request
About Us Learn more about Stack Overflow the company Business Learn more about rails completed 400 bad request hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join actioncontroller::parametermissing the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up 400 bad request error http://stackoverflow.com/questions/20810660/trying-to-get-a-post-to-return-400-bad-request when using jQuery.post up vote 0 down vote favorite I am new to Rails and is trying jQuery.post to post a form using ajax and show the results. But I'm only getting this in my console when I click submit: POST http://localhost:3000/main/newStuff 400 (Bad Request) I am looking at the example at the bottom of this jQuery page. Here is my ajax code: $( "#new_stuff_form" ).submit(function( http://stackoverflow.com/questions/25691716/400-bad-request-error-when-using-jquery-post event ) { // Stop form from submitting normally event.preventDefault(); // Get some values from elements on the page: var $form = $( this ), title = $form.find( "#new_stuff_title" ).val(), url = $form.attr( "action" ); // Send the data using post var posting = $.post( url, { title: title } ); posting.done(function( data ) { console.log(data); }); posting.fail(function( data ) { console.log(data); }); }); Here is my controller: def createStuff @stuff = Stuff.new(stuff_params) @stuff.save render json: {status: "ok"} end private def stuff_params params.require(:stuff).permit(:title, :due_date) end And here is my view: <%= simple_form_for @stuff, url: stuff_create_path, html: {id: "new_stuff_form",class: "form-horizontal", role: "form"} do |f| %>
Bad Request, Forbidden and Not Found errors in Rails?In your app, there are lots of places when invalid https://www.quora.com/What-is-the-best-way-to-handle-Bad-Request-Forbidden-and-Not-Found-errors-in-Rails input from the user would result in errors - such as when there is a post to a create method on a comment controller when the user is not logged in. How should the controller behave, in your opinion? Right now, I'm just doing this:else render :text => "403 Forbidden", :status => :forbidden end .. but bad request it doesn't feel ideal.UpdateCancelAnswer Wiki2 Answers Tim Morgan, Ruby on Rails developer, SquareUpdated 278w agoUse respond_to and choose more appropriate behavior for each response type:respond_to do |format| format.html { redirect_to root_url, alert: "You cannot access that part of the website." } format.xml { head :forbidden } format.json { head :forbidden } end 3.4k Views · View 400 bad request UpvotesRelated QuestionsMore Answers BelowHow to I test for 403 Forbidden response code in Rails testing?In Rails, what is the best way to return a RecordNotFound Error from a Model.find call in JSON?What is the best strategy to handle exceptions and errors in Rails?What's the best way to handle a multiple domain-based internationalized Rails site that uses HTTPS?How does Rails handle multiple requests from different users without colliding? What is the logic? Dilip ChouhanWritten 81w agoCustom Error Pages with Rails 4It is nice way to handle errors and custom error pages for rails 41.1k ViewsView More AnswersRelated QuestionsWhat is the best way to handle custom error pages in Rails 3?What is the best way to handle file manipulation (themes) by users in a Rails application?What is the best way to handle multiple versioned TOS documents in a Ruby on Rails application?What's the best way for a beginner to start learning Ruby on Rails?How do I get good at ruby on rails?Are there any free or cheaper alternatives