Getting user_id in model

I'm Rails newbie so this might be incredibly stupid question but I hope somebody finds the time to help me :)

I'm currently doing my first Rails 3 application with MongoDB. I have setup Devise for authentication and it's working great. I'm trying to create blog type of app so each registered user can post an article. Naturally user model has "many: posts" and post model has "belongs_to: user" but here's the problem: I don't want to have user_id as a field in the new post form for security reasons so how can I pass the current user to post model? I removed all fields related to user data from the form so now it doesn't seem to be able to get the data for user_id anywhere.

So how do I use Devise's current_user helper method to pass the correct user_id to post model for proper association?

-------------Problems Reply------------

I assume you have a controller action responsible for handling your post creation. In RESTful design that would be create action normally. That action would call a save or create method on your model. Prior to the method call, assign the user_id to your devise's

Category:ruby on rails 3 Views:6 Time:2010-08-09

Related post

  • how to pass session variable to model in RoR? 2010-06-10

    I used a global variable in my app for passing information before. But I got a problem and thanks everyone here suggested me to store those data in session with database. I tried, but I found that I can't access the session variable in Model. I googl

  • Why can't I pass a variable to Model's constructor in Code Igniter? 2011-11-09

    It seems that it is impossible to pass a variable to a Model's Constructor in Code Igniter. Maybe I'm missing something in MVC or Code Igniter, but I don't understand what. In my case, should I use librairies instead of Model ? (librairies actually a

  • Backbone model is undefined? 2012-01-10

    I can't understand why this.model would be defined in view.intialize() when I run this.model.fetch() on it but not in view.render(). define([ 'jquery', 'underscore', 'backbone', 'text!templates/example.html' ], function($, _, Backbone, exampleTemplat

  • passing arguments to Model constructor 2015-01-19

    How can i pass my arguments to the model constructor,i have the following code in my controller public static function inbox(){ $user=Sentry::getUser(); $results=new Message($user->id); $inbox=$results->inbox(); return $inbox; } And in my modal

  • django, admin template error Caught an exception while rendering: 'NoneType' object has no attribute 'label' 2010-02-23

    good day guys! in project, among others, have models: class Category(models.Model): name = models.CharField(max_length = 50, blank = False, null = False) def __unicode__(self): return "Category %s" % class Meta: db_table = "categories" mana

  • Struggling with a complex Rails 3 query (need: user.friends.checkins) 2010-10-15

    I'm working on a social networking application and am trying to build a complex query that efficiently pulls all of a users' friends' checkins from the database. Basically I need: "user.friends.checkins" I've recreated (in simplified form) the data s

  • PHP issue with Wordpress Shopping Cart plugin 2011-01-13

    I'm using a plugin for Wordpress called Wordpress Shopping Cart from Tribulant Software. Normally it runs very well, however on this one site it just constantly throws me all sorts of errors. I had it running for a little while, but it just spontaneo

  • 2 SQL command combined? 2011-05-18

    I've got 2 SQL command wich I need to populate a gridview, First I select every ID wich the current user had filled out succefully with the following SQL command : "SELECT Mod_ID FROM Toewijzing WHERE User_ID = '" + Session["userid"].ToString() + "'

  • rails attaching database entry to logged in user 2011-07-21

    I'm new to rails and am done setting up my login system. However, I want someone to be able to make a new blog post and attach it to their account when logged in. How can I attach a post to their user_id as well as list all their previous posts? ----

  • Problem serializing checkboxes with jQuery and PHP 2011-09-10

    I have checkboxes (all with the same 'name' attribute) that I want sent to a PHP controller. Here's a brief snippet of my markup: <script type="text/javascript"> $(document).ready(function(){ $(".someButton").click(function(){ $.post("my_contro

  • Group result by field 2011-09-13

    Am building a messaging system for a site, but i want it to have an instant messaging app feel. I have table with a structure like this: <!-- language: lang-none --> id R_id = Reciever's id S_id = Sender's Id message read = 0 if unread, 1 if re

  • How to link multiple records at once using cakephp hasMany? 2012-01-14

    I googled this a lot but nothing seems to be similar to what i am looking for.. What i am trying to do is to link one record "User" with other multiple records "Cards" at once using a multiple HTML list. Tables are: users (id, name, username) cards (

  • CMultiFileUpload didn't work in CJuiDialog widget Yii 2013-03-02

    Am using CJuiDialog widget with ajaxlink for popup works well.. But now i need to upload multiple files from that popup form.. so i use CMultiFileUpload for upload option but i acts like a normal single file button.. please help me to solve

  • In Rails 3 how can you access the user_id of the last user in the database in your model? 2011-01-22

    Edit See Below: class Post < ActiveRecord::Base last_user_id = validates_inclusion_of :user_id, :in => 0..last_user_id end The above solution works but as vojlo explains, once in production the code will only be executed once and t

  • Adding a user_id to a Message model 2011-05-21

    I currently have a simple app that includes user authentication through devise and a message model(the message model uses Jquery and Faye). Both are working fine independently, but I would like to add a user_id to the messages. I have already updated

  • generate model using user:references vs user_id:integer 2011-10-22

    I'm confused on how to generate a model that belongs_to another model. My book uses this syntax to associate Micropost with User: rails generate model Micropost user_id:integer but says to do it like this: rails generat

  • symfony admin list view Sort by username when the model is associated with user_id 2011-12-13

    I'm working with sfDoctrineGuardPlugin. I have a model Poster which has a user_id field linked to the id field of the sfGuardUser table. Poster: actAs: Timestampable: ~ Sluggable: fields:[name] columns: id: type: integer(4) autoincrement: true primar

  • Rails, how to query a friendship model when there are two equal fields: user_id & friend_id? 2011-12-18

    I have a friendship model like so: Friendship (user_id, friend_id, status) Where there is only 1 record to establish a friendship between User A and User B. user_id is the user who initiated on friend_id. On create the status is "pending." When the f

  • How to pass user_id to nested_resource model? 2012-01-19

    I have a small project setup with devise and cancan. There are User, Project, Responsible and Task Models. Project has nested Tasks. Each Project is assigned to one or multiple users. The task model has a name, user_id and project_id. Authentication

Copyright (C), All Rights Reserved.

processed in 0.210 (s). 11 q(s)