【发布时间】:2014-04-11 22:51:55
【问题描述】:
我已尝试通读 form_for Ruby 文档,但仍然很难理解其中的区别。
加载 new.html.erb 视图时,:post 有效,而 @post 无效。这是相关的视图和控制器:
This is Post's new.html.erb
<%= form_for(:post) do |f| %>
<%= f.text_area :note, value: "Say something" %><br>
<%= f.submit "Post" %>
<% end %>
后控制器:
class PostsController < ApplicationController
before_action :signed_in_user, only: [:new, :create]
def index
@posts = Post.all
end
def new
end
def create
@post = current_user.posts.build
puts "This is #{@post.user_id} user"
redirect_to posts_path if @post.save #post/index.html.erb
end
def destroy
end
private
def signed_in_user
redirect_to signout_path, notice: "Please sign in." unless signed_in?
end
end
【问题讨论】:
-
您尝试过 guides.rubyonrails.org 吗?
-
是的,不清楚。
标签: ruby-on-rails instance-variables form-for