【问题标题】:Form does not save! rails表格不保存!导轨
【发布时间】:2012-04-13 22:29:06
【问题描述】:

我的目标是半定期更新网站的“关于”部分。我想使用命名资源来做到这一点。我的问题是表单不会保存到数据库中,并且在我单击“创建文本”按钮后会转发到关于页面。

我查看了我的路由、控制器和视图,但我不知道我做错了什么。任何帮助表示赞赏。

这是控制台输出:

Started POST "/about" for 127.0.0.1 at 2012-04-13 17:45:29 -0700
Processing by StaticPagesController#about as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kmkSyhhqrHiwtIEMd8R1+EEh1ic7688h7lNFW9FnMUM=", "text"=>   {"title"=>"", "text"=>"asdfasd"}, "commit"=>"Create Text"}
Text Load (0.2ms)  SELECT "texts".* FROM "texts" WHERE "texts"."id" = ? LIMIT 1  [["id", 6]]
Text Load (0.2ms)  SELECT "texts".* FROM "texts" WHERE "texts"."id" = ? LIMIT 1  [["id", 7]]
Text Load (0.1ms)  SELECT "texts".* FROM "texts" WHERE "texts"."id" = ? LIMIT 1  [["id", 8]]
Rendered static_pages/about.html.erb within layouts/application (0.7ms)
Rendered shared/_header.html.erb (1.5ms)
Rendered shared/_footer.html.erb (1.1ms)
Completed 200 OK in 30ms (Views: 15.5ms | ActiveRecord: 1.1ms)

这是 about.html.erb 视图:

 1 <div id="container_about">
 2   <%= image_tag "chocolate_lady.jpg", id: "about_image" %>  
 3   <div id="right_column_about" > 
 4     <h1> Who we are </h1>
 5     <%= @text1.text %> 
 6     <br /> 
 7     <br /> 
 8     <%= @text2.text %>
 9     <br />
 10     <br /> 
 11     <%= @text3.text %>
 12   </div> 
 13 </div>   

这是表单视图:

4   <%= flash[:notice] %>
5   <%= flash[:status] %>
6   
7 <%= form_for @about, :url => { :action => 'save_about'}  do |f| %>
8 
9   <div class="field">
10     <%= f.label :title %><br />
11     <%= f.text_field :title %>
12   </div>
13   <div class="field">
14     <%= f.label :text %><br />
15     <%= f.text_area :text %>
16   </div>
17 
18   <div class="actions">
19     <%= f.submit %>
20   </div>
21 <% end %>
22 

表单html:

<form accept-charset="UTF-8" action="/about" class="new_text" id="new_text" method="post"><div    style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token"   type="hidden" value="kmkSyhhqrHiwtIEMd8R1+EEh1ic7688h7lNFW9FnMUM="></div>

<div class="field">
<label for="text_title">Title</label><br>
<input id="text_title" name="text[title]" size="30" type="text">
</div>
<div class="field">
<label for="text_text">Text</label><br>
<textarea cols="40" id="text_text" name="text[text]" rows="20"></textarea>
</div>

 <div class="actions">
<input name="commit" type="submit" value="Create Text">
 </div>
</form>

这是我的 rake 路线:

product_photos GET    /product_photos(.:format)          product_photos#index
               POST   /product_photos(.:format)          product_photos#create
new_product_photo GET    /product_photos/new(.:format)      product_photos#new
edit_product_photo GET    /product_photos/:id/edit(.:format) product_photos#edit
product_photo GET    /product_photos/:id(.:format)      product_photos#show
               PUT    /product_photos/:id(.:format)      product_photos#update
               DELETE /product_photos/:id(.:format)      product_photos#destroy
          root        /                                  static_pages#home
      products        /products(.:format)                ProductPhotos#index
         about        /about(.:format)                   static_pages#about
       contact        /contact(.:format)                 static_pages#contact
        policy        /policy(.:format)                  static_pages#policy
      new_text        /about/new(.:format)               static_pages#new_about
     show_text        /about/:id(.:format)               static_pages#show_about
               POST   /about(.:format)                   static_pages#save_about

这是我的路线:

 7   match '/products' => 'ProductPhotos#index'
 8   match '/about' => 'static_pages#about' 
 9   match '/contact' => 'static_pages#contact' 
10   match '/policy' => 'static_pages#policy'
11   match '/about/new/' => 'static_pages#new_about', :as => :new_text
12   match '/about/:id' => 'static_pages#show_about', :as => :show_text
13   match '/about/' => 'static_pages#save_about', via: 'post'

这是我的控制器:

12   def show_about
13     @about = Text.find(params[:id])
14 
15     respond_to do |format|
16       format.html
17       format.json { render json: @about }
18     end 
19   end
20 
21   def new_about  
22     @about = Text.new 
23     respond_to do |format|
24       format.html # new_about.html.erb
25       format.json { render json: @about }
26     end
27   end
28 
29   def save_about
30     @about = Text.new(params[:text]) 
31     
32     respond_to do |format|
33       if @about.save
34         format.html { redirect_to show_about_path(@about), notice: 'Product photo was successfully created.' }
35         format.json { render json: @about, status: :created, location: @about }
36       else
37         format.html { redirect_to action: 'new_about',  notice: 'Product photo wasn\'t save'}
38         format.json { render json: @about.errors, status: :unprocessable_entity }
39       end
40     end
41   end
42   

【问题讨论】:

  • 您的 html 是否使用 method=post 正确呈现表单?
  • 是的,我会更新帖子以显示这一点。
  • match '/about/' =&gt; 'static_pages#save_about', via: 'post' 似乎不对。它应该与/about 匹配,并且没有斜杠。如果这不起作用,请查看您的路线顺序。也许之前定义的一条路径正在使用该帖子。
  • 我刚刚添加了 rails 的输出。它通过了帖子,但加载了原始关于页面。
  • @Benjamin:试过了,我一定会尝试切换顺序。

标签: ruby-on-rails forms routes


【解决方案1】:

8 match '/about' =&gt; 'static_pages#about' 匹配与13 match '/about/' =&gt; 'static_pages#save_about', via: 'post' 相同的路由

由于它们都匹配 /about 并且通用的是第一个,因此将使用这个。要么将更具体的路由放在match '/about/' =&gt; 'static_pages#save_about', via: 'post' 前面,然后将通用路由放在后面,或者更改通用路由以使其像match '/about' =&gt; 'static_pages#about', via: :get 一样监听:get。通过特别添加:get,它永远不会消耗:post

基本上,当您运行 rake routes 时,您希望看到每个路径的 HTTP 方法,例如

用户 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

【讨论】:

  • 我在这上面花了一天时间。可能是一个星期!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
相关资源
最近更新 更多