Ruby On Rails - How to send information about a button, click and display the information of two models on the same page

advertisements

I'm new to Ruby on Rails (and MVC) and am going around in circles trying to implement something. It relates to routing, and passing information from a button click on a static page to another class then rendering information from two different models (that have a relationship set up). Any help is much appreciated:-

So I generated users, matches and matchPicks scaffolds in rails on Nitrious.io and everything with them is fine. I think I understand most of how the routing goes re: GET, POST etc.

I then created a static page called blocks that has a number of buttons on it (Button 1 - 10). What I want to do is the following:-

  • If the user clicks Button 1, relevant information on the matches (from the match model) in block 1 render as well as drop-down menus (from the match_picks model) that allow the user enter their team selection.
  • If the user clicks Button 2, relevant information on the matches (from the match model) in block 2 render as well as drop-down menus (from the match_picks model) that allow the user enter their team selection.
  • And so on.....

I think I have my relationships set up properly (see models code below) but my questions are:-

  1. How do I pass information on what button was clicked (i.e. the block number) to the match_picks controller? And then how do I take that information in the match_picks controller and only display the relevant matches with the same block number?

  2. Assuming I got the above working, I would only be displaying information from the match_picks database (in the match_pick/index view). How do I also show information from the match database in this view?

I'm pulling my hair out at this stage on how to implement it. I've read loads but am still so confused. I've posted some code below. If some other information is needed let me know.

Thanks for reading (and hopefully helping).

user.rb

  class User < ActiveRecord::Base
  attr_accessible :email, :username, :password, :password_confirmation, :admin
  attr_accessor :password
  before_save :encrypt_password
  has_many :match_picks

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email, :on => :create
  validates_presence_of :username, :on => :create
  validates_uniqueness_of :email
  validates_uniqueness_of :username

  def self.authenticate_by_email(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def self.authenticate_by_username(username, password)
    user = find_by_username(username)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end

match.rb

class Match < ActiveRecord::Base
  attr_accessible :block, :round, :date, :day, :time, :venue, :team1, :team2, :played
  has_many :match_picks
end

match_pick.rb

class MatchPick < ActiveRecord::Base
      attr_accessible :userID, :matchID, :userPick, :result, :points, :closed
      belongs_to :user
  belongs_to :match
end

routes.rb

Rails.application.routes.draw do
  resources :match_picks

  resources :matches

  root :to=>"home#index"
  get "sign_in" => "authentication#sign_in"
  # get "home" => "authentication#login"
  # get "instructions" => 'home'
  get "signed_out" => "authentication#signed_out"
  get "new_user" => "authentication#new_user"
  post "sign_in" => "authentication#login"
  put "sign_in" => "authentication#login"
  post "new_user" => "authentication#register"
  put "new_user" => "authentication#register"
  get "admin_users" => "admin#users"
  delete "user/:id" => "admin#delete_user", :as => "user"
  get "admin_users" => "authentication#admin_users"
  get '/home', to: 'home#home'
  get '/instructions', to: 'home#instructions'
  get '/blocks', to: 'home#blocks'  

end

schema.rb

ActiveRecord::Schema.define(version: 20140507123848) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "match_picks", force: true do |t|
    t.integer  "userID"
    t.integer  "matchID"
    t.integer  "userPick"
    t.integer  "result"
    t.integer  "points"
    t.boolean  "closed"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "matches", force: true do |t|
    t.integer  "block"
    t.string   "round"
    t.date     "date"
    t.string   "day"
    t.string   "time"
    t.string   "venue"
    t.string   "team1"
    t.string   "team2"
    t.integer  "result"
    t.string   "resultString"
    t.boolean  "played"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "username"
    t.string   "email"
    t.string   "password_hash"
    t.string   "password_salt"
    t.boolean  "admin"
    t.integer  "points"
    t.integer  "leagues",       array: true
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

views/home/blocks.html.erb

<div class= " col-md-1 text-center">

<h1>Matches</h1>

<p id="notice"><%= notice %></p>

<% today=Date.today %>
<!--<% @today="2014-06-12".to_date %>-->

<div>
  <div>
    <%= button_to "Match Block 1", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-11".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 12th June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

  <div>
    <%= button_to "Match Block 2", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-14".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 14th June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 3", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-18".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 18th June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 4", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-21".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 21st June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 5", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-24".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 24th June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 6", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-28".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 28th June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 7", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-06-30".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 30th June 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 8", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-07-04".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 4th July 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 9", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-07-08".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 8th July 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

    <div>
    <%= button_to "Match Block 10", matches_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>
      <% block1Close = "2014-07-12".to_date %>
      <% if today<=block1Close %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block's match selections must be made before 00:00 on 12th July 2014 </p>
      <% else %>
      <p style="color:black; background: #FFFFF2; display:inline-block; border: 1px solid #000066;padding: 4px;">This block is now closed to selections. You can check your picks and see results only</p>
      <% end %>
  </div>

  <%= button_to "Match Picks", match_picks_path, :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>

</div>

</div>

views/match_picks/index.html.erb

<h1>Listing match_picks</h1>

<table>
  <thead>
    <tr>
      <th>Userid</th>
      <th>Matchid</th>
      <th>Userpick</th>
      <th>Result</th>
      <th>Points</th>
      <th>Closed</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @match_picks.each do |match_pick| %>
      <tr>
    <td><%= match_pick.userID %></td>
    <td><%= match_pick.matchID %></td>
    <td><%= match_pick.userPick %></td>
    <td><%= match_pick.result %></td>
    <td><%= match_pick.points %></td>
    <td><%= match_pick.closed %></td>
    <td><%= link_to 'Show', match_pick %></td>
    <td><%= link_to 'Edit', edit_match_pick_path(match_pick) %></td>
    <td><%= link_to 'Destroy', match_pick, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Match pick', new_match_pick_path %>

EDIT 1

I can't get it to pass the parameter. here's the relevant code:

In blocks.html.erb

<%= button_to "Match Block 1", matches_path(first_match: 1), :id => "open-contacts-dialog-btn", :class => "btn btn-large btn-primary", :method => :get %>

In matches_controller.rb

  def index
    if(params[:first_match].equal?(1))
      @matches = Match.all
      puts "The Test is true"
      puts "The value of first_match is" + params[:first_match].to_s
    else
      @matches = Match.where(:id => 1..10)
      puts "This test is false"
      puts "The value of first_match is" + params[:first_match].to_s
     end
  end

The test keeps failing so enters the else and the debug line comes out as "The value of first_match is " (i.e. no value). I can't figure out why it's not passing the value of the parameter


Welcome to Rails!

To pass a variable to a controller you can simply add a parameter to the path like this :

<%= link_to 'Show', match_path(match_pick, id: my_value) %>

And you will get it in the controller like this

params[:id]

To display a different views with different models you can have a method in your controller like this :

def show
  @match = Match.find(params[:id])
  @match_picks = @match.match_picks
end

You can use it in your by simply calling @match or @match_pick. You can also display a different view by using the render method.

If you want to show a specific view for each block, you should create more views.