現在我正在完成我的小型學習項目,但是我一直在困惑。該項目的功能是:register
,login
,logout
爲作者; creating
,editing
,removing
的事件;出席,不參加活動。Ruby on Rails - 控制器方法中的問題在視圖中顯示
我想要實現的最後一件事是能夠看到誰參加了某個活動。參加/不參加的功能已經完成(參考事件和作者)製作了一個表Eventattendences。
唯一缺少的是顯示WHO正在參加不同的活動(即在show.html.erb中顯示events_id相等的eventattendance表中的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
的意見/事件/ show.html.erb(在這裏,我想設置從eventattendances的放映功能正參加活動的作家的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
好吧,我改了我的模型文件(我將他們加入到我的職位)內。我改變了'Show.htm.erb'中的方法,但是我仍然收到#這個錯誤'undefined method eventattendances'..一個問題,當我更改模型文件時,是否需要做一些遷移?因爲有了這個我不確定。或者其他建議如何解決?謝謝! –
當然,因爲使用has_belongs_to_many,你應該使用'@event.authors.map do | author |
OK我想,這就是結果:'找不到表「authors_events''。它看起來不會工作。我只是想,如果我只需要與現在活動的events_id(展示中)內的eventat參加活動的作者。你認爲我能用這個命令獲得這個狀態嗎? –