> 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