Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - Add Devise user info to post
    primarykey
    data
    text
    <p>I have a web app where users can sign up, via Devise, and post pictures for other people to vote on.</p> <p>I'm trying to attach the users username to the photo that they upload, but I can't for the life of me figure out how.</p> <p>I figured out how to get the Devise user_id into the form through a hidden field, but the username just shows up in the database as a blank field (not <code>NULL</code>, just blank).</p> <p>How do I get the username from Devise to post instead of the user_id?</p> <p>I have all of the appropriate <code>belongs_to</code> and <code>has_many</code> in the models.</p> <p><strong>The form.</strong></p> <pre class="lang-rb prettyprint-override"><code>&lt;% if current_user %&gt; &lt;%= simple_form_for Picture.new, :remote =&gt; true, :html =&gt; { :class =&gt; "form-horizontal" } do |f| %&gt; &lt;%= f.input :title %&gt; &lt;%= f.input :image, :as =&gt; :image_preview, :label =&gt; false, :input_html =&gt; {:preview_version =&gt; :thumb} %&gt; &lt;div class="control-group"&gt; &lt;div class="controls"&gt; &lt;a href="#" class="btn" id="file"&gt;Choose a File&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;%= f.input :desc, :as =&gt; :text, :input_html =&gt; { :cols =&gt; 20, :rows =&gt; 3 }, :label =&gt; 'Description' %&gt; &lt;%= f.hidden_field :user_id %&gt; &lt;%= f.hidden_field :username %&gt; &lt;div class="control-group"&gt; &lt;div class="controls"&gt; &lt;div class="btn-group"&gt; &lt;%= f.submit "Add Picture", :class =&gt; 'btn btn-primary' %&gt; &lt;button class="btn btn-danger" id="form-cancel"&gt;Cancel&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p><strong>pictures_controller.rb</strong><br> </p> <pre><code>def create #This line inserts the user_id into the database @picture = current_user.pictures.create!(params[:picture]) @picture.save respond_to do |format| format.html { redirect_to pictures_url, notice: 'Thanks for your picture!' } format.js end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload