lundi 6 juillet 2015

Ruby on Rails - Creating unique page for each person

super noob here.

I am trying to create an association between wine and wine maker.

I have a wine list page with a table that has wine's name, vintage, region, country, and maker.

I want user to be able to click the maker, then the user can see the wine maker's profile page. I don't know how to link to some wine maker profile page based on the name.

Wine maker will have many wines, but I don't know how to create a path for each wine maker's profile.

Thank you!

config/routes

Wine::Application.routes.draw do
devise_for :users
root 'static_pages#index'
resources :wine_lists
resources :wine_makers

model/wine_list.rb

class WineList < ActiveRecord::Base
  attr_accessor :wine_maker_id
  belongs_to :wine_maker
end

model/wine_maker.rb

class WineMaker < ActiveRecord::Base
  has_many :wine_lists
end

wine_lists_controller.rb

class WineListsController < ApplicationController
respond_to :html, :json

def new
    @wine_list = WineList.new
    @wine_maker = WineMaker.find(params[:wine_maker])
end

def index
    # @wine_maker = WineMaker.find_by_name(params[:wine_maker_id])
    @wine_list = WineList.all 
end

def create
    @wine_list = WineList.create(wine_list_params)
    redirect_to wine_list_path(@wine_list)
end

def show 
    @wine_list = WineList.find(params[:id])
    @wine_maker = WineMaker.find(params[:id])
end

def update
  @wine_list = WineList.find(params[:id])
  @wine_list.update_attributes(params[:wine_list_params])
  respond_with @wine_list
end

def destroy
    @wine_list = WineList.find(params[:id])
    @wine_list.destroy

    redirect_to wine_lists_path, :notice => "Deleted"
end

private

def wine_list_params 
    params.require(:wine_list).permit(:name, :vintage, :region, :country, :vino_maker)
end
end

wine_makers_controller.rb

class WineMakersController < ApplicationController

def new
    @wine_maker = WineMaker.new
end

def index 
    @wine_maker = WineMaker.all
end

def create
    @wine_maker = WineMaker.create(wine_maker_params)
    redirect_to root_path
end


def show
    @wine_maker = WineMaker.find(params[:id])
end

private

def wine_maker_params
params.require(:wine_maker).permit(:name, :born_on, :nationality, :profile, :wine )
end
end

views/wine_lists/index.html.erb

<h1>Wine List</h1>

<table id="wine_lists" class="display">     
  <thead>
    <tr>
      <th>Name</th> 
      <th>Vintage</th> 
      <th>Region</th> 
      <th>Country</th> 
      <th>Maker</th>
      <th>Delete</th>
    </tr>
  </thead>
<tbody>
  <% @wine_list.each do |wine| %>   
    <tr>
      <td><%= best_in_place wine, :name %></td>
      <td><%= best_in_place wine, :vintage %></td>
      <td><%= best_in_place wine, :region %></td>
      <td><%= best_in_place wine, :country, collection:  [ "Algeria", "Argentina", "Australia", "Austria", "Belarus", "Brazil", "Bulgaria", "Canada", "Chile", "China", "Croatia", "Czech Republic", "France", "Georgia", "Germany", "Greece", "Hungary", "Italy", "Japan", "Macedonia", "Mexico", "Moldova", "Morocco", "New Zealand", "Peru", "Portugal", "Romania", "Russia", "Serbia", "Slovakia", "Slovenia", "South Africa", "Spain", "Switzerland", "Tunisia", "Turkey", "Ukraine", "United States", "Uruguay", "Uzbekistan" ]  %></td>
      <td><%= link_to ":vino_maker", :controller => :wine_make_controller, :action => :index %></td>
      <td><button id="btnDeleteRow"><%= link_to 'Delete', wine, :method => "Delete" %></button></td>
    </tr>
  <% end %>
</tbody>
</table>

Aucun commentaire:

Enregistrer un commentaire