yuyanegishi / issue_check

0 stars 0 forks source link

★Rails Turtorial:Micropost作成 #4

Open yuyanegishi opened 5 years ago

yuyanegishi commented 5 years ago
def create
      @micropost = current_user.microposts.build(micropost_params)
      if @micropost.save
        flash[:success] = "Micropost created!"
        redirect_to root_url
      else
        @feed_items = []
        render 'static_pages/home'
      end
end
yuyanegishi commented 5 years ago
@micropost = current_user.microposts.build(micropost_params)
def current_user
    if session[:user_id]
      @current_user ||= User.find_by(id: session[:user_id])
    end
end

内容

リファレンス確認 ※Ruby on Rails APIにてbuildで検索。

build(attributes = nil, &block)
Alias for: new
new(attributes = nil, &block)

Initializes new record from relation while maintaining the current scope.
Expects arguments in the same format as ActiveRecord::Base.new.