Rails入門 日程調整アプリを作ろう4
Rails入門 日程調整アプリを作ろう3の続き。
実装 -2-(Schedule,UserSchedule)の10です。
10.Schedule_controller.rb、ScheduleのviewsをScheduleの親であるeventに従属するよう変更する
10-1./adjuster/app/controllers/schedule_controller.rbを開き、def listを修正します。
def list # @schedule_pages, @schedules = paginate :schedules, :per_page => 10 @event = Event.find(params[:id]) @schedules = @event.schedules end
10-2./adjuster/app/views/schedule/list.rhtmlを開き、以下を修正する。
<h1><%=h @event.name %> Listing schedules</h1>
<%# link_to 'Previous page', { :page => @schedule_pages.current.previous } if @schedule_pages.current.previous %> <%# link_to 'Next page', { :page => @schedule_pages.current.next } if @schedule_pages.current.next %>
ページングの2行をコメントアウトする必要あり。ここでちょっと詰まった。
<%= link_to 'New schedule', :action => 'new', :id => @event %>
ちなみに、h1の上部が切れてしまっていたため、/adjuster/public/stylesheets/scaffold.cssを修正しました。
body, p, ol, ul, td { font-family: verdana, arial, helvetica, sans-serif; font-size: 13px; /* line-height: 18px; */ }
10-3./adjuster/app/controllers/schedule_controller.rbを開き、showの修正を行います。
def show @schedule = Schedule.find(params[:id]) @event = @schedule.event end
10-4./adjuster/app/views/schedule/show.rhtmlを編集します。list表示時にeventのidが必要になったためです。
<% for column in Schedule.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @schedule.send(column.name) %> </p> <% end %> <%= link_to 'Edit', :action => 'edit', :id => @schedule %> | <%= link_to 'Back', :action => 'list', :id => @event %>
10-5.showのページを確認するため、scheduleのデータを入力します。phpMyAdminから入力しました。
10-6./adjuster/app/controllers/schedule_controller.rbを開き、newの修正を行います。
def new # @schedule = Schedule.new @event = Event.find(params[:id]) @schedules = @event.schedules.build end
10-7./adjuster/app/views/schedule/new.rhtmlを編集します。
<h1>New schedule</h1> <% form_tag :action => 'create', :id => @event do %> <%= render :partial => 'form' %> <%= submit_tag "Create" %> <% end %> <%= link_to 'Back', :action => 'list', :id => @event %>
10-8./adjuster/app/views/schedule/_form.rhtmlを開き、attendに対して記述されている部分を削除します。
<%= error_messages_for 'schedule' %> <!--[form:schedule]--> <p><label for="schedule_schedule_date">Schedule date</label><br/> <%= time_select 'schedule', 'schedule_date' %></p> <!--[eoform:schedule]-->
10-9./adjuster/app/controllers/schedule_controller.rbを開き、create、editの修正を行います。
def create # @schedule = Schedule.new(params[:schedule]) @event = Event.find(params[:id]) @schedule = @event.schedules.build(params[:schedule]) if @schedule.save flash[:notice] = 'Schedule was successfully created.' redirect_to :action => 'list', :id => @event else render :action => 'new', :id => @event end end
def edit @schedule = Schedule.find(params[:id]) @event = @schedule.event end
10-10./adjuster/app/views/schedule/edit.rhtmlを編集します。
<h1>Editing schedule</h1> <% form_tag :action => 'update', :id => @schedule do %> <%= render :partial => 'form' %> <%= submit_tag 'Edit' %> <% end %> <%= link_to 'Show', :action => 'show', :id => @schedule %> | <%= link_to 'Back', :action => 'list', :id => @event %>
10-11./adjuster/app/controllers/schedule_controller.rbを開き、update、destroyの修正を行います。
def update @schedule = Schedule.find(params[:id]) @event = @schedule.event if @schedule.update_attributes(params[:schedule]) flash[:notice] = 'Schedule was successfully updated.' redirect_to :action => 'show', :id => @schedule else render :action => 'edit' end end
def destroy # Schedule.find(params[:id]).destroy @schedule = Schedule.find(params[:id]) @event = @schedule.event @schedule.destroy redirect_to :action => 'list', :id => @event end
controllerの修正部分がだんだんとわからなくなってきた。。いろいろ調べないと