Al D
Al D

Reputation: 667

Ruby on Rails update a table outside a form

I'm trying to get my head around this problem but am new to Ruby on Rails and can't figure it out. I have three models: user, match and match_pick.

My index.html.erb page in matches view lists all matches. I have split the games into 10 blocks, and have set up buttons on a page for Block 1, Block 2......When these buttons are pressed, parameters are passed to the index method in matches_controller.rb and it then only shows the games related to that block.

I'm now trying to set it up to take predictions from the user for each match in the relevant block and save these to the match_pick model - set up to save a userID, matchID and userPick.

I was hoping to put a dropdrown list beside each game listed on the index.html.erb page in matches view (see image below). I was then hoping that the user could only finalise a block's picks by pressing a submit button which would then record their picks (usually 8) for that block. This would result in 8 new lines in the match_pick model.

I've only tried to do the drop-down list this afternoon and am finding it hard because a lot of the help out there have them as part of a form. I'll have another go later as I want the dropdown list to display the options Draw, Team1 name, Team2 name. I plan on only recording an int for the user pick - 0 for draw, 1 for Team1 and 2 for Team2.

Before I throw a load more time at it, can anyone tell me if I'm approaching this correctly and if it's feasible i.e. Am I able to create multiple entries in the match_pick model by pressing the submit button? If so, where and how do I implement the functionality to do this. My main problem is how do I get the information from each of the drop-downs and how do I then write their choices as well as the userID and matchID to the match_pick model.

Here's some relevant code. I know the drop-down code is wrong but it was the only way I could get it working earlier and I'll have to spend more time trying to get it to do what I want.

I've also included an image to show what I'm talking about.

Thanks for taking the time to look. Any and all help is very much appreciated.

views/matches index.html.erb

<div class= "span10 center-table">

  <% if (params[:first_match].to_i == 0) %> 
    <h1>Administer matches</h1>
  <% else %>
     <h1>Block <%= @matches[0].block %></h1>
  <% end %>  

<table class="table table-bordered table-condensed table-striped" >
  <thead>
    <tr>
       <th class="col-sn-1">ID</th>
      <th class="col-sn-1">Block</th>
      <th class="col-sn-1">Round</th>
      <th class="col-sn-1">Date</th>
      <th class="col-sn-1">Day</th>
      <th class="col-sn-1">Time</th>
      <th class="col-sn-1">Venue</th>
      <th class="col-sn-1">Team1</th>
      <th class="col-sn-1">Team2</th>
     <% if (params[:first_match].to_i == 0) %> 
      <th class="col-sn-1">Result</th>
      <th class="col-sn-1">Played</th>
      <% else %>      
      <th class="col-sn-1">User Pick</th>
      <% end %>
    </tr>
  </thead>

  <tbody>
    <% @matches.each do |match| %>
      <tr>
    <td><%= match.id %></td>
    <td><%= match.block %></td>
    <td><%= match.round %></td>
    <td><%= match.date %></td>
    <td><%= match.day %></td>
    <td><%= match.time %></td>
    <td><%= match.venue %></td>
    <td><%= match.team1 %></td>
    <td><%= match.team2 %></td>

    <% if (params[:first_match].to_i == 0) %> 
    <td><%= match.resultString %></td>
    <td><%= match.played %></td>
    <td><%= link_to 'Show', match %></td>
    <td><%= link_to 'Edit', edit_match_path(match) %></td>
    <% else %>
    <td><%= select :userPick, [['','hot'],['Medium','medium'],['Cold','cold']], :include_blank => true %></td>
    <% end %>       
      </tr>
    <% end %>


  </tbody>


</table>

<%= link_to "Submit Picks", home_path, :id => "open-contacts-dialog-btn", :class => "btn-large btn-primary", :method => :get %>






</div>

Matches Image

Matches Image

EDIT 1

Hi anonymoussxxx, the last part is causing a problem and I don't really understand it enough to fix (tried to put a post before the match but not working). The part where you say "And don't forget add an action new_match_pick into routes.rb"

match "match/create-match-pick" => "matches#create_match_pick", :as => :create_match_pick

Nitrious is giving me the following error:

Error Code

=> Booting WEBrick                                                                                                                                                                    
=> Rails 4.1.0 application starting in development on http://0.0.0.0:3000                                                                                                             
=> Run `rails server -h` for more startup options                                                                                                                                     
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)                                                                               
=> Ctrl-C to shutdown server                                                                                                                                                          
Exiting                                                                                                                                                                               
/home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:196:in `normalize_conditions!': You should not use the `match` method in your router wit
hout specifying an HTTP method. (RuntimeError)                                                                                                                                        
If you want to expose your action to both GET and POST, add `via: [:get, :post]` option.                                                                                              
If you want to expose your action to GET, use `get` in the router:                                                                                                                    
  Instead of: match "controller#action"                                                                                                                                               
  Do: get "controller#action"                                                                                                                                                         
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:68:in `initialize'                                                         
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:1462:in `new'                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:1462:in `add_route'                                                        
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:1441:in `decomposed_match'                                                 
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:1422:in `block in match'                                                   
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:1412:in `each'                                                             
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/mapper.rb:1412:in `match'                                                            
    from /home/action/workspace/PAW/config/routes.rb:22:in `block in <top (required)>'                                                                                            
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/route_set.rb:335:in `instance_exec'                                                  
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/route_set.rb:335:in `eval_block'                                                     
    from /home/action/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.0/lib/action_dispatch/routing/route_set.rb:313:in `draw'                                                           
    from /home/action/workspace/PAW/config/routes.rb:1:in `<top (required)>'                                                                                                      
    from /home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'                                                     
    from /home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'                                                   
    from /home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'                                               
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:40:in `each'                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:40:in `load_paths'                                                        
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:16:in `reload!'                                                           
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:26:in `block in updater'                                                  
    from /home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/file_update_checker.rb:75:in `call'                                                        
    from /home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/file_update_checker.rb:75:in `execute'                                                     
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:27:in `updater'                                                           
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'                                                 
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application/finisher.rb:71:in `block in <module:Finisher>'                                               
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:30:in `instance_exec'                                                                   
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:30:in `run'              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:55:in `block in run_initializers'                                                       
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'                                                                                 
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'                                               
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'                                                              
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'                                                          
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `each'                                                                                                
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `call'                                                                                                
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'                                                                   
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'                                                                                          
    from /home/action/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'                                                                                          
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:54:in `run_initializers'                                                                
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/application.rb:288:in `initialize!'                                                                      
    from /home/action/workspace/PAW/config/environment.rb:5:in `<top (required)>'                                                                                                 
    from /home/action/workspace/PAW/config.ru:3:in `require'                                                                                                                      
    from /home/action/workspace/PAW/config.ru:3:in `block in <main>'                                                                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'                                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'                                                                                 
    from /home/action/workspace/PAW/config.ru:in `new'                                                                                                                            
    from /home/action/workspace/PAW/config.ru:in `<main>'                                                                                                                         
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'                                                                                       
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'                                                                            
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'                                                                                 
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'                                                          
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'                                                                                        
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/server.rb:50:in `app'                                                                           
    from /home/action/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'                                                                                
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/server.rb:130:in `log_to_stdout'                                                                
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/server.rb:67:in `start'                                                                         
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:81:in `block in server'                                                       
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:76:in `tap'                                                                   
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:76:in `server'                                                                
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'                                                          
    from /home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'                                                                     
    from /home/action/workspace/PAW/bin/rails:8:in `require'                                                                                                                      
    from /home/action/workspace/PAW/bin/rails:8:in `<top (required)>'                                                                                                             
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/rails.rb:27:in `load'                                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/rails.rb:27:in `call'                                                                              
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/command.rb:7:in `call'                                                                             
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client.rb:26:in `run'                                                                                     
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/bin/spring:48:in `<top (required)>'        
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/binstub.rb:11:in `load'                                                                                   
    from /home/action/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/binstub.rb:11:in `<top (required)>'                                                                       
    from /home/action/workspace/PAW/bin/spring:16:in `require'                                                                                                                    
    from /home/action/workspace/PAW/bin/spring:16:in `<top (required)>'                                                                                                           
    from bin/rails:3:in `load'                                                                                                                                                    
    from bin/rails:3:in `<main>'  

Thanks for your help.

EDIT 2

Ok so I just changed the line

match "match/create-match-pick" => "matches#create_match_pick", :as => :create_match_pick

to

post "match/create-match-pick" => "matches#create_match_pick", :as => :create_match_pick

This looks to have solved it. I need to make some changes to save the items I need but it looks good. Thanks a lot anonymousxxx

Upvotes: 1

Views: 147

Answers (1)

rails_id
rails_id

Reputation: 8220

You can try it something like edit multiple concept.

This is code for on your index view, I put check_box_tag for match.id and using jquery for enable select_tag if check_box_tag is checked. But all of whatever you want to do, most importantly I explain the concept of what I make.

<%= form_tag create_match_pick_path, :method => :post do %>
    <% @matches.each do |match| %>
    <%= check_box_tag "match_ids[]", match.id, false, :class => 'matchid' %>
    <%= select_tag "user_picks[]", options_for_select([["Select One", ""], "Hot", "Medium", "Cold"]), disabled: true, 'data-id' => match.id %>
    <% end %>
    <%= button_tag :class => "btn-large btn-primary" do %> Submit Pick <% end %>
<% end %>
<script type="text/javascript">
$('.matchid').on('click', function () {
    var dataid = $(this).val();
    if (this.checked) {
        $('select[data-id="'+ dataid +'"]').prop('disabled', false);
    } else {
        $('select[data-id="'+ dataid +'"]').prop('disabled', true);
    }
});
</script>

And on your controller looks like :

def create_match_pick
   if params[:match_ids].nil?
     redirect_to matches_url, :flash => { :error => 'Your notice or whatever' } 
   else
     @matches = Match.find(params[:match_ids])
     @userpick = params[:user_picks] unless params[:user_picks].nil?
     @matches.each_with_index do |match, index|
       MatchPick.create!(:match_id => match.id, :name => @userpick[index]) 
       if @matches.size == index + 1
         redirect_to matches_path
       end
      end
   end
end

And don't forget add an action new_match_pick into routes.rb

POST "match/create-match-pick" => "matches#create_match_pick", :as => :create_match_pick

I have tried it and successfully.

Started POST "/match/create-match-pick" for 127.0.0.1 at 2014-05-20 03:22:02 +0700
Processing by Match#create_match_pick as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"ILrFvhYTBks6gP0cKI/3iwIURpkfQgM8Xe4TytVXJvc=", "match_ids"=>["1", "2"], "user_picks"=>["Medium", "Cold"]}
  ←[1m←[35mMatch Load (0.0ms)←[0m  SELECT "matches".* FROM "matches" WHERE "matches"."id" IN(1, 2)
  ←[1m←[36m (1.0ms)←[0m  ←[1mBEGIN←[0m
  ←[1m←[35mSQL (120.0ms)←[0m  INSERT INTO "match_picks" ("match_id", "created_at", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["match_id", 1], ["created_at", Tue, 20 May 2014 03:18:01 WIB +07:00], ["name", "Medium"], ["updated_at", Tue, 20 May 2014 03:18:01 WIB +07:00]]
  ←[1m←[36m (14.0ms)←[0m  ←[1mCOMMIT←[0m
  ←[1m←[35m (1.0ms)←[0m  BEGIN
  ←[1m←[36mSQL (1.0ms)←[0m  ←[1mINSERT INTO "match_picks" ("match_id", "created_at", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"←[0m  [["match_id", 2],["created_at", Tue, 20 May 2014 03:18:01 WIB +07:00], ["name", "Cold"], ["updated_at", Tue, 20 May 2014 03:18:01 WIB +07:00]]
  ←[1m←[35m (1.0ms)←[0m  COMMIT
Redirected to http://localhost:3000/matches
Completed 302 Found in 165ms (ActiveRecord: 142.0ms)

Upvotes: 1

Related Questions