I don't think it's a secret, I guess it's right there in the form_with
documentation

El lun., 2 dic. 2019 01:57, fugee ohu <fugee...@gmail.com> escribió:

>
>
> On Sunday, December 1, 2019 at 11:04:01 PM UTC-5, Walter Lee Davis wrote:
>>
>>
>>
>> > On Dec 1, 2019, at 10:17 PM, fugee ohu <fuge...@gmail.com> wrote:
>> >
>> > A generic scaffold posts to the create action PATCH and PUT according
>> to you in this discussion POST to update Even still new is fetched by GET
>> where new.html.erb renders _form.html.erb
>> >
>> >
>>
>> I'm not sure if you're confused about what I wrote, or just confused.
>> Here are the actual actions that a generic Rails 6 scaffold generates and
>> responds to. Read the comments before each method:
>>
>> class FoosController < ApplicationController
>>   before_action :set_foo, only: [:show, :edit, :update, :destroy]
>>
>>   # GET /foos
>>   # GET /foos.json
>>   def index
>>     @foos = Foo.all
>>   end
>>
>>   # GET /foos/1
>>   # GET /foos/1.json
>>   def show
>>   end
>>
>>   # GET /foos/new
>>   def new
>>     @foo = Foo.new
>>   end
>>
>>   # GET /foos/1/edit
>>   def edit
>>   end
>>
>>   # POST /foos
>>   # POST /foos.json
>>   def create
>>     @foo = Foo.new(foo_params)
>>
>>     respond_to do |format|
>>       if @foo.save
>>         format.html { redirect_to @foo, notice: 'Foo was successfully
>> created.' }
>>         format.json { render :show, status: :created, location: @foo }
>>       else
>>         format.html { render :new }
>>         format.json { render json: @foo.errors, status:
>> :unprocessable_entity }
>>       end
>>     end
>>   end
>>
>>   # PATCH/PUT /foos/1
>>   # PATCH/PUT /foos/1.json
>>   def update
>>     respond_to do |format|
>>       if @foo.update(foo_params)
>>         format.html { redirect_to @foo, notice: 'Foo was successfully
>> updated.' }
>>         format.json { render :show, status: :ok, location: @foo }
>>       else
>>         format.html { render :edit }
>>         format.json { render json: @foo.errors, status:
>> :unprocessable_entity }
>>       end
>>     end
>>   end
>>
>>   # DELETE /foos/1
>>   # DELETE /foos/1.json
>>   def destroy
>>     @foo.destroy
>>     respond_to do |format|
>>       format.html { redirect_to foos_url, notice: 'Foo was successfully
>> destroyed.' }
>>       format.json { head :no_content }
>>     end
>>   end
>>
>>   private
>>     # Use callbacks to share common setup or constraints between actions.
>>     def set_foo
>>       @foo = Foo.find(params[:id])
>>     end
>>
>>     # Never trust parameters from the scary internet, only allow the
>> white list through.
>>     def foo_params
>>       params.require(:foo).permit(:bar, :baz)
>>     end
>> end
>>
>> Here's what I have gleaned from this exchange:
>>
>> You want to start at the "index" view, and have a form on that page send
>> a request to the "new" view, passing along a parameter that you will use to
>> determine what `item_type` is to be created by the "create" method that the
>> form on the "new" view will display.
>>
>> On your Index page, here's what that form could look like:
>>
>> <%= form_with url: 'foos/new', method: :get, local: true do |f| %>
>> <%= f.collection_select :item_type, %w[one two], :to_s, :titleize  %>
>> <%= f.submit 'Choose', name: nil %>
>> <%- end -%>
>>
>> When that form is submitted, you will have access to either 'one' or
>> 'two' in the params[:item_type] variable. You can do something interesting
>> with that in the #new controller method. For a start, just to show that it
>> worked, I changed the generic scaffold new.html.erb page to include this
>> line:
>>
>> <h1>New <%= params[:item_type].to_s.titleize %></h1>
>>
>> When I test that, I see either "New One" or "New Two" when that page
>> loads, depending on the choice I made on the index page.
>>
>> Try some of these suggestions in a new, scaffolded controller in a
>> different Rails app (or the same one, if you've been keeping up with your
>> version control). Just try it as a test, and see what you can learn.
>>
>> Walter
>>
> Thanks Walter Like  Ariel said, form_with does an ajax request by default
> unless you add 'local: true' I don't know how they keep that a secret
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/e922a2fb-4118-41c4-b719-4f98bda7d1e7%40googlegroups.com
> <https://groups.google.com/d/msgid/rubyonrails-talk/e922a2fb-4118-41c4-b719-4f98bda7d1e7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcA2i3wC9zhAyBWwi28cBG%2B%3D_7f544zU0KkKu-Ngk5caBg%40mail.gmail.com.

Reply via email to