我正在处理的应用程序具有以下功能:用户可以单击链接向朋友发送电子邮件。我不想保存朋友信息(第一个,最后一个,电子邮件),所以我尝试执行以下操作(在users_controller.rb中):
def send_invitation_to_friend
if params[:commit] == 'Email Invitation'
@friend_info = params[:friend_info]
EventMailer.deliver_send_invitation_to_friend(@friend_info).deliver
redirect_to_root_url
end
render 'send_invitation_to_friend'
end
在我的观看/ users / send_invitation_to_friend_html.erb中,我有:
<div class="pagetopper"></div>
<% if !user_signed_in? %>
<% redirect_to_root_url %>
<% end %>
<%= form_for @friend_info, :url => {:controller => :users, :action => :send_invitation_to_friend} do |f| %>
<div class="field-section-header">
About The Person You're Inviting
</div>
<div class="control-group">
<div class="control-label"><%= f.label :first, FIRST_STR %></div>
<div class="controls"><%= f.text_field :first, :class => "request-text-line" %></div>
</div>
<div class="control-group">
<div class="control-label"><%= f.label :last, LAST_STR %></div>
<div class="controls"><%= f.text_field :last, :class => "request-text-line" %></div>
</div>
<div class="control-group">
<div class="control-label"><%= f.label :email, EMAIL_STR %></div>
<div class="controls"><%= f.email_field :email, :class => "request-text-line" %></div>
</div>
<div class="control-group">
<div class="controls"><%= submit_tag 'Email Invitation', :class => "submit_button" %></div>
</div>
我收到以下错误:
undefined method `model_name' for NilClass:Class
Extracted source (around line #7):
4: <% redirect_to_root_url %>
5: <% end %>
6:
7: <%= form_for @friend_info, :url => {:controller => :users, :action => :send_invitation_to_friend} do |f| %>
我没有friend_info的模型和迁移,因为我不想保存这些信息,所以我如何显示表单?
答案 0 :(得分:3)
form_for用于模型更新/创建。在您的情况下,您可以使用form_tag as,
<%= form_tag( {:controller => :users, :action => :send_invitation_to_friend} ) do %>
<div class="control-group">
<div class="control-label"><%= label_tag :first, FIRST_STR %></div>
<div class="controls"><%= text_field_tag :first, :class => "request-text-line" %></div>
...
您可以在http://apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag
上找到有关form_tag的更多信息