Ruby on Rails Sunday, December 1, 2019



On Sunday, December 1, 2019 at 7:12:18 PM UTC-5, Walter Lee Davis wrote:
Look at a generic Rails controller, fresh out of `rails generate scaffold foo bar baz`. You will see that the `foos/new` form posts to the /foos path. That's what I mean by "posts to the collection". Once an instance has been persisted, you will see that the `foos/1/edit` form sends a POST disguised as a PATCH or PUT to the instance, at foos/1, and the `update` method is invoked by the controller. But when you are first creating a new instance, you are sending a POST to /foos (the collection of Foo instances).

Don't try to create a separate form for each kind of thing you want to create yet, just focus for now on making the two-step "wizard" work. Once you have that working, you can use the controller's `new` method (which will be getting a parameter passed to it to say what the item_type is) to decide which form to render. But until you have that part figured out, don't waste your energy on rendering a different form.

Walter

> On Dec 1, 2019, at 3:17 PM, fugee ohu <fuge...@gmail.com> wrote:
>
>
>
> On Sunday, December 1, 2019 at 12:49:53 PM UTC-5, Walter Lee Davis wrote:
> That's just it, you don't. You POST to the collection, which `create`s a new instance.
>
> If you are expecting the GET with a querystring to create a new form with a picker selected, you should start by creating the form all in one piece, where you create a new form including a select (like the one you are trying to divide across two pages) in that same form. Make sure that submitting that form works, even though it doesn't do precisely what you're asking it to do in the divided form (yet).
>
> Then figure out how to set that property (which the select chooses) on the `new` controller method.
>
> There's a couple of different ways to do that. One would be to create a completely hand-made variable that you pass in the querystring of the form:
>
> /items/new?item_type=Foo
>
> and then "catch" that in the controller in the new method:
>
> @item = Item.new(item_type = params[:item_type])
>
> The other would be to go full-on REST, and create a new instance in the `index`, use that to build your form, and then you can use the regular strong_params accessor to get that same value (which will be "nested" inside a properly-named instance params hash) and assign it to the instance in the `new` method.
>
> Now, what you should see at this point is that your all-in-one `new` form begins to behave "automatically". The instance that you build in the controller will be populated with the item_type property, and the picker will auto-select to the chosen value.
>
> Once you see this working, you can change the select (picker) to a hidden field, and with no other changes, you will have your two-step form.
>
> Walter
>
> > On Dec 1, 2019, at 12:17 PM, fugee ohu <fuge...@gmail.com> wrote:
> >
> > Thanks but since when do we post to the new action?
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fd3f5207-840b-4fec-ba29-100fdf882447%40googlegroups.com.
>
>
> What does that mean? How do I POST to a collection which creates a new instance
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1a0cfa0a-8962-4aa6-ab89-b26a680dacb0%40googlegroups.com.


Strangely the log shows it rendered the form but the page doesn't change in the browser as if nothing happened when I clicked the submit button on the index page
  Rendered items/_form.html.erb (34.5ms)

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f853922b-b6a3-4b25-a909-bd319967c91f%40googlegroups.com.

No comments:

Post a Comment