【问题标题】:Ruby on Rails Devise userRuby on Rails 设计用户
【发布时间】:2010-08-28 00:41:43
【问题描述】:

我是 ruby​​ on rails 的新手。我最初是一名 PHP/mysql 程序员。我似乎无法理解您如何将任何帖子连接到用户并显示它们(例如,使用用户 ID 成功创建帖子,然后查询用户 ID 以获取他或她的姓名。)

我正在使用设备进行身份验证。

控制器

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

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案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 -%>
    

    【讨论】:

    • 在设计中,如果current_user 为 nil,那么您还没有登录。我已经更新了上面的视图代码以反映这一点。
    • 让它工作了,但是当我显示信息时仍然无法弄清楚如何查询用户信息。感谢您的所有帮助!
    • 因为用户有_many 投票。您可以通过user.polls 从用户对象访问民意调查。同样,如果您有 poll 对象,您可以通过 poll.user 访问用户。
    猜你喜欢
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多