为每个人创建唯一的页面

时间:2015-07-06 22:35:00

标签: ruby-on-rails ruby ruby-on-rails-3 associations

我正在努力建立葡萄酒和酿酒师之间的联系。

我有一个酒单页面,上面有一个有葡萄酒名称,年份,地区,国家和制造商的表格。

我希望用户能够点击制作者,然后用户可以看到葡萄酒制造商的个人资料页面。我不知道如何根据名称链接到某个酿酒师资料页面。

葡萄酒制造商将拥有许多葡萄酒,但我不知道如何为每个葡萄酒制造商的形象创造一条道路。

配置/路由

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

模型/ wine_list.rb

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

模型/ 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

视图/ 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>

2 个答案:

答案 0 :(得分:1)

到目前为止,你做得很好!保持。

只需使用#link_to帮助程序即可完成工作:

link_to "Wine Maker", @wine_list.wine_maker

您会注意到我并不是简单地编写link_to "Wine Maker", @wine_maker,因为Ruby on Rails中的最佳实践表明我们的控制器应该只在可能的情况下实现单个实体。

现在,#link_to只会链接到#show的{​​{1}}操作。您WineMakersController中可以放置个人资料页信息。

<强>更新

您的/wine_makers/show.html.erb方法在您的视图中应如下所示:

link_to

或者,如果您希望在<td><%= link_to "Wine Maker", @wine_list.wine_maker, controller: "wine_makers", action: "show" %></td> 中保留@wine_maker个实例,则可以为WineListsController执行此操作:

#link_to

答案 1 :(得分:0)

如下所示,对 @Colin Graves 答案的小改动应该对你有用,除非特定的wine_list没有{{1}与之相关联。

wine_maker

应该是<td><%= link_to "Wine Maker", wine.wine_maker, controller: "wine_makers", action: "show" %></td> 而不是wine.wine_maker,因为您 迭代 @wine_list.wine_maker并且@wine_list也是在 循环

难怪您在使用link_to时获得undefined method error,因为@wine_list.wine_maker@wine_list的结果,返回 WineList.all