Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving and redirecting with a model and revision model in Rails
    text
    copied!<p>I'm relatively new to RoR, and have been working on a lesson-site sharing back-end.</p> <p>Currently I have two models: Lesson and Revision. The idea is that a teacher would create a lesson (with a title, description, etc.), at the lesson/new page. The form would redirect to another form with other fields (content, comments) at the lesson/lesson_id/revision/new page. Eventually teachers will be able to "save and modify" lessons by copying revisions to their own profile, but the model isn't there yet.</p> <p>I'm still focusing on two supposedly basic issues, both in creating new lessons and revisions.</p> <p>1) Saving the lessons to a user, and revisions to a user and lesson. At one point I had revisions saved to users, but lessons were not saving.</p> <p>2) Redirecting from the lesson form to the revision form, and the revision form to the revision page. I've tried changing the redirects in the controller and one of the form_for blocks, to no avail. The new lesson form still wants to go to /lessons rather than /lesson/1/revision/new, which I've tried to redirect to with new_lesson_revision_path. The revision form also just goes back to /revisions, which will be an admin index but I'd rather go to /lesson/1/revision/1 etc.</p> <p>Thanks in advance for any advice on either or both of these issues!</p> <p>Below is my code for reference. An excerpt from routes.rb:</p> <pre><code>resources :lessons resources :revisions #For index pages for admin resources :lessons do resources :revisions end </code></pre> <p>Models</p> <pre><code>class Lesson &lt; ActiveRecord::Base attr_accessible :stable, :summary, :title, :time_created has_many :revisions, :class_name =&gt; "Revision" has_many :users, :through =&gt; :revisions end class Revision &lt; ActiveRecord::Base attr_accessible :comment, :lesson_id, :user_id, :description, :content, :time_updated 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>Lessons new.html.erb</p> <pre><code>&lt;% if user_signed_in? %&gt; &lt;h2&gt;New lesson&lt;/h2&gt; &lt;%= form_for @lesson do |f| %&gt; &lt;%= render "form" %&gt; &lt;% end %&gt; &lt;%= link_to 'Back', lessons_path %&gt; &lt;% else %&gt; &lt;h3&gt; Please &lt;%= link_to 'sign in', new_user_session_path %&gt; or &lt;%= link_to 'create an account', new_user_registration_path %&gt; to make and save lessons.&lt;/h3&gt; &lt;% end %&gt; </code></pre> <p>Lessons _form.html.erb</p> <pre><code>&lt;%= form_for @lesson, :url =&gt; new_lesson_revision_path(@lesson_id, @revision) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :summary %&gt;&lt;br /&gt; &lt;%= f.text_area :summary %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :stable %&gt;&lt;br /&gt; &lt;%= f.check_box :stable %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Revisions new.html.erb</p> <pre><code>&lt;% if user_signed_in? %&gt; &lt;h2&gt;New revision&lt;/h2&gt; &lt;%= form_for @revision do |f| %&gt; &lt;%= render "form" %&gt; &lt;% end %&gt; &lt;%= link_to 'Back', lessons_path %&gt; &lt;% else %&gt; &lt;h3&gt; Please &lt;%= link_to 'sign in', new_user_session_path %&gt; or &lt;%= link_to 'create an account', new_user_registration_path %&gt; to make and save revisions.&lt;/h3&gt; &lt;% end %&gt; </code></pre> <p>Revisions _form.html.erb</p> <pre><code>&lt;%= form_for(@revision) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.label :description %&gt;&lt;br /&gt; &lt;%= f.text_field :description %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :content %&gt;&lt;br /&gt; &lt;%= f.text_area :content %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :comment %&gt;&lt;br /&gt; &lt;%= f.text_area :comment %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Lesson Controller: new/create</p> <pre><code> def new @lesson = Lesson.new end def create @user = current_user @lesson = Lesson.new(params[:lesson]) #@user.lessons &lt;&lt; @lesson params[:lesson][:user_id] = current_user.id params[:lesson][:lesson_id] = @lesson.id respond_to do |format| if @lesson.save format.html { redirect_to new_lesson_revision_path(params[:lesson_id]), notice: 'Your lesson was successfully created.' } format.json { render json: @lesson, status: :created, location: new_lesson_revision_path} else format.html { render action: "new" } format.json { render json: @lesson.errors, status: :unprocessable_entity } end end end </code></pre> <p>Revisions Controller: new/create</p> <pre><code> def new @revision = Revision.new end def create @lesson = Lesson.find(params[:lesson_id]) @revision = @lesson.revisions.new(params[:revision]) @revision.user_id = current_user.id @revision.lesson_id = @lesson.id @revision.time_updated = DateTime.now respond_to do |format| if @revision.save format.html { redirect_to current_user.profile } format.json { } else render :new end end end </code></pre> <p>Lastly here are most of the results from my rake routes:</p> <pre><code> home_index GET /home/index(.:format) home#index profile POST /profile(.:format) profiles#create new_profile GET /profile/new(.:format) profiles#new edit_profile GET /profile/edit(.:format) profiles#edit GET /profile(.:format) profiles#show PUT /profile(.:format) profiles#update DELETE /profile(.:format) profiles#destroy lessons GET /lessons(.:format) lessons#index POST /lessons(.:format) lessons#create new_lesson GET /lessons/new(.:format) lessons#new edit_lesson GET /lessons/:id/edit(.:format) lessons#edit lesson GET /lessons/:id(.:format) lessons#show PUT /lessons/:id(.:format) lessons#update DELETE /lessons/:id(.:format) lessons#destroy lesson_revisions GET /lessons/:lesson_id/revisions(.:format) revisions#index POST /lessons/:lesson_id/revisions(.:format) revisions#create new_lesson_revision GET /lessons/:lesson_id/revisions/new(.:format) revisions#new edit_lesson_revision GET /lessons/:lesson_id/revisions/:id/edit(.:format) revisions#edit lesson_revision GET /lessons/:lesson_id/revisions/:id(.:format) revisions#show PUT /lessons/:lesson_id/revisions/:id(.:format) revisions#update DELETE /lessons/:lesson_id/revisions/:id(.:format) revisions#destroy GET /lessons(.:format) lessons#index POST /lessons(.:format) lessons#create GET /lessons/new(.:format) lessons#new GET /lessons/:id/edit(.:format) lessons#edit GET /lessons/:id(.:format) lessons#show PUT /lessons/:id(.:format) lessons#update DELETE /lessons/:id(.:format) lessons#destroy root / home#index /profile(.:format) profiles#show </code></pre>
 

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