2010-08-28 141 views
0

我是紅寶石新手。我原本是一名PHP/MySQL程序員。我似乎無法理解你如何將任何帖子連接到用戶並顯示它們(例如,使用用戶ID成功創建帖子,然後查詢用戶ID以獲取他或她的姓名。)Ruby on Rails設計用戶

我正在使用設計認證。

控制器

def create 
    poll = current_user.polls.build(params[:poll]) 
    poll.onefourty = poll.onefourty[0..140] 
    poll.created_at = Time.now 
    poll.save! 

    if @poll.save 
    redirect_to root_path 
    else 
    redirect_to root_path 
    end 

end 

形式

<%= form_for Poll.new, :html => { :multipart => true } do |f| %> 

<div id="form"> 
    <div class="text_input" style="height: 65px;"><%= f.text_area :onefourty %><div class="label" style="height: 63px; line-height: 63px;">Question</div></div> 
    <div class="text_input"><%= f.text_field :option1 %><div class="label">Option One</div></div> 
    <div class="text_input"><%= f.text_field :option2 %><div class="label">Option Two</div></div> 
    <div class="text_input"><%= f.text_field :option3 %><div class="label">Option Three</div></div> 
    <div class="text_input"><%= f.text_field :option4 %><div class="label">Option Four</div></div> 

    <div id="submit"> 
<div id="left"> 
</div> 
<%= f.submit :id => "next", :value => "" %> 
    </div> 
</div> 
<% end %> 

遷移:

create_table "polls", :force => true do |t| 
    t.text  "onefourty" 
    t.string "option1" 
    t.string "option2" 
    t.string "option3" 
    t.string "option4" 
    t.string "option5" 
    t.string "option6" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "photo1_file_name" 
    t.string "photo1_content_type" 
    t.integer "photo1_file_size" 
    t.datetime "photo1_updated_at" 
    t.string "photo2_file_name" 
    t.string "photo2_content_type" 
    t.integer "photo2_file_size" 
    t.datetime "photo2_updated_at" 
    t.string "photo3_file_name" 
    t.string "photo3_content_type" 
    t.integer "photo3_file_size" 
    t.datetime "photo3_updated_at" 
    t.string "photo4_file_name" 
    t.string "photo4_content_type" 
    t.integer "photo4_file_size" 
    t.datetime "photo4_updated_at" 
    end 

create_table "users", :force => true do |t| 
    t.string "email",        :default => "", :null => false 
    t.string "encrypted_password", :limit => 128, :default => "", :null => false 
    t.string "password_salt",      :default => "", :null => false 
    t.string "confirmation_token" 
    t.datetime "confirmed_at" 
    t.datetime "confirmation_sent_at" 
    t.string "reset_password_token" 
    t.string "remember_token" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",      :default => 0 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "username" 
    t.string "firstname" 
    t.string "lastname" 
    t.string "avatar_file_name" 
    t.string "avatar_content_type" 
    t.integer "avatar_file_size" 
    t.datetime "avatar_updated_at" 
    end 

回答

1

假設你有以下結構:

# app/model/user.rb 
class User < ActiveRecord::Base 
    has_many :polls 
    #... 
end 

# app/model/poll.rb 
class Poll < ActiveRecord::Base 
    belongs_to :user 
    #... 
end 

如果你成功創建民意調查,它顯示你可能是,那麼在任何視圖 - 你可以查詢與顯示當前用戶的投票:

# app/views/polls/index.html.erb 

<%- if current_user -%> 
    <%- current_user.polls.each do |poll| -%> 
    <h2><%= poll.onefourty %></h2> 
    <ul> 
     <li><%= poll.option1 %></li> 
     <li><%= poll.option2 %></li> 
     <li><%= poll.option3 %></li> 
     <li><%= poll.option4 %></li> 
     <li><%= poll.option5 %></li> 
     <li><%= poll.option6 %></li> 
    </ul> 
    <%- end -%> 
<%- else -%> 
    <em>Not logged in</em> 
<%- end -%> 
+0

這是行不通的。它聲稱我有一個零對象。 – morcutt 2010-08-29 00:01:52

+0

在設計中,如果'current_user'爲零,那麼你沒有登錄。我已經更新了上面的視圖代碼來反映這一點。 – theTRON 2010-08-29 00:45:56

+0

得到它的工作,但仍然無法弄清楚當我顯示信息時如何查詢用戶信息。感謝你的幫助! – morcutt 2010-08-29 09:27:08