Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving new records based on older instances in Rails
    primarykey
    data
    text
    <p>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.</p> <p>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.)</p> <p>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 <a href="http://guides.rubyonrails.org/form_helpers.html" rel="nofollow">http://guides.rubyonrails.org/form_helpers.html</a>, but am clearly still missing a step.</p> <p>The entire app is on <a href="https://github.com/arilaen/pen" rel="nofollow">https://github.com/arilaen/pen</a>.</p> <p>Revision Controller</p> <pre><code>class RevisionsController &lt; 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 </code></pre> <p>Revision Model:</p> <pre><code>class Revision &lt; ActiveRecord::Base attr_accessible :comment, :lesson_id, :user_id, :description, :content, :time_updated, :old_revision_id belongs_to :lesson, :class_name =&gt; "Lesson", :foreign_key =&gt; "lesson_id" belongs_to :user, :class_name =&gt; "User", :foreign_key =&gt; "user_id" accepts_nested_attributes_for :lesson end </code></pre> <p>New Revision Form</p> <p>edit.html.erb</p> <pre><code>&lt;%= form_for @revision do |f| %&gt; &lt;%= render "form" %&gt; &lt;% end %&gt; </code></pre> <p>_form.html.erb in revisions</p> <pre><code>&lt;%= form_for @revision :url =&gt; { :action =&gt; "create" } do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.label :description %&gt;&lt;br /&gt; &lt;%= f.text_field :description, :value =&gt; @old_revision.description %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :content %&gt;&lt;br /&gt; &lt;%= f.text_area :content, :value =&gt; @old_revision.content %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :comment %&gt;&lt;br /&gt; &lt;%= f.text_field :comment, :value =&gt; @old_revision.comment %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Excerpt from Revision/show:</p> <pre><code> &lt;% if current_user.nil? %&gt; &lt;% else %&gt; &lt;% if @revision.user_id = current_user.id %&gt; &lt;%= link_to 'New Revision', edit_lesson_revision_path(@revision.id) %&gt;&lt;br /&gt; &lt;% else %&gt; &lt;%= link_to 'Copy', edit_lesson_revision_path(@revision.id) %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Here's my lessons model because it was requested earlier, may or may not be relevant to solution:</p> <pre><code>class Lesson &lt; ActiveRecord::Base attr_accessible :stable, :summary, :title, :time_created, :revision, :revisions_attributes has_many :revisions, :class_name =&gt; "Revision" has_many :users, :through =&gt; :revisions accepts_nested_attributes_for :revisions end </code></pre> <p>routes.rb</p> <pre><code>resources :revisions resources :lessons do resources :revisions end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload