1

我的 Rails 应用有 Tasks 和一个 Task has_many Taskups(更新或评论)。在显示列出任务的页面时,我希望有一个引导模式列出现有的任务并允许添加新的任务。

这是任务列表中用于启动模式的代码:

<a data-toggle="modal" href="#task-<%= task.id %>" class="btn btn-primary btn-mini" type="button">Comments</a>
<%= render :partial => "taskups/comments", locals: {task: task} %>

模式弹出,您可以看到该任务的任务列表。还有一个新任务的输入字段。

这是模态代码:

<div id="task-<%= task.id %>" class="modal" style="display: none;">
<%= simple_form_for :taskup, :url => {:action => :create} do |f| %>
  <div class="modal-header">
    <a class="close" data-dismiss="modal">&#215;</a>
    <h3>Comments</h3>
  </div>

  <div>
  <% task.taskups.each do |taskup| %>
      *
      <%= taskup.comments %>
      </br>
  <% end %>
  </div>

  <div class="modal-body">
    <%= f.input :comments, :label => 'New Comment:'  %>
    <%= f.hidden_field :task_id, :value => task.id %>
    <%= f.hidden_field :user_id, :value => current_user.id %>
  </div>
  <div class="modal-footer">
    <%= f.submit "Save Comment", :class => "btn-primary" %>
    <a class="btn" data-dismiss="modal" href="#">Close</a>
  </div>
<% end %>
</div>

但是,如果您在输入框中输入一些文本并单击“保存评论”按钮,您看到的下一个屏幕是任务输入表单。

就像代码正在尝试添加新任务而不是新任务。

谢谢您的帮助!

更新1

这是 Taskups 的控制器代码。(但是,我认为任务控制器以某种方式被调用)

# POST /taskups
# POST /taskups.json
def create
  @taskup = Taskup.new(params[:taskup])

  respond_to do |format|
    if @taskup.save
      if @taskup.taskstatus_id != nil
        Task.find(@taskup.task_id).update_attributes(:taskstatus_id => @taskup.taskstatus_id)
      end
      format.html { redirect_to @taskup, notice: 'Task Update was successfully created.' }
      format.json { render json: @taskup, status: :created, location: @taskup }
    else
      format.html { render action: "new" }
      format.json { render json: @taskup.errors, status: :unprocessable_entity }
    end
  end
end

这是 Taskups 模型:

class Taskup < ActiveRecord::Base

  belongs_to :taskstatus
  belongs_to :task
  belongs_to :user

  default_scope { where(tenant_id: Tenant.current_id) }

  default_scope :order => 'taskup_date ASC'
end
4

1 回答 1

1

这有效:

<%= simple_form_for :taskup, :url => url_for(:action => 'create', :controller => 'taskups'), :method => 'post' do |f| %>
于 2013-07-31T01:26:14.680 回答