0

在我看来,我的 link_to 将与我希望的完全不同的“show.html.erb”。我基本上是想了解为什么“link_to @exhibit”链接到“调查”配置文件。我认为这可能与我的路线文件有关,或者它是“属于”关系的事实......但可以t 似乎让它工作......那个 link_to 应该是什么?

更新:(根据 BROIS 问题)缺少的行为不端 link_to 位于 show.html.erb 的 <%= link_to @exhibit do %> 中

我的展览.RB(模型)

class Exhibit < ActiveRecord::Base
  attr_accessible :content, :investigation_id, :name, :user_id, :media, :media_html
  belongs_to :investigation

  has_many :categorizations
  has_many :categories, :through => :categorizations

  validates :name, presence: true, length: { maximum: 140 }
  validates :content, presence: true
  default_scope -> { order('created_at DESC') }

  auto_html_for :media do
  html_escape
  image
  youtube(:width => 400, :height => 250)
  link :target => "_blank", :rel => "nofollow"
  simple_format
end

我的展品负责人:

class ExhibitsController < ApplicationController
include AutoHtml

def new
@exhibit = Exhibit.new
end

def show
@exhibit = Exhibit.find(params[:id])
end

def index
@exhibits = Exhibit.paginate(page: params[:page])
end

def create
  @investigation = Investigation.find(params[:investigation_id])
  @exhibit = @investigation.exhibits.create(params[:exhibit])
  if @exhibit.save
  flash[:success] = "You've successfully added etc etc..."
  redirect_to investigation_path(@investigation)
  else
  render 'new'
  end
end

end

我的路线.RB

resources :sessions, only: [:new, :create, :destroy]

resources :investigations do
resources :players
end

resources :investigations do
resources :exhibits
end

最后是我的 SHOW.HTML.ERB(调查资料)

<% @investigation.exhibits.each do |exhibit| %>
  <div class="row-fluid services_circles">
    <%= link_to @exhibit do %>
    <div class="media">
      <div class="pull-left">
        <%= exhibit.media_html %>
      </div>
      <div class="media-body">
        <h4 class="media-heading"><%= exhibit.name %></h4>
        <p><%= exhibit.content %></p>
      </div>
    </div>
    <% end %>
<% end %>

添加了调查控制器

class InvestigationsController < ApplicationController


def new
  @investigation = Investigation.new
end

def show
   @investigation = Investigation.find(params[:id])
end

def index
  @investigations = Investigation.paginate(page: params[:page])
end

def create
  @investigation = Investigation.new(params[:investigation])
  if @investigation.save
    flash[:success] = "You've successfully created..."
    redirect_to @investigation
  else
    render 'new'
  end
end

end

增加了调查模型

class Investigation < ActiveRecord::Base
  # belongs_to :user

  has_many :players, dependent: :destroy
  has_many :exhibits, dependent: :destroy


  default_scope -> { order('created_at DESC') }
end

感谢您的帮助...如果我需要发布更多信息,请告诉我

4

1 回答 1

0

在你的:app/contorollers/exhibits_controller.rb

def show
@investigation = Investigation.find(params[:investigation_id])
@exhibit = Exhibit.find(params[:id])
end

在你的:app/views/exhibits/show.html.erb

<%= link_to investigation_exhibit_path(@investigation, @exhibit) do %>

也许吧,我想。

于 2013-08-16T01:35:08.850 回答