nested resources rails

แก้ใน routes.rb

เพิ่มจาก

map.resources :courses

เป็น

map.resources :courses do |courses|
courses.resources :lectures
end

ทำให้เราสามารถเรียกได้จาก

http://127.0.0.1:3005/courses/1/lectures

ใช้ params[:course_id] ได้เลย

def index
@course = Course.find(params[:course_id])
@lectures = Lecture.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml  { render :xml => @lectures }
end
end

def show
@course = Course.find(params[:course_id])
@lecture = @course.lectures.find(params[:id]) # เปลี่ยนจาก Lecture.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml  { render :xml => @lecture }
format.json  { render :json => @lecture }
end
end

# GET /lectures/new
# GET /lectures/new.xml

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

@lecture = Lecture.new

respond_to do |format|
format.html # new.html.erb
format.xml  { render :xml => @lecture }
format.json  { render :json => @lecture }
end
end

# GET /lectures/1/edit
def edit
@course = Course.find(params[:course_id])
@lecture = @course.lectures.find(params[:id]) # เปลี่ยนจาก Lecture.find(params[:id])
end

def create
@course = Course.find( params[:course_id])
@lecture = Lecture.new(params[:lecture])
@lecture.course_id = @course.id

respond_to do |format|
if @lecture.save
flash[:notice] = ‘Lecture was successfully created.’
format.html { redirect_to([@course, @lecture]) } # @lecture
format.xml  { render :xml => @lecture, :status => :created, :location => @lecture }
format.json  { render :json => @lecture, :status => :created, :location => @lecture }
else
format.html { render :action => “new” }
format.xml  { render :xml => @lecture.errors, :status => :unprocessable_entity }
format.json  { render :json => @lecture.errors, :status => :unprocessable_entity }
end
end
end

def update
@course = Course.find(params[:course_id])
@lecture = @course.lecture.find(params[:id]) # เปลี่ยนจาก @lecture = Lecture.find(params[:id])
@lecture.course_id = @course.id

respond_to do |format|
if @lecture.update_attributes(params[:lecture])
flash[:notice] = ‘Lecture was successfully updated.’
format.html { redirect_to([@course, @lecture]) } # @lecture
format.xml  { head :ok }
format.json  { head :ok }
else
format.html { render :action => “edit” }
format.xml  { render :xml => @lecture.errors, :status => :unprocessable_entity }
format.json  { render :json => @lecture.errors, :status => :unprocessable_entity }
end
end
end

def destroy
@course = Course.find(params[:course_id])
@lecture = Lecture.find(params[:id])
@lecture.destroy

respond_to do |format|
format.html { redirect_to(course_lectures_url(@course)) } แก้จาก # lectures_url
format.xml  { head :ok }
end
end

————————————————————————————-

view/index.html

<h1>Listing lectures for <%= @course.name + “, ” + @course.semester + “, ” + @course.year.to_s %></h1>

<table>
<tr>
<th>Lecture number</th>
<th>Date</th>
<th>Topics</th>
<th>Readings</th>

# ลบ <th>Course</th> ทิ้งไป
</tr>

<% for lecture in @lectures %>
<tr>
<td><%=h lecture.lecture_number %></td>
<td><%=h lecture.date %></td>
<td><%=h lecture.topics %></td>
<td><%=h lecture.readings %></td>
# ลบ <td><%=h lecture.course_id %></td>

<td><%= link_to ‘Show’, [@course, lecture] %></td> # แก้จาก lecture
<td><%= link_to ‘Edit’, edit_course_lecture_path(@course, lecture) %></td> # แก้จาก edit_lecture_path(lecture)
<td><%= link_to ‘Destroy’, [@course, lecture], :confirm => ‘Are you sure?’, :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to ‘New lecture’, new_course_lecture_path(@course) %> # แก้จาก new_lecture_path

————————————————————————————-

มี blanket อยู่สองอันคือ

redirect_to([@course, @lecture])

<% form_for([@course, @lecture]) do |f| %>

มีที่ต้องระวังคือ

course_lectures_url(@course)

def destroy
format.html { redirect_to(course_lectures_url(@course)) }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s