I’m trying to create a partial template using <%= render “/shopping/coupons/cou” %> . Not really sure where went wrong. Thanks!
This is the error message.
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <h4> Coupon </h4>
2:
3: <%= form_for(@coupon, :url => shopping_coupon_path(@coupon)) do |f| %>
4: <div class="field">
5: <%= f.label :code %>
6: <%= f.text_field :code %>
this is my coupons controller
class Shopping::CouponsController < Shopping::BaseController
def cou
form_info
end
def create
@coupon = Coupon.find_by_code(params[:coupon][:code])
if @coupon && @coupon.eligible?(session_order) && update_order_coupon_id(@coupon.id)
flash[:notice] = "Successfully added coupon code #{@coupon.code}."
redirect_to shopping_orders_url
else
form_info
flash[:notice] = "Sorry coupon code: #{params[:coupon][:code]} is not valid."
render :action => 'show'
end
end
private
def form_info
@coupon = Coupon.new
end
def update_order_coupon_id(id)
session_order.update_attributes( :coupon_id => id )
end
end
@coupon is nil when the view is being rendered.
The problem might be that
<%= render "/shopping/coupons/cou" %>does not go through thecouaction in the controller thusform_infomethod does not execute and @coupon does not get assigned a value.You have to set @coupon in the action which renders the main view (the one which has the
<%= render "/shopping/coupons/cou" %>in it).