当我导航到
下面的导轨应用程序时我想创建一个新标签。我点击保存并验证开始并要求我插入标签名称,但我的网址更改为
即使在我的标签控制器中的创建操作中,我说
render :new
为什么呢? 有没有办法确保它永远是/ new
注意我简化了示例。实际情况稍微复杂一些。请参阅下面的代码。
myurl.co.za/courses/ [我的课程名称] / course_registrations /新
更改为
myurl.co.za/courses/ [我的课程名称] / course_registrations
class CourseRegistrationsController < ApplicationController
before_filter :set_to_use_course_registration_partials
def new
@course = Course.includes(:partner).friendly.find(params[:course_id])
redirect_to courses_path if !@course.is_published && !current_user.try(:is_admin?)
@course_registration = CourseRegistration.new
gon.perform_frontend_validation = 'false'
end
def create
@course = Course.includes(:partner).friendly.find(params[:course_id])
@course_registration = CourseRegistration.new(course_registration_params)
@course_registration.course_id = @course.id
@course_registration.presentation = @course.presentation
if @course_registration.save
@course_registration.subscribe_to_newsletter
flash[:success] = "Registration Successful"
redirect_to action: "success"
else
gon.perform_frontend_validation = 'true'
flash.now[:error] = "Please ensure all fields have been filled in"
render action: 'new'
end
end
def success
@course = Course.includes(:partner).friendly.find(params[:course_id])
end
private
# ensures correct navigation and flash rendered in application layout
def set_to_use_course_registration_partials
@use_course_registration_partials = true
end
def course_registration_params
params.require(:course_registration).permit(:first_name,
:last_name,
:email,
:telephone,
:identity_number,
:date_of_birth,
:address_street_1,
:address_street_2,
:address_city,
:address_state,
:address_postal_code,
:address_country,
:billing_address_street_1,
:billing_address_street_2,
:billing_address_city,
:billing_address_state,
:billing_address_postal_code,
:billing_address_country,
:is_billing_same_as_physical_address,
:payment_scheme,
:payment_method,
:promo_code,
:require_tax_invoice,
:tax_invoice_company_name,
:tax_invoice_vat_number,
:crm_reg_id,
:receive_newsletter,
:terms_and_conditions)
end
end
答案 0 :(得分:1)
这是因为当您从新操作提交表单时,您正在发布到http://myurl.co.za/tags而不是http://myurl.co.za/tags/new。然后出现的表单是对该POST操作的响应,因此它出现在相应的URL上。所以行为是Restful,因为它具有uri和HTTP动作的独特组合。
您可以更改应用的行为,以便创建处理POST到http://myurl.co.za/tags/new的POST。这也很安静,您可以通过修改路线来实现:
resources :tags do
collection do
post 'new', action: :create, as: :create_tag
end
end
然后,您需要确保在新视图中呈现的表单指定路径create_tag_path作为其目标网址。
最后我要强调,这个和标准的Rails行为都是Restful。它们只是Restful的不同实现。