How does the current_user function work?

I want to understand how the current_user method works because I want to generalize it to other models that would allow code like current_forum or current_forum_thread to be used.

To be more specific, I am trying to implement a chat forum in Rails. I have a page showing all posts (not currently) for a specific thread. A new message form is built on the same page. Debugging (params) shows:

action: show
controller: discussions
forum_id: '1'
id: '1'

discussion: !ruby/object:Discussion
  attributes:
    id: 1
    title: first discussion (thread)
    forum_id: 1

      

So the create method in the message controller needs to know what the discussion ID is. However, this code in the controller does not work.

  1. @discussion = Discussion.find(params[:id])
  2. @post = @discussion.posts.new(params[:post])
  3. if @post.save
  4.  flash[:success] = "Discussion post created!"
  5.  redirect_to '#'
  6. else
  7.  render '#'
  8. end

      

Line 1. causes an error:

Couldn't find Discussion without an ID

      

Also, when checked, it turns out that @discussion is always NIL.

+3


source to share


3 answers


I put before_filter :authenticate_user!

on top of each controller and then do something like this:

current_user.posts.new(params)

      

It also takes an attitude User has_many :posts



Seems to work (not sure if this is the best way).

Also, your error seems to mean that your prarms [: id] is null, so check if it passes correctly. You should be able to see this in magazines.

# Discussions controller - show action
@discussion = Discussion.find(params[:id])
render ...

# Discussion show view
%ul
  - @discussion.posts.each do |post|
    %li= post.content # to output list of posts
= form_for @discussion.posts.new do |f|
  = f.input :content
  = f.submit
  # form to create new post related to this discussion

# Post controller - create method
@post = Post.new(params[:id])
@post.save!
render ...

      

0


source


I think this is more of a helper function, the development way is to get the id through session, but you can do the same with the params hash, i.e.

module ApplicationHelper
  def current_forum_thread
   Thread.find(params[:id])
  end
end

      



does it work for you?

0


source


Using current_id with streams seems too complicated for this implementation as it looks like a fairly simple nested resource.

The message is not saved because it cannot find the discussion. Since you are on the Post controller and not in discussions, you need to search for discussion with

@discussion = Discussion.find(params[:discussion_id])

      

Id: The id you are looking for refers to the parameters of the message. He didn't find anything because you probably have significantly more posts being discussed. If he found something, it would be wrong.

Another thing in the checklist to make the nested route work is to configure the routes correctly. Test them with "rake routes", but it should look like this:

resources @discussions do
   resources @posts
end

      

This will add routes so your form, which should look something like <%= form_for [@discussion, @post] do |f| %>

, can submit / place in path_posts_path.

Using current_id as yours is really viewable and it looks like a mess, Ryan Bat has an awesome video on Multi-tenancy with scopes http://railscasts.com/episodes/388-multitenancy-with-scopes

0


source







All Articles