Favouriting a picture in a Rails app -
i trying figure out how execute "favourite picture" method in rails app, new to, going js/meteor background.
the point have user, favpic, pic classes:
class user < activerecord::base #some user oauth stuff 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 template:
<% if current_user %> <%= form_tag(root_path, :method => "get") %> <p> <%= text_field_tag :username, params[:username] %> <%= submit_tag "search", :name => nil %> </p> <% end %> <ul> <% @mentions.each |mention| %> <li> <%= mention.text %> <div class="row"> <% mention.media.each |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 able use app. </p> <% end %> current_user user signed in through twitter , @mentions list of tweets has username inputed in form mentioned. media.media_url picture url associated tweet.
i trying link (or whatever way it's done in rails) add media url db can list images @ separate url.
you've got choice: change <a href="#"> form, or have action link include url encoded url parameter. either way, can still use ajax invoke call server.
your server have action on controller accepted appropriate call mark favorite. since don't have controller code listed let's call piccontroller i'm assuming pic class has url property on it.
i highly recommend primer: http://guides.rubyonrails.org/association_basics.html , basic http://guides.rubyonrails.org/getting_started.html covers how action links , forms.
class piccontroller < applicationcontroller def favorite pic = pic.find(params[:url]) favorite = favpic.new favorite.pic = pic favorite.user = current_user # @ this... favorite.save pic.fav_users.add(favorite) pic.save current_user.fav_pics.add(favorite) current_user.save end end the above code covers how create new favpic instance , save appropriately.
Comments
Post a Comment