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 - 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 - about the code of the adjacency list model implemented in rails
- ruby on rails - i want to validate only the foreign key on the model side
- ruby on rails - product model test code does not succeed
- 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 - the link between the model created by devise and the model originally created does not work
- ruby on rails - pg :: datatypemismatch error when doing heroku run rails db: migrate
- ruby - pass the value from the controller in the rails model
- ruby on rails - returns a value from the rails model to the controller
- ruby on rails - i can't create a model with rails
- [ruby on rails] how to write when getting a model nested in multiple stages with includes
- [ruby on rails] i want to display a parent model that matches the information of the associated child model
- ruby on rails - rails associated model is not saved in db
- ruby - unable to create rails model (environment: cloud9)
- ruby - allow rails chat app to save to only one room
- ruby - i want to display specific columns in rails in descending order of numbers
- ruby on rails - i want to avoid displaying duplicate data for each shop
- ruby on rails - i want to deploy to aws ec2 server
- ruby on rails uninitialized constant error