【问题标题】:Show only names that belong to a code仅显示属于代码的名称
【发布时间】:2017-05-03 23:25:55
【问题描述】:

因此,在我的应用程序中,访客在 code/index.html 页面中搜索他们的 RSVP 代码,然后单击将他们带到 rsvp 页面的继续链接。我试图仅显示属于 RSVP 上特定代码的名称,但目前所有名称都显示出来。我已将代码作为索引页面上的链接中的参数传递,但是当我到达 rsvp 页面时,它显示在 url 中,其中的句点似乎不正确,例如:/rsvp.1 所以我猜这是我出错的地方,但我不知道为什么:

协会

Guest belongs_to :code
Code has_many :guest

代码/index.html.erb (带有将用户带到 rsvp 页面并将参数传递给 rsvp 页面的链接的页面)

<h2><%= link_to 'Continue', rsvp_path(code) %></h2>

code_controller

    def index
      @codes = Code.search(params[:search])
    end 
    def rsvp
      code_id = params[:code]
      @guests = Guest.where("code_id", "#{code_id}")

    end

代码/rsvp.html.erb

<div class="row">
  <div class="box">
    <div class="col-lg-12">
       <form class="row form-inline">
         <% @guests.each do |guest| %>
            <%= guest.name %>
         <% end %>
         <div class="form-group">
          <p> Will you be attending the wedding of Kristen Albrecht and Chris Alford September 1, 2017? </p>
         <button aria-haspopup="true" class="btn btn-default dropdown-toggle" ngbdropdowntoggle="" type="button" aria-expanded="false">
          Yes</button>
         </div>
         <div class="form-group">
          <button type="submit" class="btn btn-primary">No</button>
         </div>
          </form>
    </div>
  </div>
</div>

路线

  resources :codes
  resources :guests
  get '/code' =>'codes#code'
  get '/rsvp' =>'codes#rsvp'

【问题讨论】:

  • 你能出示你的routes.rb文件吗?
  • @GregAnswer 我已经添加了路线

标签: ruby-on-rails ruby


【解决方案1】:

要确保 code 是 URL 中的参数,您必须进行以下更改:

routes.rb

get '/rsvp/:code' =>'codes#rsvp'

代码/index.html.erb

<h2><%= link_to 'Continue', rsvp_path(code: code) %></h2>

这样params[:code]就会在code_controller中有一个值

【讨论】:

    【解决方案2】:

    好的,我想通了:

    我将索引页面上的链接更改为

     <%= link_to "Continue", {:controller => "codes", :action => "rsvp", :code_id => code.id }%>
    

    然后我将控制器代码更改为

      def rsvp
        @guests = Guest.where(code_id: params[:code_id]) 
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多