0
haben Ich muss die Benutzer-ID in Posts-Tabelle (für jeden neuen Beitrag) speichern. Ich habe user_id aus dem Formular als hidden_field übergeben. Was ist ein alternativer Weg, die user_id vom Controller selbst zu bekommen.Wie Benutzerinformationen in der Steuerung in Schienen
in Controller
def create
@post [email protected](post_params).save
end
Post Modell
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
<%= 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 %>
Wie halten Sie den Überblick über die aktuellen Benutzer unterzeichnet? Können Sie Formularcode posten? – xeon131
Wie speichern Sie Benutzer? Benutzt du 'device' Juwel? –
@ xeon131 Ich habe Formularcode – Aarthi