rails 3 how to automatically post user_id in column of comments - comments

im totally new to rails. here my question:
i made an app with articles and comments and use devise for authentication
sadly im only able to post 1 hyperlink so this is the middle part of my post with the files at gist: https://gist.github.com/771366
the article_id is pre selected in the comments/_form - but the user_id isnt. i googled a lot, tried value => session[:user_id] and others, but nothing worked
would be great if someone could tell me how it works ^^
thx

If you are trying to get the current user's ID you can do
current_user.id
in your view or in your controller

Related

Facebook like an object

Need help with Facebook SDK Php Facebook Like.
My website fetches Logged in FB User's Timeline and user can Like posts in his timeline.
Posting Likes works for me successfully. But my question is how should I know he had already liked the post.
I want to show Like button if he has not liked that object and unlike button if he has already liked it.
PS: I tried collecting all the list of users who liked and then compared it with logged in user's fb id, but this method takes long time if likes are more than 10k.
Is there any other methods to accomplish this task.
I am using facebook-php-sdk-v4-4.0-dev
PS: My code for showing his feed is:
(new FacebookRequest($session, 'POST',"/me/feed", $params))->execute()->getGraphObject()->asArray();
The only solution available now is looping through the list of likes like you do , as of now .
there is another solution using FQL
select user_id from like where object_id=your object_id AND user_id=me()
but the problem is that he LIKE table only considers videos, notes, links, photos and albums, not posts.
check facebook fql like documentation . also note that fql is on track for full deprecation .
May be this will help you
$fql= "select user_id from like where object_id=REQUIRE_OBJECT_ID_HERE AND user_id=me()";
$request = (new FacebookRequest($session, 'GET', "/fql?q=$fql"))->execute()->getGraphObject()->asArray();
Change REQUIRE_OBJECT_ID_HERE variable to the id that you need.

Can't put comments form above list of comments in Rails

I've seen a few other stackoverflow questions with the same question, but the answers haven't helped...
I have a two-deep nested association. My parent_model has:many child_models which has:many comments
On the Show action for the child_model I'm showing the comments associated with it. If i put the comment form below the loop of comments, then everything works fine. I'd prefer to put the form before the list of comments, so user's dont have to scroll to the bottom to comment. When i do this, i get an error involving the user. I have tried to switch the form from 'build' to 'new' and a few other suggestion's people had, but nothing is working...Here is my child_models show action
#collection = Collection.find(params[:collection_id])
#design = #collection.designs.find(params[:id])
I have a comments controller as well and here is the create action
#collection = Collection.find(params[:collection_id])
#design = #collection.designs.find(params[:design_id])
#comment = #design.comments.create(comment_params)
#comment.user = current_user
#comment.save
Can anyone figure out how to solve this problem? Thank you in advance for taking the time.

Understanding routing in rails

Ok first off I have to say I'm very new to Rails. I have spent the last few days going through tutorial after tutorial and still missing a few concepts. Mainly because I just want to start off with a simple site structure but every tutorial is either a shopping cart or a blog which are more applications within a site. I have some pages on my site that will have photo galleries that are database driven but for now I'm just trying to get some answers to these questions.
Site structure:
home | photos | about | work | contact
Work has sub pages for example:
html | ruby | rails | bla-bla
Controllers:
Do I need to set up a controller for every new page or could I have one controller that handles all main level pages.
If I could use one controller how would that work and would I need to define an action to handle each page ( view ) like
class MainController < ApplicationController
def index
end
def photos
end
def contact
end
# and so on ......
end
Routing:
How would I route the above.
Whats the difference between a resource and a *get
get "photos/photos"
resources :photos
When I setup a controller for a specific page like.
rails g controller Contact contact
It creates a folder inside my views called contact and inside it is a view called contact meaning my url is contact/contact
it also adds a route get "contact/contact"
Now what if I only want the user to type http://mydomain/contact then this is not going to work. How would I set it up so the user doesn't have to add http://mydomain/contact/contact
The only way I could find a way around this was to use the match verb.
match "contact" => 'contact#contact'
Does this mean I have to use the match for every page on my site to change the url path?
These are just a few of many question I have that are not so clear in most of the tutorials I have gone over. Please don't tell me to use the user guide as I have already and am felling overwhelmed right now. I just would love some clear answers from some developers who are working in rails and would go about setting up a structure like I have outlined above.
Thanks
You're going to get some conflicting advice I think, but here's what I'd do.
Create a MainController (I prefer HomeController as it will also handle the homepage, but that's just me). This controller will handle the actions for home, about, and contact.
Create a PhotosController since you said photos come from the database and there's a good chance there is an index/listing page and individual pages for each photo.
Create a WorksController that handles the work main page and all the sub pages.
Now.. some people would argue (myself included) that home, about, contact, and all the work pages (sub pages too) should be handled by a generic PagesController that is smart enough to know what to do. I'm not gonna get into that now though.
The difference between these two routes:
get "photos/photos"
resources :photos
Is that the first will only create a single route for a GET request to '/photos/photos'. The second will create the standard CRUD operations for '/photos'.
For your static pages, I probably would go ahead and just create:
match "about" => 'main#about'
match "contact" => 'main#contact'
...
It's harder to say for the photos and work since I don't know what all you'll be doing there. The above isn't as DRY as it could be, but unless you go the "smart pages controller" route it's the simplest.

Twitter Ruby Gem

I am trying to setup the Twitter gem, and I feel like I'm almost there... kind of.
Right now I was trying to follow this link:
http://www.phyowaiwin.com/how-to-download-and-display-twitter-feeds-for-new-year-resolution-using-ruby-on-rails
It is a bit old though, and I guess its instructions are a bit out of date. I have created a twitter model, twitter db migration and a twitter controller(not sure it's needed though), and if i open rails console, and I type:
Twitter.user_timeline("whatever").first.text
It just works. I just can't seem to be able to see it in my view. can you point me in the right direction?
Thanks a lot!
Alex
In your controller that corresponds with your view, you need to assign your results to a variable inside the appropriate functionlike so:
def controller_function
#twitter_data = Twitter.user_timeline("whatever").first.text
end
Then, in your corresponding view you can use the variable
<%= #twitter_data ... %>
Check out http://guides.rubyonrails.org/layouts_and_rendering.html for more guidance on controllers and views

Facebook posting help, adding to facebook whenever a user creates a post

I think this question is a matter of writing nice ruby code, let me see what you guys think. I've already setup all the auth/access token stuff with omniauth and and fbgraph, what I can't seem to work out is how to integrate it when a user creates a post.
My app revolves around users making posts (made up of 'title' and 'content'), I'd like the post to be automatically shared on facebook or twitter or both, depending on the particular authentications the users has setup. And not share anywhere if the user has signed up conventionally without facebook/twitter.
How would I integrate a dynamic way to share the title and content of a user's post whenever they post automatically? I was thinking of some type of after_save to the post model but I can't get it working right. Thank you for any help is it very much appreciated.Also it would great if it was a method that allowed for furture expansion if I wanted to share links and pictures later on.
This is the only post while searching that sheds some light about sharing to both but I'm still confused :(
Easy way of posting on Facebook page (not a profile but a fanpage)
In your Post model have:
after_commit :share_content
def share_content
user.share_content title, content
end
Then in User model have:
def share_content title, content
# some conditionals with whatever stuff you have to determine whether
# it's a twitter and/or facebook update...
if go_for_twitter
twitter.update title
end
if go_for_facebook
facebook.feed! :message => title
# ...etc.
end
end

Resources