【问题标题】:Rails Link_to is linking to undesired viewRails Link_to 链接到不需要的视图
【发布时间】:2013-08-15 21:38:45
【问题描述】:

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

更新:(根据 BROIS 问题) 缺少的行为不端的 link_to 位于 show.html.erb 中的

我的 EXHIBIT.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

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

【问题讨论】:

  • 您正在使用@investigation 变量,但它未在您的显示操作中设置。它是设置在视图内的某个位置还是“创建”视图?
  • 请说明在显示该视图的代码中存在哪个视图中存在所谓的行为不端的链接。 show.html.erb, but it's not clear what that means. Do you have a InvestigationProfile` 类后面的括号中有“调查资料”,但您尚未共享?
  • @BroiSatse 我想我明白了...@investigation 设置在我的调查模型/控制器中...(所有展品都属于调查)。
  • BroiSatse,所以显示的 show.html.erb 用于调查配置文件(我有一个调查模型和控制器,但认为我不需要发布那个......现在就可以了。 ) 我希望这个 link_to @exhibit 链接到展览 show.html.erb
  • 刚刚更新了调查模型和控制器...有什么想法吗?谢谢大家

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


【解决方案1】:

在你的: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 %>

也许吧,我想。

【讨论】:

  • 当我这样做时,它在调查配置文件 (show.html.erb) 上出现以下错误“没有路线匹配 {:action=>"show", :controller=>"exhibits", :investigation_id=>"
  • 我认为你对控制器很有钱......但视图中的 link_to 似乎仍然关闭。感谢您的帮助,兴州
  • 知道了......它只需要是(@investigation,展览)。非常感谢。
猜你喜欢
  • 2023-04-04
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
相关资源
最近更新 更多