Creating groups and the need to save the user in the database in the rails application

I am creating groups in a rails application and you need to save the user in the database (I think so, at least) so that I can show created by the userโ€™s email on the show page. I cannot use current_user because it changes every time I log in. below is the code I'm using and the result is ActiveRecord::RecordNotFound in GroupsController . Therefore, I need to know how I can save the user in the database (if it is correct) in order to make this work correctly or how can I make it work correctly?

user model

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me has_many :groups end 

group model

  class Group < ActiveRecord::Base belongs_to :user end 

devise-user migration (only necessary part)

t.has_many: groups

group migration

 t.belongs_to :user 

group controller creation method

 def create @user = User.find(params[:id]) @group = @user.groups.build(params[:id]) respond_to do |format| if @group.save format.html { redirect_to @group, notice: 'Group was successfully created.' } format.json { render json: @group, status: :created, location: @group } else format.html { render action: "new" } format.json { render json: @group.errors, status: :unprocessable_entity } end end end 

group controller new method

  def new @user = User.find(params[:id]) @group = @user.groups.build(params[:id]) respond_to do |format| format.html # new.html.erb format.json { render json: @group } end end 
+1
source share
1 answer

Your setup here doesn't make much sense. In the new action, you should not look for the user. Most likely, if your routing is used correctly, you wonโ€™t even get parma [: id]. The same thing happens in the creation method, which (as the name implies) should create a new user record, and not look for it.

A new action should only create a new record (without saving), for example

 def new @user = User.new ... # no building of groups here, since record is still empty, does not even have an id end 

he should now use this custom object to render the input form in new.html.erb

After the user submits a new form, he ends the create action. Now you create a new user and actually save in the database:

 def create @user = User.new(params[:user]) if @user.save # maybe create some groups here redirect_to @user else render :action => "new" end end 

At this point, it is not particularly relevant to the development and authentication of users.

This is just a rough overview to show you the most common process (as you might find in any good RoR tutorial). Otherwise, your question is not entirely clear about what exactly you want to do and what it is related to devise and current_user. Most likely, you will have some controllers and actions that will allow the user to see his email, groups and other user data. In this case, these controllers will have to use current_user (after logging in). Many projects have additional functionality for admin interfaces that allow a specific user to view user lists and their data. In this case, you will use current_user to make sure that the user has administrator rights and other search and search functions to display the data of managed users.

0
source

Source: https://habr.com/ru/post/1397957/


All Articles