Search code examples
activerecordmethodsruby-on-rails-3.1saveundefined

Ruby On Rails: Active Recode method Save is undefined when called in Link Controller,


I', working on a Ruby On Rails App, and I am getting the following error when I attempt to call the Save method from Active Record to populate a new link to the database. This is code that goes wrong, its in the Create method in my LinksController class:

def create
  @link = Link.new(params[:link])
  respond_to do |format|
    if @product.save
      format.html { render :action => "create" }
      format.json { render :json => @link }
    else
      format.html { render :action => "new" }
      format.json { render :json => @product.errors, :status => :unprocessable_entity }
    end
  end
end

When I go to

http://localhost:3000/links/new 

And attempt to create a new link with this form:

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

and click submit, I get the following error:

undefined method `save' for nil:NilClass

I have no idea what is going on, so if anyone has an answer, or even pointers, I would really appreciate it. Thanks.


Solution

  •   @link = Link.new(params[:link])
      respond_to do |format|
        if @product.save
    

    so where did @productcome from?