samedi 25 avril 2015

Favouriting a picture in a Rails app


I am trying to figure out how to execute a "favourite a picture" method in a Rails app, which I am very new to, going from a JS/Meteor background.

The point is I have a User, FavPic, Pic classes:

class User < ActiveRecord::Base
  #some user oauth stuff would be here

  has_many :fav_pics
  has_many :pics_favorited,
    class_name: 'Pic',
    through: :fav_pics

end

class FavPic < ActiveRecord::Base
  belongs_to :user
  belongs_to :pic
end

class Pic < ActiveRecord::Base
  has_many :fav_pics
  has_many :fav_users,
    class_name: 'User',
    through: :fav_pics
end

and here's my template:

<% if current_user %>
  <%= form_tag(root_path, :method => "get") do %>
    <p>
      <%= text_field_tag :username, params[:username] %>
      <%= submit_tag "Search", :name => nil %>
    </p>
  <% end %>

  <ul>
    <% @mentions.each do |mention| %>
      <li>
        <%= mention.text %>
        <div class="row">
        <% mention.media.each do |media| %>

          <div class="col-xs-3">
            <%=image_tag(media.media_url, class:"img-responsive")%>
            <a href="#" class="fav-img"><i class="fa fa-star fa-2x"></i></a>
          </div>

        <% end %>
        </div>
      </li>
    <% end %>
  </ul>
<% else %>
  <p>
    Sign in to be able to use the app.
  </p>
<% end %>

current_user is a user signed in through Twitter and @mentions is a list of tweets that has the username inputed in the form mentioned. media.media_url is a picture url that is associated with that tweet.

I am trying to get that link (or whatever way it's done in Rails) to add that media URL into the DB so I can list all the images at a separate URL.


Aucun commentaire:

Enregistrer un commentaire