*INDEXページのソースは除く

●モデル

(member.rb)
require 'digest/md5'
class Member < ActiveRecord::Base
        has_many :member_jobs
        has_many :jobs,:through=> :member_jobs

	attr_accessor :password
        validates_uniqueness_of :usr_name
        validates_presence_of :usr_name, :password
  
	def authoricate(name, password)
		return false if name != self.usr_name
		return false if Member.crypt_password(password, 'abcdefgh') != self.pwd
 		true
 	end
  	
	def before_create
		self.pwd = Member.crypt_password(self.password, 'abcdefgh')
	end

	private
	
	def self.crypt_password(password, salt)
		Digest::MD5.hexdigest(password + salt)
	end

end

(job.rb)
class Job < ActiveRecord::Base
        has_many :member_jobs
        has_many :members,:through=> :member_jobs
	belongs_to :kind
end
(member_job.rb)
class MemberJob < ActiveRecord::Base
  belongs_to :member
  belongs_to :job
end
(kind.rb)
class Kind < ActiveRecord::Base
	has_many :jobs
end

●ビュー

(mypage.html.erb)
<%= error_messages_for :member %>

<% if @member %>
<p>
  <b>Usr name:</b>
  <%=h @member.usr_name %>
</p>

<p>
  <b>Family name:</b>
  <%=h @member.family_name %>
</p>

<p>
  <b>Pwd:</b>
  <%=h @member.pwd %>
</p>

<p>
  <b>Age:</b>
  <%=h @member.age %>
</p>

<p>
  <b>Mail:</b>
  <%=h @member.mail %>
</p>

●申し込みおしごとリスト
<br />
<% if @jobs.blank? %>
まだありません<br />
<% else %>
<% for j in @jobs %>
<%= link_to j.job.name, :controller=>"jobs", :action=>"show", :id=>j.job.id %>
<%= link_to "JobDelete", 
	{ :action => "destroyjob", :id => j.id }, 
	:confirm => "Are you sure?" %> |
<br />
<% end %>
<% end %>

<%= link_to "UserDelete", 
	{ :action => "destroy", :id => @member.id }, 
	:confirm => "Are you sure?" %> |
<% else %>
ログインしてください
<% end %>
<br />
<%= link_to 'Edit', :action=>"edit" %> |
<%= link_to 'Back', :controller=>"default", :action=>"index" %>

(login.html.erb)
<h1>Session#login</h1>
こんにちは<%= @user %>さん。
<%= @name %>
<%= @pwd %>
<%= @valid %>
<% form_tag ({:action => 'login'}, 
	{:class=>'register'}) do %>
<%= text_field_tag("name",nil, :class=>"aiueo") %>
<%= password_field_tag("pwd") %>
<%= submit_tag "Send" %>
<% end %>

(memberinput.html.erb)
<h1>New member</h1>

<% form_tag ({:action => 'createmember'}, 
	{:class=>'register'}) do %>
  <p>
    <b>Usr name</b><br />
    <%= text_field_tag("usr_name") %>
  </p>

  <p>
    <b>Family name</b><br />
    <%= text_field_tag("family_name") %>
  </p>

  <p>
    <b>Pwd</b><br />
    <%= password_field_tag("password") %>
  </p>

  <p>
    <b>Age</b><br />
    <%= text_field_tag("age") %>
  </p>

  <p>
    <b>Mail</b><br />
    <%= text_field_tag("mail") %>
  </p>

  <p>
    <%= submit_tag "CreateMember" %>
  </p>
<% end %>

<%= link_to 'Back', :controller=>"default",:action=>"index" %>

(memberedit.html.erb)
<h1>Editing member</h1>

<%= error_messages_for :member %>

<% form_tag ({:action => 'update'}, 
	{:class=>'register'}) do %>
  <p>
    <b>Usr name</b><br />
    <%= text_field_tag("usr_name",@member.usr_name) %>
  </p>

  <p>
    <b>Family name</b><br />
    <%= text_field_tag("family_name",@member.family_name) %>
  </p>
  <p>
    <b>Pwd</b><br />
    <%= text_field_tag("password",@member.pwd) %>
  </p>

  <p>
    <b>Age</b><br />
    <%= text_field_tag("age",@member.age) %>
  </p>

  <p>
    <b>Mail</b><br />
    <%= text_field_tag("mail",@member.mail) %>
  </p>

  <p>
    <%= submit_tag "Update" %>
  </p>
<% end %>

<%= link_to 'Show', :action=>"mypage" %> |
<%= link_to 'Back', :controller=>"default", :action=>"index" %>

(jobindex.html.erb)
<h1>Listing jobs</h1>

<% form_tag :action => 'search' do %>
   <%= text_field :job, :name, :value => @keyword %>
   <%= submit_tag 'Search' %>
<% end %>

<table>
  <tr>
    <th>Name</th>
    <th>Money</th>
    <th>Kind</th>
    <th>Company</th>
    <th>Pict</th>
    <th>Comment</th>
  </tr>

<% for job in @jobs %>
  <tr>
    <td><%=h job.name %></td>
    <td><%=h job.money %></td>
    <td><%=h job.kind.name %></td>
    <td><%=h job.company %></td>
    <td><%=h job.pict %></td>
    <td><%=h job.comment %></td>
    <td><%= link_to 'Show', job %></td>
    <td><%= link_to 'Edit', edit_job_path(job) %></td>
    <td><%= link_to 'Destroy', job, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New job', new_job_path %>
<%= link_to 'All jobs', :action => '' if @keyword %>

(jobshow.html.erb)
<p>
  <b>Name:</b>
  <%=h @job.name %>
</p>

<p>
  <b>Money:</b>
  <%=h @job.money %>
</p>

<p>
  <b>Kind:</b>
  <%=h @job.kind_id %>
</p>

<p>
  <b>Company:</b>
  <%=h @job.company %>
</p>

<p>
  <b>Pict:</b>
  <%= image_tag(@job.pict,:alt=>"画像") %>
</p>

<p>
  <b>Comment:</b>
  <%=h @job.comment %>
</p>

<% if @session %>
<% form_tag ({:action => 'applyjob'}, 
	{:class=>'register'}) do %>
    <%= hidden_field_tag("job_id",@job.id) %>
    <%= submit_tag "申し込み" %>
<% end %>
<% else %>
申し込みはログインしてから行えます。
<% end %>

<%= link_to 'Edit', edit_job_path(@job) %> |
<%= link_to 'Back', jobs_path %>



●コントローラー

(jobs_controller.rb)
class JobsController < ApplicationController
  # GET /jobs
  # GET /jobs.xml
  def index
    @jobs = Job.find(:all)

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

  # GET /jobs/1
  # GET /jobs/1.xml
  def show
    @job = Job.find(params[:id])
    if @job.kind_id
      @kind_name = @job.kind.name
    end
    if session[:name].blank? then
      @session = false
    else
      @session = true
    end
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @job }
    end
  end

  # GET /jobs/new
  # GET /jobs/new.xml
  def new
    @job = Job.new

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

  # GET /jobs/1/edit
  def edit
    @job = Job.find(params[:id])
  end

  # POST /jobs
  # POST /jobs.xml
  def create
    @job = Job.new(params[:job])

    respond_to do |format|
      if @job.save
        flash[:notice] = 'Job was successfully created.'
        format.html { redirect_to(@job) }
        format.xml  { render :xml => @job, :status => :created, :location => @job }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @job.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /jobs/1
  # PUT /jobs/1.xml
  def update
    @job = Job.find(params[:id])

    respond_to do |format|
      if @job.update_attributes(params[:job])
        flash[:notice] = 'Job was successfully updated.'
        format.html { redirect_to(@job) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @job.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /jobs/1
  # DELETE /jobs/1.xml
  def destroy
    @job = Job.find(params[:id])
    @job.destroy

    respond_to do |format|
      format.html { redirect_to(jobs_url) }
      format.xml  { head :ok }
    end
  end

  def search
     @keyword = params[:job][:name]
     @jobs = Job.find(:all,:conditions => ["name LIKE ?",'%' + params[:job][:name] + '%' ])
     render :action => 'index'
  end


  def applyjob
     @job_id = params[:job_id]
     @mem_id = session[:id]
     mj = MemberJob.new
     mj.member_id = @mem_id
     mj.job_id = @job_id
     mj.save
     redirect_to :controller=>"session", :action => 'mypage'
  end

end

(session_controller.rb)
require 'Member'
class SessionController < ApplicationController

  def login
    @name=params[:name]
    @pwd=params[:pwd]

    if @name
      mem = Member.find_by_usr_name(@name)
      if mem
        @valid = mem.authoricate(@name, @pwd)
      else
        @valid = false
      end
    end

    if @valid == true
	session[:name]=params[:name]
        session[:id]=mem.id
    end

    if session[:name].blank? then
        @user="guest"
    else
        @user=session[:name]
    end
  end

  def logout
	reset_session
	redirect_to :controller=>"default",:action=>"index"
  end

  def mypage
    if session[:name].blank? then
    else
       @member = Member.find(session[:id])
       @jobs = MemberJob.find_all_by_member_id(session[:id])
    end
  end

  def edit
    if session[:name].blank? then
    else
       @member = Member.find(session[:id])
    end
  end

  def update
    if session[:name].blank? then
    else
       mem = Member.find(session[:id])
       mem.usr_name = params[:usr_name]
       mem.family_name = params[:family_name]
       mem.password = params[:password]
       mem.age = params[:age]
       mem.mail = params[:mail]
       mem.save!
       session[:name]=params[:usr_name]
    end
    redirect_to :action=>"mypage"
  end

  def input
  end

  def createmember
    @member = Member.new
    @member.usr_name = params[:usr_name]
    @member.family_name = params[:family_name]
    @member.password = params[:password]
    @member.age = params[:age]
    @member.mail = params[:mail]

    if @member.save
	session[:id] = @member.id
	session[:name] = @member.usr_name
        flash[:notice] = 'Member was successfully created.'
        redirect_to :action=>"mypage"
      else
        redirect_to :action=>""
    end
  end

  def destroy
    @member = Member.find(params[:id])
    @member.destroy
    reset_session
    redirect_to :controller=>"default",:action=>"index"
  end

  def destroyjob
    @memberjob = MemberJob.find(params[:id])
    @memberjob.destroy
    redirect_to :action=>"mypage"
  end

end