当我单击表单中的提交按钮时,创建操作应该有效,但它是由新操作处理的
When I click submit button in my form, create action is supposed to work but it is rather processed by new action
在我的应用程序中,我有一个应该由创建操作处理的表单,但它是由新操作处理的。这个问题发生在我的 post 任务表单中,但在我的其他表单(注册表)中,它工作得很好(它由创建操作处理)。所以我认为罪魁祸首可能出在我的模型关系中,所以我也会 post 在这里:
用户模型
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
如果您想知道,我有两个用户在执行任务 table(client_id、worker_id)。我在用户 table 中有一个布尔列来识别他们。我也有约会 table 属于任务,但我没有把它包括在这里。
现在,单击提交按钮后,我从 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"}
我在浏览器上得到这个:
以及调试消息(最后 3 行):
commit: Create Task
controller: tasks
action: new
它应该是 "action: create" 但它不是。
真是奇葩,为此折腾了一天也解决不了。我正在使用 simple_form 顺便说一句。请帮助我......我是这里的新手:(
在注册表中
<%= simple_form_for @user, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.input :county_id %>
<%= f.input :area_id %>
<%= f.button :submit %>
<% end %>
在post任务中
<%= simple_form_for @task, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
<%= field_set_tag '<span class="label label-info">1</span> What task do you require?'.html_safe do %>
<%= f.input :category_id, collection: Category.order(:name), label_method: :name, value_method: :id, label: "Category", include_blank: "Select a category of task" %>
<%= f.input :subcategory_id, collection: Category.order(:name), as: :grouped_select, group_method: :subcategories %>
<% end %>
<%= field_set_tag '<span class="label label-info">2</span> Tell us about the task.'.html_safe do %>
<%= f.input :title, placeholder: 'Give a descriptive title for your task'%>
<%= f.input :description, placeholder: 'Describe your task here...' %>
<% end %>
<%= field_set_tag '<span class="label label-info">3</span> What budget do you have in mind?'.html_safe do %>
<ul class="nav nav-tabs" id="payTypeTabs">
<%= f.input :pay_type, as: :hidden %>
<li><a href="#fixed-price-tab" data-toggle="tab">Hire for a fixed price</a></li>
<li><a href="#per-hour-tab" data-toggle="tab">Hire per hour</a></li>
</ul>
<h1></h1>
<%= f.input :pay_offer, wrapper: :horizontal_input_group do %>
<span class="input-group-addon">€</span>
<%= f.input_field :pay_offer, as: :integer, class: "form-control" %>
<span class="input-group-addon">Euro</span>
<% end %>
<% end %>
<%= field_set_tag '<span class="label label-info">4</span> Where will your task take place?'.html_safe do %>
<%= f.input :county_id, collection: County.order(:name), label_method: :name, value_method: :id, label: "County", include_blank: "Select a category of task" %>
<%= f.input :area_id, collection: County.order(:name), as: :grouped_select, group_method: :areas %>
<% end %>
<%= field_set_tag '<span class="label label-info">5</span> Set an initial appointment for this task.'.html_safe, :id => "date_time" do %>
<% f.simple_fields_for :appointments do |b| %>
<div class="row">
<div class="col-md-5 col-md-offset-2"><%= b.input :start_date, label: "Start", wrapper: :horizontal_input_group do %>
<%= b.input_field :start_date, placeholder: 'Start date', class: "form-control date start" %><span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<% end %>
</div>
<div class="col-md-5"><%= b.input :start_time, label: false, wrapper: :horizontal_input_group do %>
<%= b.input_field :start_time, placeholder: 'Start time', as: :string, class: "form-control time start" %><span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-5 col-md-offset-2">
<%= b.input :end_date, label: "End", wrapper: :horizontal_input_group do %>
<%= b.input_field :end_date, placeholder: 'End date', class: "form-control date end" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<% end %>
</div>
<div class="col-md-5">
<%= b.input :end_time, label: false, wrapper: :horizontal_input_group do %>
<%= b.input_field :end_time, placeholder: 'End time', as: :string, class: "form-control time end" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
<% end %>
</div>
<% end %>
<% end %>
<%= f.button :submit %>
<% end %>
它在第一种情况下有效而在第二种情况下无效,这真的很奇怪。
试试这个,
<%= simple_form_for @task, url: tasks_path, method: :post, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
这将强制表单使用 post 方法提交到 tasks_path,该方法会转到创建操作。让我知道它是否有效
此外,这是您的创建方法
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和return这样重定向后的代码块就不会被调用。在你的情况下,没关系,因为其余的都在 else 块中
3) 可能是 task.save 失败并呈现新的。使用调试器并检查是否到达 create
的第一行
在我的应用程序中,我有一个应该由创建操作处理的表单,但它是由新操作处理的。这个问题发生在我的 post 任务表单中,但在我的其他表单(注册表)中,它工作得很好(它由创建操作处理)。所以我认为罪魁祸首可能出在我的模型关系中,所以我也会 post 在这里:
用户模型
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
如果您想知道,我有两个用户在执行任务 table(client_id、worker_id)。我在用户 table 中有一个布尔列来识别他们。我也有约会 table 属于任务,但我没有把它包括在这里。
现在,单击提交按钮后,我从 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"}
我在浏览器上得到这个:
以及调试消息(最后 3 行):
commit: Create Task controller: tasks action: new
它应该是 "action: create" 但它不是。
真是奇葩,为此折腾了一天也解决不了。我正在使用 simple_form 顺便说一句。请帮助我......我是这里的新手:(
在注册表中
<%= simple_form_for @user, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.input :county_id %>
<%= f.input :area_id %>
<%= f.button :submit %>
<% end %>
在post任务中
<%= simple_form_for @task, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
<%= field_set_tag '<span class="label label-info">1</span> What task do you require?'.html_safe do %>
<%= f.input :category_id, collection: Category.order(:name), label_method: :name, value_method: :id, label: "Category", include_blank: "Select a category of task" %>
<%= f.input :subcategory_id, collection: Category.order(:name), as: :grouped_select, group_method: :subcategories %>
<% end %>
<%= field_set_tag '<span class="label label-info">2</span> Tell us about the task.'.html_safe do %>
<%= f.input :title, placeholder: 'Give a descriptive title for your task'%>
<%= f.input :description, placeholder: 'Describe your task here...' %>
<% end %>
<%= field_set_tag '<span class="label label-info">3</span> What budget do you have in mind?'.html_safe do %>
<ul class="nav nav-tabs" id="payTypeTabs">
<%= f.input :pay_type, as: :hidden %>
<li><a href="#fixed-price-tab" data-toggle="tab">Hire for a fixed price</a></li>
<li><a href="#per-hour-tab" data-toggle="tab">Hire per hour</a></li>
</ul>
<h1></h1>
<%= f.input :pay_offer, wrapper: :horizontal_input_group do %>
<span class="input-group-addon">€</span>
<%= f.input_field :pay_offer, as: :integer, class: "form-control" %>
<span class="input-group-addon">Euro</span>
<% end %>
<% end %>
<%= field_set_tag '<span class="label label-info">4</span> Where will your task take place?'.html_safe do %>
<%= f.input :county_id, collection: County.order(:name), label_method: :name, value_method: :id, label: "County", include_blank: "Select a category of task" %>
<%= f.input :area_id, collection: County.order(:name), as: :grouped_select, group_method: :areas %>
<% end %>
<%= field_set_tag '<span class="label label-info">5</span> Set an initial appointment for this task.'.html_safe, :id => "date_time" do %>
<% f.simple_fields_for :appointments do |b| %>
<div class="row">
<div class="col-md-5 col-md-offset-2"><%= b.input :start_date, label: "Start", wrapper: :horizontal_input_group do %>
<%= b.input_field :start_date, placeholder: 'Start date', class: "form-control date start" %><span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<% end %>
</div>
<div class="col-md-5"><%= b.input :start_time, label: false, wrapper: :horizontal_input_group do %>
<%= b.input_field :start_time, placeholder: 'Start time', as: :string, class: "form-control time start" %><span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-5 col-md-offset-2">
<%= b.input :end_date, label: "End", wrapper: :horizontal_input_group do %>
<%= b.input_field :end_date, placeholder: 'End date', class: "form-control date end" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<% end %>
</div>
<div class="col-md-5">
<%= b.input :end_time, label: false, wrapper: :horizontal_input_group do %>
<%= b.input_field :end_time, placeholder: 'End time', as: :string, class: "form-control time end" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
<% end %>
</div>
<% end %>
<% end %>
<%= f.button :submit %>
<% end %>
它在第一种情况下有效而在第二种情况下无效,这真的很奇怪。
试试这个,
<%= simple_form_for @task, url: tasks_path, method: :post, html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>
这将强制表单使用 post 方法提交到 tasks_path,该方法会转到创建操作。让我知道它是否有效
此外,这是您的创建方法
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和return这样重定向后的代码块就不会被调用。在你的情况下,没关系,因为其余的都在 else 块中
3) 可能是 task.save 失败并呈现新的。使用调试器并检查是否到达 create
的第一行