0
投稿テーブルにuser_idを格納する必要があります(新規投稿ごとに)。私はhidden_fieldとしてフォームからuser_idを渡していました。コントローラー自体からuser_idを取得する別の方法は何ですか。あなたが使用することができますコントローラのユーザ情報をレールに入れる方法
コントローラで
def create
@post [email protected](post_params).save
end
ポストモデル
class Post < ApplicationRecord
belongs_to :topic, counter_cache: true
belongs_to :user
has_and_belongs_to_many :users # to maintain read and unread status for the posts
has_many :comments , dependent: :destroy
has_many :ratings , dependent: :destroy
has_and_belongs_to_many :tags
end
フォーム
<%= form_for [@topic, @post],remote: true,html: { multipart: true} do |form| %>
<div class="field">
<%= form.label :author %>
<%= form.text_field :author, :value => current_user.email %>
</div>
<%= form.label :content %>
<%= form.text_area :content, id: :post_content, autofocus: true %>
<!--<%= form.hidden_field :user_id %>-->
<div class="field">
<%=form.label :image%><br>
<%=form.file_field :image %>
</div>
<%= form.fields_for :tags do |a|%>
<div class="field">
<%= a.label :tag_name %>
<%= a.text_area :tag_name %>
</div>
<% end %>
<div class="field">
<%= form.label "select tag" %>
<%= form.collection_select(:tag_ids, Tag.all, :id, :tag_name, {}, {:multiple => true, :size => 10, :prompt_text => "select tag"}) %>
</div>
<div class="submit">
<%= form.submit %>
</div>
<% end %>
どのようにユーザーに署名し、現在のトラックを維持していますか?フォームコードを投稿できますか? – xeon131
どのようにユーザーを保管していますか?あなたは 'devise'宝石を使いますか? –
@ xeon131フォームコード – Aarthi