Rails编辑表单

huangapple go评论119阅读模式
英文:

Rails Edit Form

问题

  1. 当我想进入编辑页面时,我一直在遇到这个错误:“NO ROUTE MATCHES”,但奇怪的是,当我将 **order = @order** 更改为 **@order.listing** 时,一切都正常,但没有信息可供编辑,我已经为这个错误苦恼了一段时间。
  2. **这是我的 Orders 控制器:**
  3. ```ruby
  4. class OrdersController < ApplicationController
  5. before_action :set_order, only: [:show, :edit, :update, :destroy]
  6. before_action :authenticate_user!
  7. # GET /orders
  8. # GET /orders.json
  9. def index
  10. @orders = Order.all
  11. end
  12. # GET /orders/1
  13. # GET /orders/1.json
  14. def show
  15. end
  16. # GET /orders/new
  17. def new
  18. @order = Order.new
  19. @listing = Listing.find(params[:listing_id])
  20. end
  21. # GET /orders/1/edit
  22. def edit
  23. end
  24. # POST /orders
  25. # POST /orders.json
  26. def create
  27. @order = Order.new(order_params)
  28. @listing = Listing.find(params[:listing_id])
  29. @seller = @listing.user
  30. @order.listing_id = @listing.id
  31. @order.buyer_id = current_user.id
  32. @order.seller_id = @seller.id
  33. respond_to do |format|
  34. if @order.save
  35. format.html { redirect_to root_url, notice: 'Pedido creado' }
  36. format.json { render :show, status: :created, location: @order }
  37. else
  38. format.html { render :new }
  39. format.json { render json: @order.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43. # PATCH/PUT /orders/1
  44. # PATCH/PUT /orders/1.json
  45. def update
  46. respond_to do |format|
  47. if @order.update(order_params)
  48. format.html { redirect_to @order, notice: 'El pedido fue actualizado' }
  49. format.json { render :show, status: :ok, location: @order }
  50. else
  51. format.html { render :edit }
  52. format.json { render json: @order.errors, status: :unprocessable_entity }
  53. end
  54. end
  55. end
  56. # DELETE /orders/1
  57. # DELETE /orders/1.json
  58. def destroy
  59. @order.destroy
  60. respond_to do |format|
  61. format.html { redirect_to orders_url, notice: 'El pedido fue eliminado con exito' }
  62. format.json { head :no_content }
  63. end
  64. end
  65. private
  66. # Use callbacks to share common setup or constraints between actions.
  67. def set_order
  68. @order = Order.find(params[:id])
  69. end
  70. # Only allow a list of trusted parameters through.
  71. def order_params
  72. params.require(:order).permit(:address, :city, :state)
  73. end
  74. end

我的编辑页面:

  1. <h1>编辑订单</h1>
  2. <%= render 'form', order: @order %>
  3. <%= link_to '返回', listing_orders_path %>

表单:

  1. <%= form_for(model: [@listing, order], local: true) do |form| %>
  2. <% if order.errors.any? %>
  3. <div id="error_explanation">
  4. <h2><%= pluralize(order.errors.count, "error") %> 阻止保存此订单:</h2>
  5. <ul>
  6. <% order.errors.full_messages.each do |message| %>
  7. <li><%= message %></li>
  8. <% end %>
  9. </ul>
  10. </div>
  11. <% end %>
  12. <div class="field">
  13. <%= form.label :address %>
  14. <%= form.text_field :address %>
  15. </div>
  16. <div class="field">
  17. <%= form.label :city %>
  18. <%= form.text_field :city %>
  19. </div>
  20. <div class="field">
  21. <%= form.label :state %>
  22. <%= form.text_field :state %>
  23. </div>
  24. <div class="actions">
  25. <%= form.submit %>
  26. </div>
  27. <% end %>

附加信息:

routes.rb:

  1. Rails.application.routes.draw do
  2. devise_for :users
  3. resources :listings do
  4. resources :orders
  5. end
  6. end

rake routes:

  1. Prefix Verb URI Pattern Controller#Action
  2. ...
  3. listing_orders GET /listings/:listing_id/orders(.:format) orders#index
  4. POST /listings/:listing_id/orders(.:format) orders#create
  5. new_listing_order GET /listings/:listing_id/orders/new(.:format) orders#new
  6. edit_listing_order GET /listings/:listing_id/orders/:id/edit(.:format) orders#edit
  7. listing_order GET /listings/:listing_id/orders/:id(.:format) orders#show
  8. PATCH /listings/:listing_id/orders/:id(.:format) orders#update
  9. PUT /listings/:listing_id/orders/:id(.:format) orders#update
  10. DELETE /listings/:listing_id/orders/:id(.:format) orders#destroy
  11. ...
  1. <details>
  2. <summary>英文:</summary>
  3. I keep getting this error when i want to enter the edit page &quot;NO ROUTE MATCHES&quot; ,but the weird thing is that when i change the **order = @order to @order.listing** it goes fine but there is no info to be edited, and i been scratching my head with this error for a while.
  4. **This is my Orders Controller:**
  5. ```ruby
  6. class OrdersController &lt; ApplicationController
  7. before_action :set_order, only: [:show, :edit, :update, :destroy]
  8. before_action :authenticate_user!
  9. # GET /orders
  10. # GET /orders.json
  11. def index
  12. @orders = Order.all
  13. end
  14. # GET /orders/1
  15. # GET /orders/1.json
  16. def show
  17. end
  18. # GET /orders/new
  19. def new
  20. @order = Order.new
  21. @listing = Listing.find(params[:listing_id])
  22. end
  23. # GET /orders/1/edit
  24. def edit
  25. end
  26. # POST /orders
  27. # POST /orders.json
  28. def create
  29. @order = Order.new(order_params)
  30. @listing = Listing.find(params[:listing_id])
  31. @seller = @listing.user
  32. @order.listing_id = @listing.id
  33. @order.buyer_id = current_user.id
  34. @order.seller_id = @seller.id
  35. respond_to do |format|
  36. if @order.save
  37. format.html { redirect_to root_url, notice: &#39;Pedido creado&#39; }
  38. format.json { render :show, status: :created, location: @order }
  39. else
  40. format.html { render :new }
  41. format.json { render json: @order.errors, status: :unprocessable_entity }
  42. end
  43. end
  44. end
  45. # PATCH/PUT /orders/1
  46. # PATCH/PUT /orders/1.json
  47. def update
  48. respond_to do |format|
  49. if @order.update(order_params)
  50. format.html { redirect_to @order, notice: &#39;El pedido fue actualizado&#39; }
  51. format.json { render :show, status: :ok, location: @order }
  52. else
  53. format.html { render :edit }
  54. format.json { render json: @order.errors, status: :unprocessable_entity }
  55. end
  56. end
  57. end
  58. # DELETE /orders/1
  59. # DELETE /orders/1.json
  60. def destroy
  61. @order.destroy
  62. respond_to do |format|
  63. format.html { redirect_to orders_url, notice: &#39;El pedido fue eliminado con exito&#39; }
  64. format.json { head :no_content }
  65. end
  66. end
  67. private
  68. # Use callbacks to share common setup or constraints between actions.
  69. def set_order
  70. @order = Order.find(params[:id])
  71. end
  72. # Only allow a list of trusted parameters through.
  73. def order_params
  74. params.require(:order).permit(:address, :city, :state)
  75. end
  76. end

My Edit Page:

  1. &lt;h1&gt;Editing Order&lt;/h1&gt;
  2. &lt;%= render &#39;form&#39;, order: @order %&gt;
  3. &lt;%= link_to &#39;Atras&#39;, listing_orders_path %&gt;

Form:

  1. &lt;%= form_for(model: [@listing, order], local: true) do |form| %&gt;
  2. &lt;% if order.errors.any? %&gt;
  3. &lt;div id=&quot;error_explanation&quot;&gt;
  4. &lt;h2&gt;&lt;%= pluralize(order.errors.count, &quot;error&quot;) %&gt; prohibited this order from being saved:&lt;/h2&gt;
  5. &lt;ul&gt;
  6. &lt;% order.errors.full_messages.each do |message| %&gt;
  7. &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
  8. &lt;% end %&gt;
  9. &lt;/ul&gt;
  10. &lt;/div&gt;
  11. &lt;% end %&gt;
  12. &lt;div class=&quot;field&quot;&gt;
  13. &lt;%= form.label :address %&gt;
  14. &lt;%= form.text_field :address %&gt;
  15. &lt;/div&gt;
  16. &lt;div class=&quot;field&quot;&gt;
  17. &lt;%= form.label :city %&gt;
  18. &lt;%= form.text_field :city %&gt;
  19. &lt;/div&gt;
  20. &lt;div class=&quot;field&quot;&gt;
  21. &lt;%= form.label :state %&gt;
  22. &lt;%= form.text_field :state %&gt;
  23. &lt;/div&gt;
  24. &lt;div class=&quot;actions&quot;&gt;
  25. &lt;%= form.submit %&gt;
  26. &lt;/div&gt;
  27. &lt;% end %&gt;

ADDITIONAL INFO:

Routes.rb:

  1. Rails.application.routes.draw do
  2. devise_for :users
  3. resources :listings do
  4. resources :orders
  5. end
  6. end

Rake routes:

  1. Prefix Verb URI Pattern Controller#Action
  2. new_user_session GET /users/sign_in(.:format) devise/sessions#new
  3. user_session POST /users/sign_in(.:format) devise/sessions#create
  4. destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
  5. new_user_password GET /users/password/new(.:format) devise/passwords#new
  6. edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
  7. user_password PATCH /users/password(.:format) devise/passwords#update
  8. PUT /users/password(.:format) devise/passwords#update
  9. POST /users/password(.:format) devise/passwords#create
  10. cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
  11. new_user_registration GET /users/sign_up(.:format) devise/registrations#new
  12. edit_user_registration GET /users/edit(.:format) devise/registrations#edit
  13. user_registration PATCH /users(.:format) devise/registrations#update
  14. PUT /users(.:format) devise/registrations#update
  15. DELETE /users(.:format) devise/registrations#destroy
  16. POST /users(.:format) devise/registrations#create
  17. listing_orders GET /listings/:listing_id/orders(.:format) orders#index
  18. POST /listings/:listing_id/orders(.:format) orders#create
  19. new_listing_order GET /listings/:listing_id/orders/new(.:format) orders#new
  20. edit_listing_order GET /listings/:listing_id/orders/:id/edit(.:format) orders#edit
  21. listing_order GET /listings/:listing_id/orders/:id(.:format) orders#show
  22. PATCH /listings/:listing_id/orders/:id(.:format) orders#update
  23. PUT /listings/:listing_id/orders/:id(.:format) orders#update
  24. DELETE /listings/:listing_id/orders/:id(.:format) orders#destroy
  25. listings GET /listings(.:format) listings#index
  26. POST /listings(.:format) listings#create
  27. new_listing GET /listings/new(.:format) listings#new
  28. edit_listing GET /listings/:id/edit(.:format) listings#edit
  29. listing GET /listings/:id(.:format) listings#show
  30. PATCH /listings/:id(.:format) listings#update
  31. PUT /listings/:id(.:format) listings#update
  32. DELETE /listings/:id(.:format) listings#destroy
  33. pages_about GET /pages/about(.:format) pages#about
  34. pages_contact GET /pages/contact(.:format) pages#contact
  35. seller GET /seller(.:format) listings#seller
  36. root GET / listings#index
  37. rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
  38. rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
  39. rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
  40. rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check
  41. rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
  42. rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
  43. rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
  44. POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
  45. new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
  46. edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
  47. rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
  48. PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
  49. PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
  50. DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
  51. rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
  52. rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
  53. rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
  54. rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
  55. update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
  56. rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create

答案1

得分: 0

你已将 orders 设置为 listings 下的嵌套资源:

  1. resources :listings do
  2. resources :orders
  3. end

这意味着 - 正如从你的路由输出中可以看出的那样 - 编辑订单的 URL 路径为:

  1. /listings/:listing_id/orders/:id/edit

我认为你没有包含链接到编辑页面的代码,但我猜你可能在使用 Rails 生成的 URL 辅助方法,edit_listing_order_path,它需要两个参数:listing_id 和订单 ID。如果你检查那个链接,我的猜测是你没有同时指定这两个 ID。我之所以这么说,是因为如果你看一下编辑页面上的 link_to

  1. <%= link_to 'Atras', listing_orders_path %>

它缺少了适当的 listing 记录的 ID:

  1. <%= link_to 'Atras', listing_orders_path(@listing) %>

我怀疑你需要检查所有的订单路径,以确保你也在指定父级 listing。

你可能还需要在你的 OrdersController 中加载 listing 实例:

  1. def set_order
  2. @order = Order.find(params[:id])
  3. @listing = @order.listing # 或者从参数中获取:Listing.find(params([:listing_id])
  4. end
英文:

You have orders set up as a nested resource under listings:

  1. resources :listings do
  2. resources :orders
  3. end

That means -- as you can see from your routes output -- that the URL path for editing an order is:

  1. /listings/:listing_id/orders/:id/edit

I don't think you included the code for the link to the edit page, but my guess is you are using the Rails-generated URL helper, edit_listing_order_path, which takes two parameters: a listing_id and an order ID. If you check that link my guess is you aren't specifying both IDs. The reason I say that is that if you look at the link_to on the edit page:

  1. &lt;%= link_to &#39;Atras&#39;, listing_orders_path %&gt;

It is missing the ID of the appropriate listing record:

  1. &lt;%= link_to &#39;Atras&#39;, listing_orders_path(@listing) %&gt;

I suspect you need to check all the orders paths to ensure you are also specifying the parent listing.

The other thing you likely need to do is load the listing instance in your OrdersController:

  1. def set_order
  2. @order = Order.find(params[:id])
  3. @listing = @order.listing # or from parameters: Listing.find(params([:listing_id])
  4. end

huangapple
  • 本文由 发表于 2020年7月24日 07:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定