フォームに値を入力した後。f.submitをクリックすると、ビュー(new.html.erb)が更新されます。フォームをデータベースに保存して、インデックスビューにリダイレクトしようとしています。
私は以下を試しました:
- post_controllercreateアクションから条件ステートメントを削除しました。
- ネストされたリソースを(投稿の)単一のリソースに変更しました。
- フォームのURLハッシュを介してルーティングオプションを渡しました。
私はまた、これらのSOの質問の適切なアドバイスに従いましたが、成功しませんでした。
これが私のコードです:
-- 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="✓" /><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
is_link: 'true'
URL: http://firsttimers.co
description: My first time.
commit: save post
action: new
controller: posts
</pre>
</body>
</html>