0
我需要在帖子表中存儲user_id(對於每個新帖子)。我從表單中將user_id作爲hidden_field傳遞。從控制器本身獲取user_id的替代方法是什麼?如何讓控制器中的用戶信息在rails中
在控制器
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