rails 7 error in build statement controller action - ruby-on-rails

The error occurs on the next line after "... do |ip|"
def edit
ItemProperty.where(item_type_id: #item.item_type_id).each do |ip|
#item.item_item_properties.build(item_property_id: ip.id) unless #item.item_item_properties.find_by(item_property_id: ip.id)
end
end
I expected the form fields to become populated with data on get edit request the first time, without having to refresh the page to see the fields get populated with values

Related

Rails keep and display model method errors

I have a model in rails for movies and within this model I have a method that parses JSON and adds each movie from that to a database.
My controller so far has an index to list the movies and it has a create action that triggers the model method that parses the JSON.
def create
#movie = Movie.parse_json
if #movie
redirect to root_path
else
render new_movie_path
end
the parse json method just parses each json element and adds it as a new movie.
My main predicament is I think I'm doing things a little wrong and so it's becoming difficult. I want to have it so if for whatever reason a movie can't save - it adds an error and moves to the next. After the JSON has been fully parsed I can then see which movies encountered errors. I've tried doing errors.add(:base, "Movie #{movie.title} can't be saved" .. and then in my movie/new.html.erb having a loop of the errors but #movie is undefined there.
So far to trigger my create action I have a button on my new.html.erb that sends a post request to trigger the create action and so parse the JSON.
<%= link_to "Get New Movies", movies_path, method: :post %>
Which parts am I doing wrong that is making it difficult. I want it so from my new movie page I can click a button and this triggers movie parsing - at the end of parsing if there are errors I can display them - else just redirect to the index page. I could do this if my controller just made a single object but the whole model method parsing json and creating multiple records is the thing that is throwing me off.

Why does validation error appear on the form after successful submit and going back?

I'm using Rails 4 and Chrome. The following results in the situation:
Post a form that causes a validation error (ie. "Name cannot be empty")
Post that same form successfully by correcting the input
Hit the browser back button and the validation error from step 1 is shown on the input field even though it has a value that is not empty
Why does the validation error from the step 1 pop back and how to fix this behaviour? Note: Turbolinks is in use, could that be the reason?
Here's the way to replicate:
rails g scaffold Page name:string
class Page < ActiveRecord::Base
validates :name, presence: true
end
Navigate to /pages/new
Submit (errors appear on the form)
Fillout the name
Submit again (redirected to successfully created model)
Hit the browser back button (the validation errors are there, and the field is filled with the last supplied value)
I guess you are using something like the following code to send back the errors related to the record being created/updated:
def update
#post = Post.find(params[:id])
if #post.update_attributes(post_params)
# your logic when successfull
else
render :edit, flash[:errors] = #post.errors
end
end
Or something similar. My point here is that you should use the following syntax for setting the errors in the flash :
flash.now[:errors] = #post.errors
(1) This should set the flash[:errors] available only for the current page and delete it right after you leave this page.
(2) You could also use flash.clear at the end of your view, but this is not how it is supposed to be done, and seems a little bit "hacky".
Resources:
(1) Rails' documentation about flash
(2) flash.clear method
(1) & (2) Why flash message won't disappear?

On re-submit of form with errors, a PATCH request is called when it's supposed to be a POST

Something very funny is happening... my controller has two checks that are made separately before it allows the form to be submitted (the code below is simplistic, there are other things happening). If either checkpoint fails, the new page is re-rendered.
Here's the code:
def create
if checkpointone == fail
render 'new'
else
if checkpointtwo == fail
render 'new'
else
redirect_to action: 'success'
end
end
end
Here's the funny flow problem I'm running into:
user enters data that fails checkpointone and passes checkpointtwo
user submits
new page successfully re-rendered with original parameters and error message
user still enters incorrect data (either checkpointone fails again, or it passes but checkpointtwo fails)
user submits
application fails with an error No route matches [PATCH] "/requests"
But why on earth is it looking for a PATCH all of the sudden? Who told it to? The weird thing is that if I start with a fail in checkpointtwo and a pass in checkpointone, from then on, I can make any number of fails and resubmissions in various combinations, and I'll always get the correct action: new page re-rendered with original parameters and error message.
Snippet of view code:
<%= form_for #requestrecord, :url => { action: 'create' }, :html=> {:id => 'form'} do |f| %>
Snippet of routes code:
get 'requests/new', to: 'requests#new', as: 'new_request'
post 'requests', to: 'requests#create'
In Rails 4, PATCH is used for updates. Could it be that your record is being created even when a checkpoint fails. This would cause Rails to think that the subsequent request is an update request, rather than create request.

Rails nested form / resource > now not nested and broken

Followed the getting started Rails guide (blog with comments) and have adjusted for my models (releases/products)
As the nested form was getting a bit cumbersome (products for me / comments in the guide), I decided to move it to it's own view. I can render the form correctly and view/update existing records without issue. However, when I try to submit a new product I get an error saying "Couldn't find Release without an ID".
I think the answer lies in the product controller, it seems to me that it's not receiving the release ID that I can see in the URL when the form is rendered, i.e. /releases/18/products/new but on submit the URL showing the error just: /products
# ProductsController
def create
#release = Release.find(params[:release_id])
#product = #release.products.create(params[:product])
redirect_to release_path(#release)
end
Ideally i'd like it to sumbit and the redirect back to the release show view as it was when nested.
Any ideas?

How to use form_remote_tag together with validations?

I would like to have Ajax form in Rails so i'm using form_remote_tag. The field
i would like to submit is email address - how can i use the Rails validations together
with form_remote_tag?
We're using form_remote_tag with the :update option and paste the whole form into a partial.
Once the form is submitted and validation fails, the partial is being rendered again and all error messages will show up.
This is the workflow:
Call "edit" action
Display form from a partial, use form_remote_tag with :update option
Submit form to "update" action
Validate data
Validation failed? Display partial again with error messages
Validation passed? Display form with some success message
In your form processing controller action decide whether
you load flash[:notice] with
validation errors and lead the user
back to the form view
OR
let her go ahead and process the valid data,
(leading to the next step)
You can (should) make this decision based on validations
def create
m = Model.new(params)
if m.valid?
m.save
#load flash with succes message
else
#load flash with error messages from m.errors
#render the form again
end
end

Resources