您从外部来源提交form_for时遇到2个问题。我正在使用Jquery ajax。它是一个窗口小部件的一个窗体,当用户在网站上添加代码时添加它(js)
这是我正在使用的Haml代码
= form_for [@deck.user, @deck, @deck.recipients.new], html: { id: "emailform" }, remote: true do |f|
= f.text_field :sender_name, placeholder: "Your name"
= f.label :sender_name, "Your name", class: "name-label"
= f.text_field :email, placeholder: "Email of your friend"
= f.label :email, "Email of your friend", class: "email-label"
.email-send#submit
= f.submit(type: "image", alt: "send email", src: image_url("widget/email.png"))
问题1: 它在主站点上没有问题。 但它应该将我们网站的根网址添加到路径中 怎么做?
问题2: 当我手动添加根URL并测试它时,它会返回以下错误: 格式未知 这可能是因为它试图提交html而不是js格式。怎么解决?
答案 0 :(得分:0)
尝试类似
的内容= form_for [@deck.user, @deck, @deck.recipients.new], html: { id: "emailform" }, remote: true, url: root_url_of_main_site , format: :js do |f|
root_url_of_main_site可以很容易地成为一个字符串,因为显然这个表单是由另一个rails应用程序在另一个中呈现的
答案 1 :(得分:0)
= form_for @deck.user, url: something_url , id: "emailform", remote: true do |f|
= f.text_field :sender_name, placeholder: "Your name"
= f.label :sender_name, "Your name", class: "name-label"
= f.text_field :email, placeholder: "Email of your friend"
= f.label :email, "Email of your friend", class: "email-label"
.email-send#submit
= f.submit(type: "image", alt: "send email", src: image_url("widget/email.png"))
something_url
是您要将表单提交到的路径。您最有可能想使用命名路由帮助程序。
嵌套超过2个级别的网址往往非常繁琐,并导致一堆令人困惑的依赖问题。
resources :users, shallow: true do
resources :decks
end
resources :decks, only: [], shallow: true do
resources :recipients
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
user_decks GET /users/:user_id/decks(.:format) decks#index
POST /users/:user_id/decks(.:format) decks#create
new_user_deck GET /users/:user_id/decks/new(.:format) decks#new
edit_deck GET /decks/:id/edit(.:format) decks#edit
deck GET /decks/:id(.:format) decks#show
PATCH /decks/:id(.:format) decks#update
PUT /decks/:id(.:format) decks#update
DELETE /decks/:id(.:format) decks#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
deck_recipients GET /decks/:deck_id/recipients(.:format) recipients#index
POST /decks/:deck_id/recipients(.:format) recipients#create
new_deck_recipient GET /decks/:deck_id/recipients/new(.:format) recipients#new
edit_recipient GET /recipients/:id/edit(.:format) recipients#edit
recipient GET /recipients/:id(.:format) recipients#show
PATCH /recipients/:id(.:format) recipients#update
PUT /recipients/:id(.:format) recipients#update
DELETE /recipients/:id(.:format) recipients#destroy
这可让我们发布到deck_recipients_url(deck_id: @deck)