【问题标题】:Showing only certain forms of a database仅显示某些形式的数据库
【发布时间】:2015-09-10 17:00:59
【问题描述】:

如果可能,我宁愿只使用 HTML 或 Ruby。我正在尝试为我的梦幻足球应用程序上的交易制作注释功能,我希望能够按相关团队进行组织。例如,当输入新笔记时,我会指定我将与哪个团队进行交易。在我的数据库中完成创建笔记后,我可以转到一个页面,选择我输入笔记的团队,并查看涉及该团队的所有笔记。基本上,我想知道如何根据表单答案之一在数据库中显示某些信息。这是我的应用程序控制器

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.

  def index_note
    @note = Note.all
  end

  def create_note
    n = Note.new
    n.team = params['team']
    n.title = params['title']
    n.pin = params['pin']
    n.pout = params['pout']
    n.pw = params['pw']
    n.otrade = params['otrade']
    n.save
    redirect_to "/index_notes"
  end

  def edit_note
    @note = Note.find_by_id(params['id'])
  end

  def update_note
    n = Note.find_by_id(params['id'])
    n.team = params['team']
    n.title = params['title']
    n.pin = params['pin']
    n.pout = params['pout']
    n.pw = params['pw']
    n.otrade = params['otrade']
    n.save
    redirect_to "/index_notes"
   end

  def destroy_note
    n = Note.find_by_id(params['id'])
    n.destroy
    redirect_to '/index_note'
  end

end

这是我的 new_note 页面

 <div class="container">
  <div class="row">
    <span class="col-md-12">
      <br  />
      <div style="background:rgba(255,255,255,0.5)" class="jumbotron"> 
        <h2 class="text-center">Add a Trade Note</h2>
        <br  />
        <br  />
        <h4 class="text-center">
          <form action="/create_note">
            <p>
              <input type="text" name="team" placeholder="Team Involved">
            </p>
            <br  />
            <p>
              <input type="text" name="title" placeholder="Title">
            </p>
            <br  />
            <p>
              <input type="text" name="pin" placeholder="Incoming Players">
            </p>
            <br  />
            <p>
              <input type="text" name="pout" placeholder="Outgoing Players">
            </p>
            <br  />
            <p>
              <input type="text" name="pw" placeholder="Position Wanted">
            </p>
             <br  />
             <p>
               <input type="text" name="otrade" placeholder="Other Trades Needed">    
            </p>
            <br  />
            <p>
              <input type="submit">
            </p>
          </form>
        </h4>
      </div>
    </span>
  </div>
</div>

这是我的 index_note 页面

<div class="container">
  <div class="row">
    <div class="col-md-12">
      <div style="background:rgba(255,255,255,0.5)" class="jumbotron">
        <h1 class = "text-center"> Trade Ideas</h1>
          <br  />
          <br  />
        <table style="width:100%">
          <tr>
            <th class= "pwsth">Title</th>
            <th class= 'pwsth'>Team</th>
            <th class= "pwsth">Position Wanted</th>
            <th class = 'pwsth'>View</th>
            <th class = 'pwsth'>Update</th>
            <th class = 'pwsth'>Destroy</th>
          </tr>
         <% @note.each do |note| %>
            <tr>
              <td><%=note.title%></td>
              <td><%=note.team%></td>
              <td><%=note.pw%></td>
              <td><a href="/note/<%= note.id %>">View</a></td>
              <td><a href="/edit_note/<%= note.id %>">Update</a>
              <td><a href="/destroy_note/<%= note.id %>">Destroy</a>
            </tr>
        <% end %>
        </table>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: html css ruby-on-rails database


    【解决方案1】:

    您需要在 rails 中指定模型关系,例如:

    class Team < ApplicationController
    
    has_many :notes
    
    end
    
    class Note < ApplicationController
    
    belongs_to :team
    
    end
    

    然后在数据库中,您有 team_id 字段用于注释记录,将其与团队联系起来。

    然后你可以通过类似的方式查询所有的团队笔记

    Team.first.notes
    

    这是一个相当开放的话题,所以请看这里:http://guides.rubyonrails.org/association_basics.html

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多