We are currently coding as follows, and before rendering a certain page, we put the data narrowed down by the condition from the table into @posts and use it in show.html.erb.
When a certain button is pressed on that page, the POST process is performed and the xxxx method is called, but when saving to the table, it passes validation, and if it succeeds, it redirects, and the error content on failure It is a flow of passing and re-rendering.
After processing the current error, @posts is not passed just before rendering, so an error has occurred.
Is it possible to reuse the @posts passed during the show for the @posts passed here?
In that case, pass all the search conditions for where
@posts = Post.where (Filter by various conditions depending on parameters)
Will it be a flow to write again?
Because this page performs post processing in various places, you have to write a lot of this processing after error validation, so I thought it would be nice if you could easily reuse @posts from a previous show. It was.
It can be a useful site, so please let me know the appropriate method.
def show @posts = Post.where (Narrow down by various parameters) render 'homes/show' end def xxxx @aaaa = XXXX.new (name: xxx, yyy: params [: yyy], zzz: params [: zzz]) if @ aaaa.save redirect_to controller: 'homes', action: 'show' else # Error handling # I want to use @posts here with the same conditions as the one passed in show # Do I have to pass the same parameters as the search condition when posting and do the following again? # @posts = Post.where (Filter by various conditions depending on parameters) render 'homes/show' end end # show.html.erb <% @ posts.each do | post |%> Various processing <% end%>
Amazon Linux release 2 (Karoo)
Ruby version: 2.6.1
Answer # 1
You cannot reuse @post.
Instance variables starting with @ can only be used within that instance. It is possible to make it accessible from outside the instance, but it disappears when the instance runs out.
An instance of the controller is created by a request from the browser and will disappear when the request is answered. Requests made with show disappear when show output ends, so @post is also gone.
- ruby - do that row each time the rails model is updated
- ruby - pass the value from the controller in the rails model
- ruby on rails - the link between the model created by devise and the model originally created does not work
- ruby on rails - rails test does not generate a model
- ruby - [rails] i want to find the date difference using the model created_at
- ruby on rails 5 - child model record not created
- ruby on rails - i can't create a model with rails
- ruby - unable to create rails model (environment: cloud9)
- ruby - change rails model name how to change yml testrb name
- [ruby on rails] i want to display a parent model that matches the information of the associated child model
- [ruby on rails] how to write when getting a model nested in multiple stages with includes
- ruby on rails - about the code of the adjacency list model implemented in rails
- ruby - when processing a file sent by rails with csvforeach, the first column and the key with \ mark cannot be obtained in the
- ruby on rails - rails associated model is not saved in db
- ruby on rails - pg :: datatypemismatch error when doing heroku run rails db: migrate
- ruby - rails user model i want to get the age from the date of birth and automatically register
- ruby - rails even if i create a new model (user), it is not displayed
- ruby on rails 5 - "thin" error when installing gem
- ruby on rails - refer to by forming an association
- ruby on rails - rails not updated after editing update
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- i want to call a child component method from a parent in vuejs