views:

294

answers:

1

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.

  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| %>

Question Option One Option Two Option Three Option Four "next", :value => "" %>

<% 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

A: 

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 -%>
theTRON
That does not work. It claims I have a nil object.
morcutt
In devise, if `current_user` is nil, then you're not logged in. I have updated the view code above to reflect this.
theTRON
got it working but still can't figure out how to query the users information when I display the information. Thanks for all your help!
morcutt