【问题标题】:Ruby on Rails: finding a model instance from a different controllerRuby on Rails:从不同的控制器中查找模型实例
【发布时间】:2014-08-02 20:50:15
【问题描述】:

目前我在项目模型和邀请模型之间有一个 has_many 和 belongs_to 关联。我在项目控制器的显示操作中有一个 form_form 邀请。

projects/show.html.erb

<div class="center">
    <h1><%= @project.title %></h1>

    <%= form_for @invitation do |f| %>
        <%= f.collection_select :user_id, User.all, :id, :first_name %><br>
        <%= f.submit "Send Invitation", class: "btn btn-primary" %>
    <% end %>
</div>

选择目标用户并提交表单会将当前用户带到“新”邀请页面,并且已为新邀请保存 user_id。但是,我还需要保存一个 project_id,但我不知道该怎么做。我试图查看是否可以在invitations_controller 的“创建”操作中定义实例变量@project,但我不知道如何。

invitations_controller.rb

class InvitationsController < ApplicationController
def new
    @invitation = Invitation.new
end

def create
    @invitation = @project.create_invitation(invitation_params)
    if @invitation.save
        flash[:success] = "Invitation sent!"
        redirect_to @invitation
    else
        render 'new'
    end
end

def show
    @invitation = Invitation.find(params[:id])
end

private
    def invitation_params
        params.require(:invitation).permit(:user_id, :project_id, :description)
    end
end

请帮帮我。谢谢!

【问题讨论】:

    标签: ruby-on-rails has-many model-associations belongs-to


    【解决方案1】:

    project 关联构建您的invitations

    # view
    ...
    <%= form_for @project.invitations.build do |f| %>
        <%= f.collection_select :user_id, User.all, :id, :first_name %><br>
        <%= f.submit "Send Invitation", class: "btn btn-primary" %>
    <% end %>
    
    # controller
    def create
      @invitation = Invitation.new(invitation_params)
      if @invitation.save
        flash[:success] = "Invitation sent!"
        redirect_to @invitation
      else
        render 'new'
      end
    end
    

    【讨论】:

    • 是否应该将项目的id保存到新邀请实例的project_id列中?
    • 我仍然得到“ActiveRecord::RecordNotFound in InvitationsController#create”、“找不到没有 ID 的项目”在这一行“ "。
    • in invitations_controller.rb 你在哪条线上ActiveRecord::RecordNotFound in InvitationsController#create?您显示的代码是视图...这是哪个视图?
    • 如果我点击“发送邀请”,它会引导我进入invitations/new.html.erb,因为我还没有填写描述(我验证了它的存在)。为了检查 project_id 是否已保存,我尝试了invitations/new.html.erb 中的“”行。
    • 我可以看到,点击send invitation后,你会转到InvitationsController#create,这会导致你转到InvitationsController#showredirect_to @invitation。我想我没有得到你
    【解决方案2】:

    最简单的方法是将 id 添加为隐藏字段。您的强大参数应该处理 project_id 它看起来像:

    <%= form_for @invitation do |f| %>
        <%= f.hidden_field :project_id, @project.id %>
        <%= f.collection_select :user_id, User.all, :id, :first_name %><br>
        <%= f.submit "Send Invitation", class: "btn btn-primary" %>
    <% end %>
    

    然后在您的控制器中,只需使用参数创建一个新的。通过新方法传递 id 将为您关联记录。

    def create
      @invitation = Invitation.new invitation_params
      if @invitation.save
        flash[:success] = "Invitation sent!"
        redirect_to @invitation
      else
        render 'new'
      end
    end
    

    另请注意,当您调用 create_invitation 时,它实际上会在那里调用 create 方法。所以它已经被保存了。对应于new,它只是内置在内存中是build_invitation(invitation_params)

    【讨论】:

    • 谢谢!这可行,除了它应该是“”。
    【解决方案3】:

    如果您询问如何将父 Project 模型包含在 Invitation 表单中,您可以将它们放在这样的数组中:

    <%= form_for [@project, @invitation] do |f| %>
        ....
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 2010-10-28
      • 2023-03-07
      • 2010-10-28
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多