0

我正在开发的应用程序有一个功能,用户可以单击链接向朋友发送电子邮件。我不想保存朋友信息(第一个、最后一个、电子邮件),所以我尝试执行以下操作(在 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

在我的意见/用户/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的模型和迁移,因为我不想保存此信息,那么我如何显示表单?

4

1 回答 1

3

form_for 用于模型更新/创建。在您的情况下,您可以使用 form_tag 作为,

<%= 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 的更多信息

于 2013-06-15T17:36:58.027 回答