如何为编辑功能渲染包含form_for的部分?

时间:2017-03-04 01:41:45

标签: javascript jquery ruby-on-rails ajax

我在父视图中有div呈现show部分。在这个show部分中,我有一个按钮,应该更改父部分的部分以呈现“#1”形式'部分编辑对象,但我不断被卡住,因为它似乎是'#形式'部分form_for缺少@project个对象。

如何将此对象传递给'表格'部分?我错过了别的什么吗?

我对使用AJAX很新。如果您需要,很乐意提供更多信息。

我在服务器终端中收到的错误是

ActionView::Template::Error (First argument in form cannot contain nil or be empty):

的routes.rb

    get 'switchProjectView', to: 'projects#switch_main_view'
    resources :projects

projects_controller.rb

class ProjectsController < ApplicationController

  def new
    @project = Project.new
  end

  def create
    @project = Project.new(project_params)
    if @project.save
      flash[:success] = "New Project Added"
      redirect_to @project 
    else
      flash[:danger] = "Project Not Added. Please Try Again"
    end

  end

  def show
    @project = Project.find(params[:id])
  end

  def index
    @projects = Project.all
  end

  def update 
    @project = Project.find(params[:id])
    if @project.update_attributes(project_params)
      redirect_to @project
    else
      render 'edit'
    end
  end

  def edit

  end

  def switch_main_view
    respond_to do |format|               
      format.html
      format.js
    end    
  end     


  def project_params
    params.require(:project).permit(:main_contact_name, :id, :main_contact_phone, :main_contact_email, :project_name)
  end

end

show.html.erb

    <div class="body">
      <div class="center jumbotron heading-jumbo">
        <h1><%= @project.project_name %></h1>  
      </div>

      <div class="body-jumbo jumbotron" id='project-details'>
        <%= render "projects/show" %>
      </div>
    </div>

switch_main_view.js.erb

    $('#project-details').html("<%= j render :partial => 'projects/form' %>");

_form.html.erb         

  <%= form_for(@project) do |f| %>

      <%= f.label :project_name %>
      <%= f.text_field :project_name, class: 'form-control'%>

      <%= f.label :main_contact_name %>
      <%= f.text_field :main_contact_name, class: 'form-control'%>

      <%= f.label :main_contact_phone %>
      <%= f.text_field :main_contact_phone, class: 'form-control'%>

      <%= f.label :main_contact_email %>
      <%= f.text_field :main_contact_email, class: 'form-control'%>

      <%= f.submit 'Save Project', class: 'btn btn-primary'%>

  <% end %>

_show.html.erb

<div class='text-center center'>
    <h3><strong>Project Information</strong></h2>  
</div>
<h2>Start Date: Launch Date:</h1>
<span class='pull-right jumbo-btn-span'><%= link_to "Edit Project Information", switchProjectView_path(@project), remote: true, class:'btn btn-primary jumbo-btn' %></span>
<h2>Primary Conact's Name:  <%= @project.main_contact_name %></h2>
<h2>Primary Conact's Phone: <%= @project.main_contact_phone %></h2>
<h2>Primary Conact's Email: <%= @project.main_contact_email %></h2>

3 个答案:

答案 0 :(得分:2)

好吧,对于每个请求,您需要在控制器方法中重新初始化变量。在您的情况下,您需要执行以下操作:

def switch_main_view
  @project = Project.new
  respond_to do |format|               
    format.html
    format.js
  end    
end

这样做可以让你摆脱错误:表单中的第一个参数不能包含nil或为空,但这不会使你做你实际想做的事情。

当您使用link_to时,默认情况下它会转到HTML请求,您可以在Rails日志中看到,如下所示:

Processing by ProjectsController#switch_main_view as HTML

为了获得JS响应,您需要告诉link_to您需要JS响应,而不是HTML响应,您可以通过format: :js传递link_to喜欢:

<%= link_to "Edit Project Information", switchProjectView_path(@project, format: :js), remote: true, class:'btn btn-primary jumbo-btn' %></span>

remote: true将确保您不会重新加载页面内容,而是尝试在后台从服务器获取内容。

答案 1 :(得分:1)

您的请求“switchProjectView_path(@project)”仅将对象ID发送到服务器,因此您需要加载该对象以将其呈现在视图中以进行响应。
1.首先,路径应该如下所示,以便url可以包含对象id:
get 'switchProjectView/:id', to: 'projects#switch_main_view', as:"switchProjectView"
2.您必须在控制器中加载对象: def switch_main_view @project = Project.find(params[:id]) respond_to do |format| format.html format.js end end

答案 2 :(得分:0)

尝试在调用partial时传递变量:

$('#project-details').html("<%= j render :partial => 'projects/form', locals: {project: @project} %>");