ラベル Ruby on Rails の投稿を表示しています。 すべての投稿を表示
ラベル Ruby on Rails の投稿を表示しています。 すべての投稿を表示

2011年9月27日火曜日

Rails sqliteでboolean

Shop.paginate(:page => 1,:per_page => 5,:conditions => ['ec_flag = ?',true])
■mysql では boolean は true と false ですが、sqlite3 では 1 と 0 だからということなのですね。

2011年9月26日月曜日

rails3 1つ前のURLに戻る

<input type=button value="買い物を続ける" onClick="history.go(-1)">

2011年9月22日木曜日

rails3 失敗したscaffoldを削除する

取り消したいscaffold名を*****で実行する。
$ rails destroy scaffold *****
 

2011年9月21日水曜日

rails3 でsessionを情報をDBで管理

$ rake db:sessions:create
$ rake db:migrate
また、config/initializers/session_store.rbの8行目
********::Application.config.session_store :active_record_store
のコメントをはずす,
また、3行目の下記をコメントに変更
#Application.config.session_store :cookie_store, :key => '_market_session' 
でOKです。


2011年9月15日木曜日

Rails3 ネストしたコントローラへの値の渡し方

 ■たとえば下記のようrake routesがあったとすると、
new_shop_shop_article GET    /shops/:shop_id/shop_articles/new(.:format){:action=>"new",:controller=>"shop_articles"}
のように値をわたせばよ、

new_shop_shop_article_path(@shop)


■また、下記のような場合だと

edit_shop_shop_article GET    /shops/:shop_id/shop_articles/:id/edit(.:format){:action=>"edit", :controller=>"shop_articles"}


edit_shop_shop_article(@shop,@shop_articles)やedit_shop_shop_article()でOKです。たぶん

redirect_toアクション指定

■下記はrake routes の表示
 shop_admin_relay GET    /shop_admin_relays/:id(.:format){:action=>"show",:controller=>"shop_admin_relays"}


■redirect_toでshop_admin_relay_pathを記述せず@shop_admin_aelay.shop_adminだけを渡すと、


format.html { redirect_to(@shop_admin_aelay.shop_admin 。。。。
のように渡すとshopコントローラのshowメソッドが存在する場合,
shopのshowにアクションが移ってしまうが、
*下記にように指定shop_admin_relay_pathするとrake routesの記載どうりアクションが移動する
format.html { redirect_to(shop_admin_relay_path(@shop_admin_aelay.shop_admin)

2011年9月14日水曜日

findメソッドでActiveRecord::RecordNotFoundの対処

find_by_idを使いましょう、
こちらはnilが帰ってきます。

rails 多対多

rails generate scaffold group name:string

rails generate scaffold menber name:string

rails generate migration group_and_menber







class GroupAndMenber < ActiveRecord::Migration
  def self.up
    create_table :group_and_menbers do |t|
      t.integer :menber_id
      t.integer :group_id
    end
  end

  def self.down
    drop_table :group_and_menbers
  end
end

class GroupAndMenber < ActiveRecord::Base
  belongs_to :menber
  belongs_to :group
end


class Group < ActiveRecord::Base
  has_many :group_and_menbers
  has_many :menbers, :through => :group_and_menbers
end

class Menber < ActiveRecord::Base
  has_many :group_and_menbers
  has_many :groups, :through => :group_and_menbers
end



menber = Menber.new(:name => 'menber1')
menber.save
group = Group.new(:name => 'group1')
group.save  

menber.groups << group

で多対多が成立します







2011年9月13日火曜日

rails3 fields_forでcontrollerに配列渡し



    <%= fields_for :パラメータ名 do |field| %>
        <% @mise_admins.each do |mise_admin| %> 
              <%= field.check_box %><%= mise_admin.name %>
          <% end %>   
    <% end %> 


解説:fields_for :パラメータ名 の部分がparams[:パラメータ名]になり、
{"1"=>"0", "2"=>"1", "3"=>"1"}に受け取ることができます。1と0で判別します

rails3 多対多

rails generate scaffold man name:string

rails generate scaffold woman name:string

rails generate migration create_to_man_woman


class Woman < ActiveRecord::Base
  has_and_belongs_to_many :mans
end


class Man < ActiveRecord::Base
  has_and_belongs_to_many :womans
end

class CreateToManWoman < ActiveRecord::Migration
  def self.up
    create_table :men_women, :id => false do |t|
      t.integer :man_id
      t.integer :woman_id
    end
  end

  def self.down
    drop_table :man_woman
  end
end





m = Man.create(:name => 'man1')
w = Woman.create(:name => 'woman1')
m.womans << w


m = Man.create(:name => 'man3')
m.womans << w

rails3では<%= check_box_tag の引数

<%= check_box_tag '第1引数','第2引数' %>
第1引数はidとnameになります。
第2引数はvalueになります。

2011年9月2日金曜日

link_to タグでアクション指定

<% @shops.each do |shop| %>
  <tr>
    <td><font size="1"><%= shop.name %></font></td>
    <td><font size="1"><%= shop.address %></font></td>
     <td><%= link_to("確認",  :action => "show", :id => shop) %>
  </tr>
<% end %>

2011年8月11日木曜日

Rails3でfrom_forの引数していの内容

<%= form_for :test, :url => "tests/show" do |f| %>
<%= f.select  :id, @tests.collect {|m| [m.name, m.id] }  %>
<%= f.submit    %>

上記の記述では
第一引数がパラメータ名、第2引数がコントローラとメソッドの指定になります。
また、f.selectの第一引数がパラメータ名、第2引数を配列で渡した場合、でオプションタグのvalueとテキストになります。

コントローラでパラメータを受け取る場合は、
params[:test][:id]で受け取る事ができます。

Rails3 のroutes.rbでpostでパラメータを受け取る

routes.rbの記述
post  "tests/show", :via => :post
を記述すると
$ rake routes
コマンドを実行すると、
tests_show POST   /tests/show(.:format)       {:controller=>"tests", :action=>"show"}
が生成されパラメータを受け取る事ができます。


2011年7月13日水曜日

WindowsでRuby on Railsをインスト

■■■■Rails 3.0.9バージョンのインスト■■■■
>gem update --system
>gem install rails sqlite3

■■■■SQLITEのドライバインスト■■■■■
 http://www.sqlite.org/sqlitedll-3_7_3.zip
よりsqliteのドライバをインストしパスの通っている・Rubyのbin等にコピーしてください


■■■■Rails3のアプリの作成■■■■■

>rails new demo

create
create  README
create  Rakefile
create  config.ru
create  .gitignore
create  Gemfile
create  app
create  app/controllers/application_controller.rb
create  app/helpers/application_helper.rb
create  app/mailers
create  app/models
create  app/views/layouts/application.html.erb
create  config
create  config/routes.rb
create  config/application.rb
create  config/environment.rb
create  config/environments
create  config/environments/development.rb
create  config/environments/production.rb
create  config/environments/test.rb
create  config/initializers
create  config/initializers/backtrace_silencers.rb
create  config/initializers/inflections.rb
create  config/initializers/mime_types.rb
create  config/initializers/secret_token.rb
create  config/initializers/session_store.rb
create  config/locales
create  config/locales/en.yml
create  config/boot.rb
create  config/database.yml
create  db
create  db/seeds.rb
create  doc
create  doc/README_FOR_APP
create  lib
create  lib/tasks
create  lib/tasks/.gitkeep
create  log
create  log/server.log
create  log/production.log
create  log/development.log
create  log/test.log
create  public
create  public/404.html
create  public/422.html
create  public/500.html
create  public/favicon.ico
create  public/index.html
create  public/robots.txt
create  public/images
create  public/images/rails.png
create  public/stylesheets
create  public/stylesheets/.gitkeep
create  public/javascripts
create  public/javascripts/application.js
create  public/javascripts/controls.js
create  public/javascripts/dragdrop.js
create  public/javascripts/effects.js
create  public/javascripts/prototype.js
create  public/javascripts/rails.js
create  script
create  script/rails
create  test
create  test/fixtures
create  test/functional
create  test/integration
create  test/performance/browsing_test.rb
create  test/test_helper.rb
create  test/unit
create  tmp
create  tmp/sessions
create  tmp/sockets
create  tmp/cache
create  tmp/pids
create  vendor/plugins
create  vendor/plugins/.gitkeep

が作成されます。


■■■■Railsのwebサーバの起動■■■■■
>rails server
で起動、このときセキュリティの警告がでますが解除してください。

次に「http://localhost:3000/」にアクセスしてみてください、
WEBrickサーバが立ち上がり「welcome aboard」と表示されるはずです。


■■■■say/indexを表示■■■■■
>rails generate controller say index
上記のコマンドは「say」がコントローラ名で、
「index」がアクションになります。


下記がはきだされるはずです。
create  app/controllers/say_controller.rb
 route  get "say/index"
invoke  erb
create    app/views/say
create    app/views/say/index.html.erb
invoke  test_unit
create    test/functional/say_controller_test.rb
invoke  helper
create    app/helpers/say_helper.rb
invoke    test_unit
create      test/unit/helpers/say_helper_test.rb


それでは「http://localhost:3000/say/index」にアクセスしてみましょう。
「Say#index
Find me in app/views/say/index.html.erb」と表示されているはずです。

コントローラの中身は
class SayController < ApplicationController
  def index
  end
end
となっているはずです。http://localhost:3000/say/indexのindexがメソッドになります。

■■■■say/helloを作成しを表示■■■■■
class SayController < ApplicationController
  def index
  end
  def hello
  end
end
を追加、demo/config/routes.brに
get "say/hello"を追加し、
また、demo\app\viwe\say\hello.html.erbを作成し、<h1>hello</h1>
「http://localhost:3000/say/hello」を表示するとhelloと表示されるはずです。