找不到没有ID的控制器

时间:2015-01-21 14:53:59

标签: ruby-on-rails ruby controller reddit

我正在尝试用Schneems在Rails上构建Reddit。我一直在提交新的link

时收到此错误

ruby​​ 1.9.3

Rails 4.2.0

我的Links#Controller出了什么问题?

class LinksController < ApplicationController

def show
  @link = Link.find(params[:id])
end

def new
 @link = Link.new
end

def create
  @link = Link.new(links_params)
 if @link.save
  redirect_to(:action => 'show')
 else
  render('new')
 end
end

private
  def links_params
    params.require(:link).permit(:title, :url)
  end
end

代码应该带我到显示提交的标题和网址的页面。但它给了我:

ActiveRecord::RecordNotFound in LinksController#show
Couldn't find Link without an ID
Rails.root: C:/Users/Andrew/Documents/reddit_on_rails
Application Trace | Framework Trace | Full Trace
app/controllers/links_controller.rb:4:in `show'

新观点是:

<h1>New link</h1>


<%= form_for @link do |f| %>
<% if @link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@link.errors.count, "error") %> prohibited this link from   being saved:</h2>

  <ul>
  <% @link.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

我已经为捆绑商添加了强大的参数,但没有做任何事情。

1 个答案:

答案 0 :(得分:0)

我在四天后修好了(最后!)。 因此链接控制器应如下所示:

def create
@link = Link.new(links_params)
if @link.save
  render('show')
else
  render('new')
end

因为在它带我去寻找localhost:3000 / links / show之前我需要localhost:3000 / links / id