2012-10-21 51 views
0

填寫完值後,點擊f.submit只是刷新我的看法(new.html.erb)。我試圖將表單保存到我的數據庫並重定向到我的索引視圖。Rails 3.2.8 form_for提交按鈕正在使用「新」操作而不是「創建」操作

我曾嘗試以下:

  1. 刪除從posts_controller條件語句創建行動。
  2. 將我的嵌套資源更改爲單數資源(職位)。
  3. 通過我的表單中的URL散列傳遞路由選項。

我也在這些SO問題中遵循了相關的建議,但都沒有成功。

  1. Rails Create Action Not Workinghttps://stackoverflow.com/editing-help

  2. rails form_for never invokes the create controller action to use redirect_to

  3. Rails form posting to /new instead of /create?

這裏是我的代碼:

-- views/posts/index.html.erb 
<h5>Posts#index</h5> 
<small><i>Find me in app/views/posts/index.html.erb</i></small> 
<hr /> 

<div class="hero hero-unit"> 
    <button><%= link_to "Create a post", new_post_path %></button> 
    <hr /> 

    <ul class="unstyled"> 
     <% @posts.each do |post| %> 

     <h5><small>post ID: </small><%= post.id %></h5> 
     <li><%= post.URL %></li> 
     <li> 
      <%= link_to post.description, post %> 
     </li> 
     <li> 
      <ul class="unstyled"> 
       <li><small><i>submitted <%= time_ago_in_words(post.created_at) %> ago.</i></small></li> 
      </ul> 
     </li> 
     <li> 
      <%= pluralize(post.comments.count, 'comment') %> 
     </li> 
     <!-- votes moved to votes/index.html.erb --> 
     <li> 
      <button class="btn btn-mini btn-success"> 
       <%= link_to '', post_votes_path(post, vote: "true"), method: :post, remote: true, class: 'icon-arrow-up' %> 
      </button> 

      <button class="btn btn-mini btn-danger"> 
       <%= link_to '', post_votes_path(post, vote: "false"), method: :post, remote: true, class: 'icon-arrow-down' %> 
      </button> 
      <p><%= post.vote_total.to_s + " votes" %></p> 
      <!-- 
       <span id="post_<%= post.id %>_votes"><%= post.vote_total.to_s + " votes" %></span> 
      --> 
     </li> 
     <% end %> 
     <hr /> 
    </ul> 
</div><!-- END hero, hero-unit CLASS --> 



-- views/posts/new.html.erb 

    <h5>Posts#new</h5> 
<small><i>Find me in app/views/posts/new.html.erb</i></small> 
<hr /> 

<div class="hero hero-unit"> 
    <form class="form-horizontal"> 
     <%= form_for @post do |f| %> 
     <div class="control-group"> 
      <label class="control-label"> 
       <%= f.label "Select post type" %> 
      </label> 
      <div class="controls"> 
       <%= f.select :is_link, options_for_select([["Link", true], ["Text", false]]) %> 
      </div> 
     </div> 

     <div class="control-group"> 
      <label class="control-label" for="URL"><%= f.label :URL %></label> 
      <div class="controls"> 
       <%= f.text_field :URL %> 
      </div> 
     </div> 

     <div class="control-group"> 
      <label class="control-label" for="description"><%= f.label :description %></label> 
      <div class="controls"> 
       <%= f.text_area :description, rows: 5 %> 
      </div><br /> 
      <div class="controls"> 
       <%= f.submit 'save post' %> 
       <% end %> 
      </div> 
     </div> 
    </form> 
</div><!-- end hero, hero-unit CLASS --> 



-- posts_controller.rb 

class PostsController < ApplicationController 
    def index 
    @posts = Post.find(:all) 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(params[:post]) 

    redirect_to root_path 
    end 

    def show 
    # Post 
    @post = Post.find(params[:id]) 

    # Comments assiciated by :post_id 
    @comments = @post.comments.all 
    @comment = @post.comments.build(params[:comment]) 
    @comment.save 
    end 

    def edit 
    @post = Post.find(params[:id]) 
    end 
end 




-- config/routes.rb 

    root to: 'posts#index' 

    resources :posts do 
    resources :comments 
    resources :votes 
    end 
. 
. 
. 
#match ':controller(/:action(/:id))(.:format)' 

--Terminal rake routes 

      root  /           posts#index 
    post_comments GET /posts/:post_id/comments(.:format)   comments#index 
        POST /posts/:post_id/comments(.:format)   comments#create 
new_post_comment GET /posts/:post_id/comments/new(.:format)  comments#new 
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit 
    post_comment GET /posts/:post_id/comments/:id(.:format)  comments#show 
        PUT /posts/:post_id/comments/:id(.:format)  comments#update 
        DELETE /posts/:post_id/comments/:id(.:format)  comments#destroy 
     post_votes GET /posts/:post_id/votes(.:format)    votes#index 
        POST /posts/:post_id/votes(.:format)    votes#create 
    new_post_vote GET /posts/:post_id/votes/new(.:format)   votes#new 
    edit_post_vote GET /posts/:post_id/votes/:id/edit(.:format) votes#edit 
     post_vote GET /posts/:post_id/votes/:id(.:format)   votes#show 
        PUT /posts/:post_id/votes/:id(.:format)   votes#update 
        DELETE /posts/:post_id/votes/:id(.:format)   votes#destroy 
      posts GET /posts(.:format)       posts#index 
        POST /posts(.:format)       posts#create 
     new_post GET /posts/new(.:format)      posts#new 
     edit_post GET /posts/:id/edit(.:format)     posts#edit 
      post GET /posts/:id(.:format)      posts#show 
        PUT /posts/:id(.:format)      posts#update 
        DELETE /posts/:id(.:format)      posts#destroy 


--WEBrick 

Started GET "/posts/new?utf8=%E2%9C%93&authenticity_token=Z8LuvU7i5ytFi1OxiIGpgwYOy%2BWE%2BfiAZv7m7T%2BwYRI%3D&post%5Bis_link%5D=true&post%5BURL%5D=http%3A%2F%2Ffirsttimers.co&post%5Bdescription%5D=My+first+time.&commit=save+post" for 127.0.0.1 at 2012-10-20 22:37:52 -0400 
Processing by PostsController#new as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"Z8LuvU7i5ytFi1OxiIGpgwYOy+WE+fiAZv7m7T+wYRI=", "post"=>{"is_link"=>"true", "URL"=>"http://firsttimers.co", "description"=>"My first time."}, "commit"=>"save post"} 
    Rendered posts/new.html.erb within layouts/application (2.0ms) 
Completed 200 OK in 12ms (Views: 11.8ms | ActiveRecord: 0.0ms) 

任何幫助解決方案和/或解釋什麼我俯視和發生了什麼(我希望它不是一個錯字...大聲笑)將不勝感激。

-- EDIT. HTML Generated code for posts/new.html.erb 

<!DOCTYPE html> 
<html> 
<head> 
    <title>PostEmHigh</title> 
    <link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" /> 
<link href="/assets/custom.css?body=1" media="all" rel="stylesheet" type="text/css" /> 
<link href="/assets/posts.css?body=1" media="all" rel="stylesheet" type="text/css" /> 
<link href="/assets/scaffolds.css?body=1" media="all" rel="stylesheet" type="text/css" /> 
    <script src="/assets/jquery.js?body=1" type="text/javascript"></script> 
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script> 
<script src="/assets/posts.js?body=1" type="text/javascript"></script> 
<script src="/assets/application.js?body=1" type="text/javascript"></script> 
    <script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script> 
    <meta content="authenticity_token" name="csrf-param" /> 
<meta content="Z8LuvU7i5ytFi1OxiIGpgwYOy+WE+fiAZv7m7T+wYRI=" name="csrf-token" /> 
</head> 
<body> 

    <h5>Posts#new</h5> 
<small><i>Find me in app/views/posts/new.html.erb</i></small> 
<hr /> 

<div class="hero hero-unit"> 
    <form class="form-horizontal"> 
     <form accept-charset="UTF-8" action="/posts" class="new_post" id="new_post" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="Z8LuvU7i5ytFi1OxiIGpgwYOy+WE+fiAZv7m7T+wYRI=" /></div> 
     <div class="control-group"> 
      <label class="control-label"> 
       <label for="post_Select post type">Select post type</label> 
      </label> 
      <div class="controls"> 
       <select id="post_is_link" name="post[is_link]"><option value="true">Link</option> 
<option value="false">Text</option></select> 
      </div> 
     </div> 

     <div class="control-group"> 
      <label class="control-label" for="URL"><label for="post_URL">Url</label></label> 
      <div class="controls"> 
       <input id="post_URL" name="post[URL]" size="30" type="text" /> 
      </div> 
     </div> 

     <div class="control-group"> 
      <label class="control-label" for="description"><label for="post_description">Description</label></label> 
      <div class="controls"> 
       <textarea cols="40" id="post_description" name="post[description]" rows="5"> 
</textarea> 
      </div><br /> 
      <div class="controls"> 
       <input name="commit" type="submit" value="save post" /> 
</form>   </div> 
     </div> 
    </form> 
</div><!-- end hero, hero-unit CLASS --> 

    <pre class='debug_dump'>--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
utf8: ✓ 
authenticity_token: Z8LuvU7i5ytFi1OxiIGpgwYOy+WE+fiAZv7m7T+wYRI= 
post: !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
&nbsp; is_link: &#x27;true&#x27; 
&nbsp; URL: http://firsttimers.co 
&nbsp; description: My first time. 
commit: save post 
action: new 
controller: posts 
</pre> 
</body> 
</html> 
+0

我剛卸載並重新安裝導軌。這並沒有解決問題。 – sirramongabriel

回答

1

刪除形式幫助鏈接選項,然後再試

可以肯定它不是來自你,

運行腳手架發電機您的文章資源

rails g scaffold post is_link:boolean url description:text 

你可以使用twitter bootstrap主題生成器選項來重新生成引導程序樣式,如果你想

+0

您好Uchenna,我已編輯此代碼以反映您的指示。恐怕我的提交按鈕還沒有觸及創建動作。 – sirramongabriel

+0

您是否將您的Rails應用升級到Rails 3.2.8的舊版Rails代碼(基於您的索引操作) – Uchenna

+0

它表示另一次遷移已命名爲create_posts。我不太確定發生了什麼事。我是否在運行rails g scaffold以確保我沒有任何拼寫錯誤或路由不一致? BTW thx幫助在星期六晚上!是的,我的gemfile說3.2.8 – sirramongabriel

0

您可以複製爲您的表單生成的HTML代碼嗎?此外,請嘗試以下操作:

form_for @post, :url => posts_path(), :method => :post 

並複製表單的HTML代碼。

+0

您好edelpero,我添加了HTML生成的代碼。它位於我的問題的末尾。此外,我添加了網址哈希和方法沒有成功。 – sirramongabriel

相關問題