Controller to add current_user email to database in rails - ruby-on-rails

I have a column in my database that I'm trying to update in my controller. I'm trying to take the current logged in user's email and send it to the column in the model using the create method. It's not sending the email to the database though.
Here's my create method in the controller
def create
#request = Request.new(request_params)
respond_to do |format|
if #request.save
format.html { redirect_to #request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: #request }
#request.email = current_user.email
#request.increment!(:voteCount)
else
format.html { render :new }
format.json { render json: #request.errors, status: :unprocessable_entity }
end
end
end
I'm fairly certain the trouble lies in the line #request.email = current_user.email but Im not sure why.
The user model and the request models are different, so im trying to grab the data from one model and send it to another.

Sergio's comment helped me figure this out.
Added #request.save

Related

Render and/or redirect called multiple times in action

I'm using Devise and Pundit.
To create a new profile page, the user has to be authorized to do so.
This has been working fine since I first implemented it, but today it just started acting up with an error message:
Render and/or redirect were called multiple times in this action.
Please note that you may only call render OR redirect, and at most
once per action. Also note that neither redirect nor render terminate
execution of the action, so if you want to exit an action after
redirecting, you need to do something like "redirect_to(...) and
return".
Here's my code from my Application controller:
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
...
private
def user_not_authorized(exception)
flash[:alert] = "Sorry, you are not authorized to perform this action."
redirect_to(request.referrer || root_path)
end
Here's my ProfilePage controller:
def new
#profile_page = ProfilePage.new
authorize #profile_page
end
def create
#profile_page = ProfilePage.new(profile_page_params)
respond_to do |format|
if #profile_page.save
format.html { redirect_to #profile_page, notice: 'Profile page was successfully created.' }
format.json { render :show, status: :created, location: #profile_page }
else
format.html { render :new }
format.json { render json: #profile_page.errors, status: :unprocessable_entity }
end
end
authorize #profile_page
end
Someone suggested that I add this line of code below flash[:alert]:
self.response_body = nil
But now my user is redirected to the 'new profile' page again, rather than the successful profile page. It also tells the user that they are not authorized to complete this action, despite the fact that it HAS authorized them to do so.
In the create action you have to put the authorization logic before saving the record:
You have to move
authorize #profile_page
at the top of your create action, after initializing the #profile_page, like so:
def create
#profile_page = ProfilePage.new(profile_page_params)
authorize #profile_page
respond_to do |format|
if #profile_page.save
format.html { redirect_to #profile_page, notice: 'Profile page was successfully created.' }
format.json { render :show, status: :created, location: #profile_page }
else
format.html { render :new }
format.json { render json: #profile_page.errors, status: :unprocessable_entity }
end
end
end

Create more than one object at once using the standard create method in Ruby on Rails

I am trying to use the standard create method created for Ruby/Rails projects and simply pass in an additional form field that tells the method how many objects to create (vs just creating one object). The standard create method looks like so:
def create
#micropost = Micropost.new(micropost_params)
respond_to do |format|
if #micropost.save
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
I want to pass in an additional data (form field called number_to_create) which tells the method how many of the microposts to create. I just added a new form field like this, in addition to the other micropost form field params:
<%= text_field_tag :number_to_create %>
My question is how do I modify the create method code such that it creates N number of micropost objects vs. just one. So if I pass in 3 from the form along with the other micropost attributes, the method creates 3 identical micropost objects, not just one as it currently does.
Thanks in advance for your help on this.
You could use the param as times
#microposts = Micropost.transaction do
[].tap do |microposts|
param[:number_to_create].times do
microposts << Micropost.create(micropost_params)
end
end
end
respond_to do |format|
if #microposts.all? &:persisted?
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
The transaction block is to make sure that either all of them gets saved, or none of them gets saved, this way you can fix your errors and recreate them without worrying of getting any stray saved objects

How to pass user id in table from controller Ruby On Rails

I have been struggling all day to pass a user ID from a current user in my application to the sensors table when i create my new sensor
In the sensor Controller i have this code
def create
#sensor = Sensor.new(sensor_params)
respond_to do |format|
if #sensor.save
format.html { redirect_to #sensor, notice: 'Sensor was successfully created.' }
format.json { render action: 'show', status: :created, location: #sensor }
else
format.html { render action: 'new' }
format.json { render json: #sensor.errors, status: :unprocessable_entity }
end
end
end
def sensor_params
params.require(:sensor).permit(:name, :typeOfSensor, :privacy,:user_id)
end
I have used this code
#sensor = current_user.sensors.build(:current_user_id => params[:current_user_id])
But it passes only the id inside the sensor table without the rest of the attributes
How is the correct way to merge the code so that it passes all the parameters (name, typeOfSensor etc and also it passes the id of the user that created this sensor
Thanks in advance
are you sure you need the current_user_id from params? you already have the current user, using a param for that is dangerous, a user con edit your code and create a sensor for another user.
justo do
current_user.sensors.build(sensor_params)
and you are done
Merge is indeed the answer.
sensor_params.merge(current_user_id: current_user.id)

maintain `new` in url after validation failed

from the default scaffold generator I have the following create action in my blogs controller:
# POST /blogs
# POST /blogs.json
def create
#blog = Blog.new(params[:blog])
respond_to do |format|
if #blog.save
format.html { redirect_to #blog, notice: 'Blog was successfully created.' }
format.json { render json: #blog, status: :created, location: #blog }
else
format.html { render action: "new" }
format.json { render json: #blog.errors, status: :unprocessable_entity }
end
end
end
When the sent form contains errors, my browser is redirected to /blogs URL but in the page the new action is rendered.
This is really ugly in my opinion and (also to simplify my javascript) I would like the browser to remain in the same blogs/new URL.
I tried with changing redirect_to :new instead of render action: "new", but this of course loses the #blog data.
any clue on how to do this?
thanks,
If you want to keep new in your path you could redirect with params like so:
redirect_to new_blog_path(blog: params[:blog])
and then check for these params in blog#new

Rails with Devise - Use current_user after 'remote => true' submit

I have a three part user signup that work with two separate models: User, and Customer Info. There's a user signup that I submit remotely, and it goes to the next part of the form. I want to be able to use the current_user variable when I submit the Customer Info row. The following code works when I test without remote, but it just doesn't even create the row at all when I do it with the remote. Is there another variable I can use in lieu of this variable? Or is there a better way of accomplishing this?
def create
#customer_info = CustomerInfo.new(params[:customer_info].merge(:user_id => current_user.id))
respond_to do |format|
if #customer_info.save
format.html { redirect_to tasks_url, notice: 'Customer info was successfully created.' }
format.json { render json: tasks_url, status: :created, location: #customer_info }
else
format.html { render action: "new" }
format.json { render json: #customer_info.errors, status: :unprocessable_entity }
end
end
end
Turns out it was a devise config situation. I wasn't allowing users to be unconfirmed for any amount of time, so "curent_user" was staying empty even after they signed up. I added config.allow_unconfirmed_access_for = 2.days and restarted the server and that worked.

Resources