Rails Link_to链接到不需要的视图

时间:2013-08-15 21:38:46

标签: ruby-on-rails ruby routes rails-routing link-to

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

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

MY EXHIBIT.RB(MODEL)

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

MY EXHIBIT CONTROLLER:

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

MY ROUTES.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

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

1 个答案:

答案 0 :(得分:0)

在你的:app / contorollers / exhibit_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 %>

也许,我想。