I have a Lesson model and a Revision model. When a lesson is created its first revision is created (the models have a has_many through: relation). Users should be able to create new revisions by updating the old one - but the updates would save to a new revision.
Currently my lessons and the first revision version save properly, through a nested form. What I want to know is - how can I create a "new revision" form that does what the update form does, but save the results to a new revision? (The lesson the new revision is tied to would be unchanged.)
EDIT: With my updated (and hopefully more concise) code below, new revisions save, but I never get directed to a form. I'm sent straight to the profile page where I can see the list of revisions. How can I make updates rather than just a duplicate? I've been reading http://guides.rubyonrails.org/form_helpers.html, but am clearly still missing a step.
The entire app is on https://github.com/arilaen/pen.
Revision Controller
class RevisionsController < ApplicationController
def new
@revision = Revision.new
@lesson = Lesson.find(params[:lesson_id])
end
def create
@revision = Revision.new(params[:revision])
@revision.user_id = current_user.id
@revision.time_updated = DateTime.now
@revision.save
redirect_to current_user.profile
end
def show
@revision = Revision.find(params[:id])
end
def edit
@old_revision= Revision.find(params[:id])
@revision = Revision.new(params[:revision])
@revision.user_id = current_user.id
@revision.lesson_id = @old_revision.lesson_id
@revision.time_updated = DateTime.now
@revision.save
redirect_to current_user.profile
end
end
Revision Model:
class Revision < ActiveRecord::Base
attr_accessible :comment, :lesson_id, :user_id, :description, :content, :time_updated, :old_revision_id
belongs_to :lesson, :class_name => "Lesson", :foreign_key => "lesson_id"
belongs_to :user, :class_name => "User", :foreign_key => "user_id"
accepts_nested_attributes_for :lesson
end
New Revision Form
edit.html.erb
<%= form_for @revision do |f| %>
<%= render "form" %>
<% end %>
_form.html.erb in revisions
<%= form_for @revision :url => { :action => "create" } do |f| %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description, :value => @old_revision.description %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content, :value => @old_revision.content %>
</div>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_field :comment, :value => @old_revision.comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Excerpt from Revision/show:
<% if current_user.nil? %>
<% else %>
<% if @revision.user_id = current_user.id %>
<%= link_to 'New Revision', edit_lesson_revision_path(@revision.id) %><br />
<% else %>
<%= link_to 'Copy', edit_lesson_revision_path(@revision.id) %>
<% end %>
<% end %>
Here's my lessons model because it was requested earlier, may or may not be relevant to solution:
class Lesson < ActiveRecord::Base
attr_accessible :stable, :summary, :title, :time_created, :revision, :revisions_attributes
has_many :revisions, :class_name => "Revision"
has_many :users, :through => :revisions
accepts_nested_attributes_for :revisions
end
routes.rb
resources :revisions
resources :lessons do
resources :revisions
end