Ruby on Rails Thursday, May 30, 2019

Yes, that's how you would get the pickers controller, if that's how you want to model this. You could also use your existing controller for the object in question (I've forgotten the names of things in your original app). If you had a widgets_controller, you could make a method in that for each of your pickers. Your route would change depending on which approach you chose. To add the route, you would open your routes file and add the appropriate declaration there:

(assuming you were using pickers_controller#picker_two)

resources :pickers, only: [] do
collection do
get :picker_two, to: 'pickers#picker_two'
get :picker_three, to: 'pickers#picker_three'
end
end

(If you already had a widgets_controller, and wanted to add on)

resources :widgets do
collection do
get :picker_two, to: 'widgets#picker_two'
get :picker_three, to: 'widgets#picker_three'
end
end

Then run 'rake routes' to see what those routes get named. Note that because these are "collection" routes, they won't pass the ID as I had noted in my example previously. You'd have to pass that as a querystring variable, so your link would be something like /widgets/picker_two?parent=42, and you'd have to allow the parent argument in your safe_params, and then get it with params[:parent] rather than relying on the id.

Most flexible of all would be to use match rather than a resource-based route:

match 'picker_two/:id', to: 'pickers#picker_two', via: :get, as: :picker_two

That would get you a very specific named route that would fit with my original example.

All of this is covered in great detail in the excellent guide "Routing from the outside in", at guides.rubyonrails.org

The ultimate answer to your questions is 'you use the framework'. All the tools are there for you to build whatever you want, or whatever makes sense to you.

Walter

> On May 30, 2019, at 12:15 AM, David Merrick <merrickdav@gmail.com> wrote:
>
> Hi Can I just do rails g controller Pickers?
>
> How do I get the route for this as not one is made?
>
> Cheers Dave
>
> On Wed, May 29, 2019 at 12:57 AM Walter Lee Davis <waltd@wdstudio.com> wrote:
> The way I start doing this is to decompose the view into partials, at least one per picker. Make sure that each partial after the first one takes an argument (allowed to be null) indicating what the previous picker chose. If there is no argument passed, the picker is rendered with no options. Next, you need to add controller, with a route per picker, and either write unobtrusive JS event listeners, or add long-hand onchange arguments to your picker to trigger the next picker.
>
> f.collection_select :picker_one, @picker_one_options, :name, :id
>
> $(document).on('change', '#picker_one', function(evt){
> $('#picker_two_holder').load('/pickers/picker_two/' + $(this).val());
> });
>
> In the pickers_controller, you'd have a method like
>
> def picker_two
> @picker_two_options = Option.find(params[:id]).children
> render partial: 'picker_two', layout: false
> end
>
> Each picker then loads the next one, and finally, the surrounding form is submitted with all the dynamic pickers within it.
>
> Let me know if any of this is too cryptic.
>
> Walter
>
> > On May 28, 2019, at 4:06 AM, David Merrick <merrickdav@gmail.com> wrote:
> >
> > My Schema.rb is below
> >
> > ActiveRecord::Schema.define(version: 2019_05_27_040649) do
> >
> > create_table "days", force: :cascade do |t|
> > t.integer "season_id"
> > t.datetime "created_at", null: false
> > t.datetime "updated_at", null: false
> > t.date "raceday"
> > t.index ["season_id"], name: "index_days_on_season_id"
> > end
> >
> > create_table "points", force: :cascade do |t|
> > t.text "place"
> > t.text "riders"
> > t.integer "racepoints"
> > t.text "status"
> > t.integer "race_id"
> > t.datetime "created_at", null: false
> > t.datetime "updated_at", null: false
> > t.index ["race_id"], name: "index_points_on_race_id"
> > end
> >
> > create_table "races", force: :cascade do |t|
> > t.boolean "display"
> > t.text "racename"
> > t.integer "season_id"
> > t.integer "day_id"
> > t.datetime "created_at", null: false
> > t.datetime "updated_at", null: false
> > t.text "raceclass"
> > t.index ["day_id"], name: "index_races_on_day_id"
> > t.index ["season_id"], name: "index_races_on_season_id"
> > end
> >
> > create_table "seasons", force: :cascade do |t|
> > t.date "year"
> > t.datetime "created_at", null: false
> > t.datetime "updated_at", null: false
> > end
> >
> > end
> >
> > I want to end up with something like this for the race index.html.erb file
> >
> >
> >
> > <MoorePark Results.png>
> >
> >
> >
> >
> >
> >
> >
> > You select the season it then displays the days for the the selected season. You then select the day and the races for that day are displayed.
> >
> > The four tables are joined and the results is displayed in the Race Details.
> >
> > My current points index.html is this.
> >
> > <p id="notice"><%= notice %></p>
> >
> > <h1>Points</h1>
> >
> > <div class ='row'>
> > <div class="control-group">
> > <div class= 'pointsLabel'>Season</div>
> > <div class='controls'>
> > <%= collection_select(:season, :id, Season.all, :id, :year, {}, {:multiple => false}) %>
> > </div>
> > </div>
> > </div><br>
> >
> > <div class ='row'>
> > <div class="control-group">
> > <div class= 'pointsLabel'>Race Day</div>
> > <div class='controls'>
> > <%= collection_select(:day, :id, Day.all, :id, :raceday, {}, {:multiple => false}) %>
> > </div>
> > </div>
> > </div><br>
> >
> > <div class ='row'>
> > <div class="control-group">
> > <div class= 'pointsLabel'>Race Name</div>
> > <div class='controls'>
> > <%= collection_select(:race, :id, Race.all, :id, :racename, {}, {:multiple => false}) %>
> > </div>
> > </div>
> > </div><br>
> >
> > <br>
> >
> > I am thinking of using a grouped_collection_select to populate the relevant dropdowns . I know I need to Jquery or javascript to pass the selected options from one drop down to another .but can't get it to work
> >
> > Cheers Dave
> >
> > --
> > 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 post to this group, send email to rubyonrails-talk@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/39724b42-3377-44b4-8171-7635059f1f13%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> > <MoorePark Results.png>
>
> --
> 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 post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4426491F-C56F-4330-9EFE-6AB5C8B0189B%40wdstudio.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> Dave Merrick
>
> TutorInvercargill
>
> http://tutorinvercargill.co.nz
>
> Daves Web Designs
>
> Website http://www.daveswebdesigns.co.nz
>
> Email merrickdav@gmail.com
>
> Ph 03 216 2053
>
> Cell 027 3089 169
>
> --
> 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 post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CA%2B%3DMcKa1ZLg6rnE%3DE4VGP0LQ1wivHTS1aZPbHV91OKNZFiy8EQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
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 post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F47B28D6-ECA9-4DF1-B3B4-573DB639D327%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment