Rails 3.2.8 form_for提交按钮正在使用' new'行动而不是创造'行动

时间:2012-10-21 02:46:05

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

用我的表格填写值。单击f.submit只刷新我的视图(new.html.erb)。我正在尝试将表单保存到我的数据库并重定向到我的索引视图。

我尝试了以下内容:

  1. 从posts_controller创建操作中删除条件语句。
  2. 将我的嵌套资源更改为单一资源(帖子)。
  3. 通过我的表单中的url哈希传递路由选项。
  4. 我也遵循了这些SO问题中的相关建议,但没有成功。

    1. Rails Create Action Not Working https://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?

    4. 这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

删除表单助手中的url选项,然后重试

确定它不是来自你,

为您的帖子资源运行脚手架生成器

rails g scaffold post is_link:boolean url description:text

如果需要,可以使用twitter bootstrap主题生成器选项重新生成引导样式

答案 1 :(得分:0)

您可以复制为表单生成的HTML代码吗?另外,试试这个:

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

并复制表单的HTML代码。