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.
Related
I have an application, which I only have configured for one company, I need to change it, so that it accepts multiple companies, as follows:
When you create a company, create the independent db for that company and in turn the user, later when that user with that company_id authenticates, connect to that previously created database.
def create
#datos_empresa = DatosEmpresa.new(datos_empresa_params)
respond_to do |format|
if #datos_empresa.save
client = Mysql2::Client.new(:host => 'localhost', :username=>"*****", :password=> "*********")
nombre_db = 'name_company'+#datos_empresa.nombre
client.query("CREATE DATABASE #{nombre_db.downcase}")
ActiveRecord::Base.establish_connection(client)
format.html { redirect_to #datos_empresa, notice: 'Datos empresa was successfully created.' }
format.json { render :show, status: :created, location: #datos_empresa }
else
format.html { render :new }
format.json { render json: #datos_empresa.errors, status: :unprocessable_entity }
end
end
end
The best way I found of doing this on my application was using the ros-apartment gem, it handles all the DB creation, migrations (bet you forgot that one), seeds and switching.
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
I'm working on creating a basic survey app to better learn Rails.
Right now I'm working on learning how to send emails. I've gotten it to work, but now I'm running into a scenario where I send out a lot of duplicate emails. The duplicates are coming out because I'm only checking for a status of Submitted on PUT and then sending out the email.
def update
p params
# unless signed_in?
# redirect_to signin_path
# end
respond_to do |format|
if #survey.update(survey_params)
#survey.check_email
if #survey.status == "Submitted"
SurveyMailer.survey_created(#survey).deliver
end
format.html { redirect_to #survey, notice: 'Survey was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
What's the best way to check/track which emails for a survey I've sent it out to? Basically some sort of "if user has not been notified that a survey has been shared with them, send it out, otherwise don't"
I have a Rails 3.2 app that uses session store in the controllers to get the user back to the screen they were previously on.
It's been working fine for over a year. All of a sudden, the production version on Heroku, has started having issues. The user is looking at a worequest and clicks on the following in order to add a comment.
<%= link_to 'New Comment', new_comment_path(:worequest_id => #worequest.id), :class => 'btn btn-primary' %>
This is the code I use:
def new
#comment = Comment.new
#comment.build_attachment
#worequest = params[:worequest_id]
session[:return_to] ||= request.referer
respond_to do |format|
format.html # new.html.erb
format.json { render json: #comment }
end
end
# POST /comments
# POST /comments.json
def create
#comment = Comment.new(params[:comment])
respond_to do |format|
if #comment.save
if session[:return_to] != nil
format.html { redirect_to session.delete(:return_to), notice: 'Comment was successfully created.' }
format.json { render json: #comment, comment: :created, location: #comment }
else
format.html { redirect_to :back, notice: 'Comment was successfully created.' }
format.json { render json: #comment, comment: :created, location: #comment }
end
else
format.html { render action: "new" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
In development and staging (on Heroku), the user goes back to the worequest after entering a new comment.
Now in Production, the url looks like this:
mywebsite/comments instead of mywebsite/worequests/639
I'm not even sure where the session[:return_to] gets stored. Therefore, I'm having trouble debugging the issue.
Thanks for your help!!
Are you able to replicate this behavior 100% of the time, or just seeing it sometimes in your log?
It looks like someone is getting to comments#new from /comments (comments#index). Is there a route to /comments?
Run rake routes to see all your routes.
If there is a route to comments#index, and there's no reason for it to be exposed because you only intend for people to post comments from within the context of a specific article, consider removing it the comments#index route.
Also, one thing to consider, request.referrer is not always available. It's sent by the client who may choose not to send it (e.g. certain privacy extensions remove this header).
I have a partial which gets loaded on a few different views. The partial contains a button which connects to an update method in one of my controllers. After the button gets pressed, I want the action to occur and then redirect back to wherever the current user is. However, when I use things like request.path, it always redirects back to the specific model that the button connects with. How do I do a true redirect back to the current page the user is on, regardless of where the button points to?
In the controller that is being called by the new, edit, or destroy action you will want to change the #controller in the action (create, update, destroy) that is being called.
Below, you can see where when a users creates a new answer, it will redirect them to the previous page.
# POST /answers
# POST /answers.json
def create
#answer = Answer.new(params[:answer])
respond_to do |format|
if #answer.save
format.html { redirect_to :back, notice: 'Answer was successfully created.' }
format.json { render json: :back, status: :created, location: #answer }
else
format.html { redirect_to :back, alert: 'Error Posting Answer.' }
format.json { render json: #answer.errors, status: :unprocessable_entity }
end
end
end