在 user_sponsorship 上获得未定义的方法错误
app/controllers/user_sponsorships_controller.rb:7:in `new'
这是 new.html.erb:
<% if @event %>
<%= form_for @user_sponsorship, method: :post do |form| %>
<div class="mike">
<%= form.hidden_field :event_id, value: @event %>
<%= submit_tag "Yes, sponsor event", class: 'btn btn-success' %>
<%= link_to "Cancel", event_path(@event), class: 'btn btn-default'%>
</div>
<% end %>
<% end %>
这是控制器:
class UserSponsorshipsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
if params[:event_id]
@event = Event.find(params[:event_id])
@user_sponsorship = current_user.user_sponsorship.new(event: @event)
else
flash[:error] = "Event required"
end
end
end
这是模型:
class UserSponsorship < ActiveRecord::Base
belongs_to :user
belongs_to :event
attr_accessible :user, :event
end
如您所知,我对 Rails 还很陌生。在此先感谢您的帮助