I am new to ruby on rails. I am originally a PHP/mysql programmer. I can not seem to understand how you connect any posts to the user and display them (e.g. a post is successfully created with the user ID and then the users ID is queried to get his or her name.)
I am using devise for authentication.
Controller
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
<%= 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; 开发者_StackOverflow中文版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 %>
Migrations:
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
Assuming you have the following structure:
# app/model/user.rb
class User < ActiveRecord::Base
has_many :polls
#...
end
# app/model/poll.rb
class Poll < ActiveRecord::Base
belongs_to :user
#...
end
And if you're successfully creating polls, which it appears you probably are, then in any view - you could query and display the current user's polls with:
# 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 -%>
精彩评论