当我单击表单中的“提交”按钮时,创建操作应该可以正常工作,但它会由新操作处理

在我的应用程序中,我有一个应该由create action处理的表单,但它由新操作处理。 这个问题发生在我的post任务表单中,但在我的其他表单(注册表单)中,它运行得很好(它由create action处理)。 所以我想也许罪魁祸首在我的模型关系中,所以我也会在这里发布:

用户模型

has_many :client_relationships, class_name: "Task", foreign_key: "client_id", dependent: :destroy has_many :worker_relationships, class_name: "Task", foreign_key: "worker_id", dependent: :destroy has_many :worker_tasks, through: :client_relationships, source: :worker has_many :client_tasks, through: :worker_relationships, source: :client 

任务模型

 belongs_to :client, class_name: "User" belongs_to :worker, class_name: "User" 

在我的routes.rb

 resources :users do member do get :client_tasks, :worker_tasks end end resources :tasks 

如果您想知道,我在任务表中有两个用户(client_id,worker_id)。 我在users表中有一个布尔列来标识它们。 我也有约会表属于任务但我没有在这里包括它。

现在,点击提交按钮后,我从cmd获取此信息:

 Processing by TasksController#new as HTML Parameters: {"utf8"=>"V", "authenticity_token"=>"e5K/I8TpQJwpqGtapZs7xP4n7i3FF a9xHVSv7CLVYIsYSomK95x6B+J2hSbr77CHvxI01te8hQ7HOGRTRLANNg==", "task"=>{"category _id"=>"", "title"=>"", "description"=>"", "pay_type"=>"", "pay_offer"=>"", "coun ty_id"=>"", "area_id"=>"", "appointments_attributes"=>{"0"=>{"start_date"=>"", " start_time"=>"", "end_date"=>"", "end_time"=>""}}}, "commit"=>"Create Task"} ty_id"=>"", "area_id"=>"", "appointments_attributes"=>{"0"=>{"start_date"=>"", " start_time"=>"", "end_date"=>"", "end_time"=>""}}}, "commit"=>"Create Task"} 

我在浏览器上得到这个:

HTTP://本地主机:3000 /任务/新UTF8 =%E2%9C%93&authenticity_token = E5K%2FI8TpQJwpqGtapZs7xP4n7i3FFa9xHVSv7CLVYIsYSomK95x6B%2BJ2hSbr77CHvxI01te8hQ7HOGRTRLANNg%3D%3D&任务%5Bcategory_id%5D =任务%5Btitle%5D =任务%5Bdescription%5D =任务%5Bpay_type %5D =&任务%5Bpay_offer%5D =&任务%5Bcounty_id%5D =&任务%5Barea_id%5D =&任务%5Bappointments_attributes%5D%5B0%5D%5Bstart_date%5D =&任务%5Bappointments_attributes%5D%5B0%5D%5Bstart_time%5D =&任务%5Bappointments_attributes%5D%5B0%5D%5Bend_date%5D =&任务%5Bappointments_attributes%5D%5B0%5D%5Bend_time%5D =&提交=创建+任务

并在调试消息(最后3行):

commit:创建任务控制器:tasks action:new

它应该是“行动:创造”,但事实并非如此。

这真的很奇怪,我一整天都在为此敲打我的脑袋,但仍无法解决这个问题。 我正在使用simple_form btw。 请帮帮我……我是新手:(

在报名表格

           

在后期任务中

  <%= field_set_tag '1 What task do you require?'.html_safe do %>    <%= field_set_tag '2 Tell us about the task.'.html_safe do %>    <%= field_set_tag '3 What budget do you have in mind?'.html_safe do %>  

Euro <%= field_set_tag '4 Where will your task take place?'.html_safe do %> <%= field_set_tag '5 Set an initial appointment for this task.'.html_safe, :id => "date_time" do %>

它在第一种情况下工作而不是在第二种情况下工作真的很奇怪。

试试这个,

 <%= simple_form_for @task, url: tasks_path, method: :post, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %> 

这将强制表单使用post方法提交到tasks_path,该方法将转到create动作。 如果有效,请告诉我

此外,这是您的创建方法

  def create @task = current_user.client_relationships.build(task_params) if @task.save flash[:success] = "Micropost created!" redirect_to @current_user else render 'new' end end 

几个问题。

1)redirect_to @current_user没有任何意义,对吧? 应该像user_path(@current_user)

2)你应该总是执行redirect_to并返回,以便不会调用重定向后的代码块。 在你的情况下,它是好的,因为其余的是在一个else块

3)可能是task.save失败并且渲染了new。 使用调试器并检查是否来到create的第一行