-4

I'm trying to build Reddit on Rails by Schneems. And I keep getting this error at the point of submitting a new link.

ruby 1.9.3

Rails 4.2.0

What's wrong with my 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

The code should take me to a page showing submitted title and url. But it gives me :

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'

New view is:

<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 %>

I've already added strong params to bundler but that did nothing.

1 Answers1

0

I fixed it after four days(finally!). So links controller should look like this:

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

Because before it was taking me to page localhost:3000/links/show and I needed localhost:3000/links/id