0

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
arilaan
  • 384
  • 2
  • 17
  • Can you post your Lesson model? Does a Lesson have many revisions? If so, I'd look at nested resources. http://stackoverflow.com/questions/12375361/rails-nested-resources It helps out a lot when you have a relationship like this where one model has_many of another model. Rails helps manage the IDs and makes your job a lot easier if you follow this convention of nested resources. – Kenrick Chien May 24 '13 at 21:31
  • Take a look at this to modify your form to handle nested models: http://railscasts.com/episodes/196-nested-model-form-part-1 – Kenrick Chien May 24 '13 at 21:33
  • I've used the second link you referred to, which another user gave me in response to an earlier question (http://stackoverflow.com/questions/16706372/saving-and-redirecting-with-a-model-and-revision-model-in-rails), to help save lessons and revisions. I'll add the forms I used there and the lesson model. The question I have here, for a new form how to use an older version of a revision to create a new one (like update, but saving a new object). The lesson would be the same. – arilaan May 25 '13 at 00:15
  • @arilaan is your code on GitHUb or something like that? If so, please link. – Soham Chowdhury May 25 '13 at 15:25
  • @SohamChowdhury My Github is https://github.com/arilaen/pen. – arilaan May 25 '13 at 17:04
  • I'm not sure I'm following this completely, but I think that instead of trying to get the "edit" method to save to a new instance, it would be easier to approach this by having "new/create" default it's initial values to the last revision in the case that a revision already exists. The way I understand it, you really don't want to provide an "edit" operation for revisions in the sense that Rails uses the term. – Peter Alfvin May 25 '13 at 18:58
  • @PeterAlfvin Hm, figuring out how to make the initial values the default is my main problem - right not it technically happens, but the revision_id stays the same (probably a nested form issue), and there's no form. I initially used the create form, which worked fine except it lacked earlier revision's values. – arilaan May 25 '13 at 19:31
  • @arilaan You can conditionally set the initial values in the "new" method of the controller, as discussed in http://stackoverflow.com/questions/4637677/rails-default-value-in-text-field-but-only-for-new-record. The revision_id is not changing currently because you're using "edit". When you return to using "new", you'll get the new id. – Peter Alfvin May 25 '13 at 22:40

0 Answers0