Controller and routes issues in my rails app

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have an app where users can create courses, and each course has_one syllabus. How could I go about configuring my courses and syllabuses (I know it's Syllabi but apparently Rails doesn't) controller, and my routes, so on a course's page there is a link to create or show the course's syllabus, and a link back to the course from the show syllabus page?

In my routes I have:

 resources :courses do
   resources :syllabuses
   member do
  put :enroll #this is so users can enroll in the course
 end
end

Currently , so the course_id will be saved in the syllabus table in my courses_controller, I have:

def create_syllabus
   @course = Course.find(params[:id])
   @syllabus = @course.build_syllabus(params[:syllabus])
  if @syllabus.save
   redirect_to @syllabus, notice: "Successfully created syllabus."
  else
   render :new
  end
end

then in my courses show page I have:

 <section>
  <% if (current_user.courses.includes(@course) || 
   current_user.coursegroups.find_by_course_id_and_role(@course.id, "admin")) %>
   <%= render 'create_syllabus' %>
   <% end %>
  section>

then in my create_syllabus form (in my courses views folder) I have tried starting it off with:

    # I have @course = Course.find(params[:id]) defined in show in the 
                                                    #courses_controller 
   <%= form_for @course.create_syllabus do |f| %>
   <%= form_for @course.syllabus.create_syllabus do |f| %>
   <%= form_for @course.syllabus.create do |f| %>

and I get an undefined method error for each of those.

profilepic.png
manpreet 2 years ago

If you want to create a new syllabus in your show action of a specific course, you can add this to your controllers and views:

courses_controller.rb

@course = Course.find(params[:id])

# Build a new @syllabus object, only if there is none for the current course
unless @course.syllabus
  @syllabus = @course.build_syllabus
end

views/courses/show.html.erb

# Show the syllabus name if there is one, or show the form to create a new one

<% if @course.syllabus.name %>
  <p>Syllabus: <%= @course.syllabus.name %>p>
<% else %>
  <p>Create Syllabus:p>
  <%= form_for([@course, @syllabus]) do |f| %>
    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    div>
    <div class="actions">
      <%= f.submit %>
    div>
  <% end %>
<% end %>

syllabuses_controller.rb

def create
  @course = Course.find(params[:course_id])

  # Build new syllabus object based on form input
  @syllabus = @course.build_syllabus(params[:syllabus])

  if @syllabus.save
    # redirect to /course/:id
    redirect_to @course, notice: 'Syllabus was successfully created.' }
  end
end

course.rb

class Course < ActiveRecord::Base
  attr_accessible :name
  has_one :syllabus
end

syllabus.rb

class Syllabus < ActiveRecord::Base
  belongs_to :course
  attr_accessible :name, :course_id
end

Some things that I left out but you should still include:

  • validations
  • rerendering form if something goes wrong
  • pulling things out into partials
  • fixing bad code like if @course.syllabus.name
  • pull out if/else logic into a helper

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.