Ruby on Rails - 控制器方法的问题在视图中显示

时间:2017-05-18 06:35:59

标签: ruby-on-rails ruby ruby-on-rails-4

现在我正在完成我的小型学习项目,但我正在做一件事。该项目的功能包括:registerloginlogout 作者; 事件creatingeditingremoving;参加,不参加活动。

我想要实现的最后一件事是,能够看到谁参加某个活动。参加/不参加的功能已经完成(制作一个表格,参与活动作者)。

唯一缺少的是显示世界卫生组织正在参加不同的活动(这意味着从我的show.html.erb中events_id等于的eventattendances表中显示Authors_key)。当我尝试这样做时,我总是从eventattendacnes_controller的SHOW方法中获取一个空对象。

在这里添加所有与此相关的代码(如果遗漏了任何内容,请告诉我,我会在此处添加)。谢谢!

Versions: Rails(4.0.0), Ruby(2.3.3p222)

eventattendances_controller

class EventattendancesController < ApplicationController
  before_action  only: [:show, :edit, :update, :destroy]
  before_filter :require_login



def new
  if Eventattendance.exists?(events_id: params[:event_id].to_i, authors_id: current_user.id)
    redirect_to event_path(params[:event_id])
  else
    @eventattendance = Eventattendance.new(events_id: params[:event_id].to_i, authors_id: current_user.id)
    @eventattendance.save
    redirect_to event_path(params[:event_id])
  end
end

def destroy
  if Eventattendance.exists?(events_id: params[:event_id].to_i, authors_id: current_user.id)
    Eventattendance.where(events_id: params[:event_id].to_i, authors_id: current_user.id).first.destroy
    redirect_to event_path(params[:event_id])
  else
    redirect_to event_path(params[:event_id])
  end
end

def show
  @eventattendances = Eventattendance.find(events_id= params[:event_id].to_i)
end


 private
 def author_params
   params.require(:author).permit(:username, :email, :password,:password_confirmation)
 end

 def event_params
   params.require(:event).permit(:title, :body)
 end
   end

eventattendances.rb (型号)

  class Eventattendance < ActiveRecord::Base
    belongs_to :author
    belongs_to :event
  end

的routes.rb

Events::Application.routes.draw do

 root to: 'events#index'
 resources :events
 resources :authors
 resources :author_sessions, only: [ :new, :create, :destroy ]
 resources :eventattendances, only: [:destroy, :new, :show]
 get 'login'  => 'author_sessions#new'
 get 'logout' => 'author_sessions#destroy'
end

views / events / show.html.erb (这里我想设置来自eventattendances的show函数,以显示参加活动的作者的authors_id)

 `<h1><%= @event.title %></h1>
  <p><%= @event.body %></p>     
  <%= @at_list.blank? %>
  <%= @at_list_att.blank? %>
  <%=sql = "Select * from eventattendances"
   at_list = ActiveRecord::Base.connection.execute(sql) %>

   <% @event.eventattendances.map do |eventattendance| %>
     <tr>
     <td><%= eventattendance.author_id %></td>
     </tr>
   <% end %>

   <%= link_to "<< Back to Event List", events_path %>

   <p> </p>
   <br>

   <% if logged_in? %>
   <%= link_to "edit", edit_event_path(@event) %>
   <%= link_to "delete", event_path(@event), method: :delete, data: { confirm: "Are you sure?" } %>
   <% end %>


<%= link_to "Cancel atendance", eventattendance_path(event_id: @event.id), method: :delete, class: "btn btn-primary" %>

<%= link_to "Attend", new_eventattendance_path(event_id: @event.id), class: "btn btn-success"%>

`

events_controller

class EventsController < ApplicationController
include EventsHelper
before_filter :require_login, except: [:index]

def index
  @events = Event.all
end

def show
  @event = Event.find(params[:id])
end

def new
  @event = Event.new

end



def create
  @event = Event.new(event_params)
  @event.save

  redirect_to event_path(@event)
end

def destroy
  @event = Event.find(params[:id])
  @event.destroy
  redirect_to events_path
end

def edit
  @event = Event.find(params[:id])
end

def update
  @event = Event.find(params[:id])
  @event.update(event_params)

  redirect_to event_path(@event)
end



private
def event_params
  params.require(:event).permit(:title, :body)
end
end

author.rb

`class Author < ActiveRecord::Base
   authenticates_with_sorcery!
   validates_confirmation_of :password, message: "should match confirmation", if: :password
   has_and_belongs_to_many :events


 end`

event.rb

class Event < ApplicationRecord
  has_and_belongs_to_many :authors
end

2 个答案:

答案 0 :(得分:1)

首先,如果您正在渲染views/events/show,那么

    <% @event.eventattendances.map do |eventattendance| %>
      <tr>
        <td><%= eventattendance.author_id %></td>
      </tr>
    <% end %>

获取author_id。

但是想得更好,也许你有关系问题,你需要一个 has and belongs to many association

答案 1 :(得分:0)

解决方案是将其放入events_controller.rb show 方法(而不是eventattendances_controller

@eventattendances = Eventattendance.find_by(events_id: params[:id])

+

更改查找的参数:

(events_id= params[:event_id])(events_id: params[:id])

(也许它将来会帮助某人)

相关问题