Answer the question
In order to leave comments, you need to log in
How to define nested attributes in rails?
It doesn't reach me how to define attributes in the nested controller or use two controllers in one form if possible (it would be cool)
What do I need: When creating a Post model, if the user is not authorized, register or authenticate him? and create a User model, as well as send an email to subscribers about the creation of a new Post.
How is
the form itself now:
views/posts/new.html.erb
<% resourse.posts.build %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.email_field :email, placeholder: "E-mail" %>
<%= f.password_field :password, placeholder: "password" %>
<%= f.password_field :password_confirmation, placeholder: "password confirmation" %>
<%= f.fields_for :posts do |form| %>
<%= form.text_field :title %>
<%= form.text_field :content %>
<%= f.submit "Sign up and create post" %>
<% end %>
<%= render "devise/shared/links" %>
class User < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
end
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
super
#вот тут я не что делать как определить nested_attributes для posts
end
Answer the question
In order to leave comments, you need to log in
I did this, maybe it will come in handy for someone
I filed my RegistrationController which refers to Devise
, and if anyone has a thread more than nested attribustes (I have one),
respectively post = resourse.posts.second and so on
class RegistrationsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)
resource_saved = resource.save
yield resource if block_given?
if resource_saved
if resource.posts.present?
post = resource.posts.first
flash[:success] = "Поздравляем Ваше сообщение опубликованно!"
User.includes(:categories, :roles).references(:categories, :roles).where(categories: {id: post.category_id}, roles: { id: 4 }).pluck(:email).each do |email|
PostsMailer.delay.deliver_posts_register(email, post)
end
end
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
@validatable = devise_mapping.validatable?
if @validatable
@minimum_password_length = resource_class.password_length.min
end
respond_with resource
end
end
end
1. How to send a letter - guides.rubyonrails.org/action_mailer_basics.html
2. There are all sorts of callback methods for checks, for example
after_create do |post|
post.user.nil?
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question