Ask A Question

Notifications

You’re not receiving notifications from this thread.

Adding a New Blog Post Action Discussion

Hi Chris, i have noticed that the show action has an instance variable called @blog_post. Which is the current blog post being viewed.
Why are we overwriting that variable in the new action?

Reply

We're not overriding it. Rails is only going to call one method in the controller when an HTTP request comes in. We're just using the same variable name because that's the object were working with.

Reply

Correct me if i am wrong but the way i understand this is that
When we click on a particular id, a request with that id as its params will be sent to get blog_posts/:id.
Since our action for that particular url is set to show in our , the show method in the controller will handle that.
The show method creates a @blog_post instance attribute that holds the blog with the id that was sent over, then through our view we render that particular blog_post.
Then the user visits the blog_post/new url to create a new url..this then is handled by the new action that overwrites the blog_post that the user was previously reading.....yes?
Or are there two @blog_post instance variables created?

Why is that important? Why cant we create two instance variables that track two different things ie the blog_post last read, and the new one being created?

Reply

I believe reusability of @blog_post in different methods is due to the scoping rules of instance variables within the class. Each HTTP request is a fresh start, and Rails sets up new instance variables for each request based on the action that handles it.

Also the stateless nature of HTTP means that each request-response cycle is independent. So, the @blog_post variable from a show request won't be remembered in a subsequent new request. Each action sets up its own @blog_post variable, independent of what happened in previous requests.

Reply

I'm stuck with a undefined methodblog_posts_path' for an instance of...error that I feel like I'm just not understanding the routing magic enough to debug. It's definitely theform_with model: @plog_post` line that is causing it, not the controller itself, but I've compared our routes.rb files against one another 100 times and I can't seem to find the error. Any help? Where's the plural magic inserting itself here?

Reply

I fixed this by using this line instead: resources :blog_posts, only: [:new, :create]

Reply

I am following these tutorials on a slightly newer version of Rails (7.1.3) and I'm thinking there may have been a change there where it checks if the POST endpoint exists prior to rendering the form and errors out if it does not.

Reply

undefined methodblog_posts_path' for an instance of...

What was the rest of this error? The part after where you ended the message could be the piece that helps solve this error.

Reply

Class and then I'm assuming a memory path or some other unique ID of the class.

Like "class <123456>"

Going the resource route that is shared a few lessons later works. I think in Rails 7.1 you just can't use the form_with helper without having the create/POST route helper as well. Essentially you need this lesson and the next before you can reload the page.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,584+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.