So i have this 2-step Twitter Sign in and at the page where the user must enter his Email i get an undefined method error.
My Form :
<%= simple_form_for(#user, :as => "user", :url => account_create_path, :html => {:class => "form-inline"}) do |f| %>
<%= f.input :email, :placeholder => User.human_attribute_name(:email), :class => "input-medium" %>
<%= f.submit "Finish registration", :class => "btn btn-small" %>
<% end %>
I think that it's a Syntax problem, but as i am fairly new to Rails i cant figure out what is wrong. Google wont tell me either :)
my Accounts_controller.rb:
def create
data = session["devise.omniauth_data"]
data[:email] = params[:user][:email]
user = User.find_for_twitter_oauth(data)
user.email = data[:email]
if user.save
flash[:notice] = I18n.t "devise.registrations.signed_up_but_unconfirmed"
redirect_to root_path
else
flash[:error] = I18n.t "devise.omniauth_callbacks.failure",
:kind => data[:provider].titleize,
:reason => user.errors.full_messages.first
render "users/omniauth_callbacks/add_email"
end
end
Change variable user to instance variable #user in controller and problem will be fixed.
Related
I am Rails newbie. I am creating a section that is pulling existing user's details and when the user click on edit, he can save the changes he has made. However, the changes aren't reflecting once the user saves it. Can you tell me what I am missing in here?
Here's the html/ruby form I am using:
<%= form_tag(html: {:id => 'user_profile_form'}, :url => patient_profile_path(#user), :method => :put) do %>
<%= text_field_tag(:inputFieldName, "#{#user.first_name} #{#user.last_name}", {:disabled => true}) %>
<%= submit_tag 'Save', :id=> 'saveButton' %>
<%= end %>
Here's the routes:
put :patient_profile, to: 'users#patient_profile'
post :dashboard, to: 'dashboard#index'
Here are the controller codes:
def patient_profile
if params[:user]
u = params[:user]
#user.first_name = u[:first_name] unless u[:first_name].nil? || u[:first_name].empty?
#user.last_name = u[:last_name] unless u[:last_name].nil? || u[:last_name].empty?
#user.save!
# index
render :index
end
end
It doesn't look like your form is actually updating anything since your form fields don't match your model. Try simplifying your form action:
View
<%= form_for(#user, html: {:id => 'user_profile_form'}, :url => patient_profile_path(#user), :method => :put) do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.submit "Update User" %>
<%= end %>
Controller:
def patient_profile
# TODO: Handle failed validation
#user.update_attributes!(params[:user])
# index
render :index
end
end
def user_params
params.require(:user).permit(:first_name, :last_name)
end
I want to implement a real simpel newsletter.
Therefor I can select as much recipients I want.
To that newsletter I can attach a file. This works great for the first mail. All next mails have corrupted attachments (1 byte size).
actionmailer:
def send_newsletter(recipient,subject,content,file)
#content = content
if file
attachments[file.original_filename] = {
:content=> file.read,
:mime_type=> file.content_type
}
end
mail(:to => recipient, :template_name => "deliver_newsletter",
:subject => subject).deliver!
end
applicationcontroller:
def create
#customers = Customer.where("CHAR_LENGTH(email) > 0")
#recipients = params[:sent_to]
#subject = params[:subject]
#content = params[:content].html_safe
#file = params[:file]
if #recipients
#recipients.each do |mail_recipient|
Newsletter.send_newsletter(mail_recipient,#subject,#content,#file)
end
end
respond_to do |format|
format.html { redirect_to bills_path, notice: "everything works fine" }
end
end
and finally the form for sending newsletters:
<%= form_tag ('/newsletters'), :multipart => true do %>
<%= t 'views.newsletter.to_recipient' %>:<br>
<%= select_tag 'sent_to', options_from_collection_for_select(#customers, 'email', 'name'), :multiple => true, :class => 'sent_to' %><br><br>
<%= t 'views.newsletter.subject' %>:<br>
<%= text_field_tag 'subject' %><br><br>
<%= t 'views.newsletter.content' %>:<br>
<%= text_area_tag 'content', "".html_safe, :size=>"20x8" %><br><br>
<%= t 'views.newsletter.attachment' %>:<br>
<%= file_field_tag 'file' %><br><br>
<%= submit_tag t('views.buttons.newsletter_send_now'), :class => "btn btn-primary", :disable_with => t('views.buttons.newsletter_sending') %>
<% end %>
another small question: why is "delay" from the "delay_jobs" gem not working here? All other mails can be sent by typing "Newsletter.delay.send_newsletter(...)"
This is probably happening because file.read leaves you at the end of the file with nothing left to read. I would add a file_contents = file.read in your ApplicationController, and then pass that in as an additional parameter to each send_newsletter call, assuming the file is small.
After Googling everywhere about how I can fix this I decided to post a question. I'm trying to add a custom action "create_pro" to the Devise Registrations controller so I can create a user with additional fields (A pro user). This form is on the same page as the devise create action for a user.
I keep getting the following error after submitting the form:
Unknown action
The action 'create_pro' could not be found for
Devise::RegistrationsController
How can I fix this?
Routes
devise_for :users,:controllers => { :registrations => "registrations" }
devise_scope :user do
post "gopro", :to => "devise/registrations#create_pro"
end
Registrations Controller
class RegistrationsController < Devise::RegistrationsController
before_filter :authenticate_user!, :only => :token
def new
super
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:notice] = "Welcome to Banking Analytics."
redirect_to '/mybank'
else
render :action => :new
end
end
def create_pro
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:notice] = "Welcome to Banking Analytics."
redirect_to '/mybank'
else
render :action => :new
end
end
end
View
<div class="signup_form_basic">
<%= simple_form_for #user, :url => url_for(:controller => 'registrations', :action => 'create_pro') do |f| %>
<%= f.error_notification %>
<%= f.input :email, :required => true, :autofocus => true, :label => 'Username ( Your Email )', :placeholder => 'Email Address' %>
<%= f.input :password, :required => true, :autofocus => true, :label => 'Password', :placeholder => 'Password' %>
<%= f.input :password_confirmation, :required => true, :autofocus => true, :label => 'Confirm Password', :placeholder => 'Password Confirmation' %>
<%= f.button :submit, "Sign Up >>", class: 'btn btn-inverse' %>
<% end %>
</div>
I don't have an answer for the 'Unknown Action error' that you are facing, but I think the current solution is sub-optimal.
Since your goal is to have the following two classes of users
Pro-users, and
Regular (Non-pro) users,
it is probably best to follow the suggestion at How To: Customize routes to user registration pages and set up parallel structures for the two types of users.
That would be much more in line with the relationship between the two types of users.
I've got a pickem object that has one result. I'm having an issue getting the result to save properly to the database.
pickems_controller.rb
def results
#pickem = Pickem.find params[:id]
# #pickem.result = #pickem.build_result if #pickem.result.blank?
#pickem.result = Result.new
end
def update_results
#pickem = Pickem.find params[:id]
#pickem.result = Result.new params[:pickem][:result_attributes]
if #pickem.result.update_attributes params[:pickem][:result_attributes]
redirect_to edit_pickem_results_path(#pickem), :notice => 'The results have been successfully updated.'
else
render "edit"
end
end
results.html.erb
<%= simple_form_for #pickem, :url => edit_pickem_results_path(#pickem), :method => :put, do |f| %>
<%= f.simple_fields_for :result do |r| %>
<%= r.input :first_name %>
...
<% end %>
<%= f.submit :class => 'btn btn-success', :label => 'Submit Results' %>
<% end %>
pickem.rb
has_one :result, :dependent => :destroy
accepts_nested_attributes_for :result
result.rb
belongs_to :pickem
I was initially using the build_result code that is commented out in the controller but had to back out of that. With build_result a result record was saved to the database the instant somebody clicked into the results form. There are rules in place in the application that don't allow users to make any picks if a result has been entered. So even if a user clicked into the result form but didn't submit, the result was still being created.
How can I build my form and save the result record only when the user clicks save, and not when the form is loaded? The current solution I've pasted above does not work. It saves a result record with the appropriate foreign key but never gets the form data. If I dump #pickem.result the correct form data is in the result object, I just can't get it to save right. Other solutions I've tried save the form data correctly but have a foreign key of 0.
EDIT:
For whatever reason #pickem.result = Result.new was still saving a record to the database so I changed it to #result = Result.new and updated the form as follows:
<%= simple_form_for #result, :url => edit_pickem_results_path(#pickem), :method => :put, do |r| %>
<%= r.input :first_name %>
<%= r.submit :class => 'btn btn-success', :label => 'Submit Results' %>
<% end %>
Then using the suggestion from Chuck W of #result = #pickem.result.build params[:result], I get undefined methodbuild' for nil:NilClass`.
pickems_controller.rb
def results
#pickem = Pickem.find params[:id]
#pickem.result.blank? ? #result = Result.new : #result = #pickem.result
end
def update_results
#pickem = Pickem.find params[:id]
#result = #pickem.result.build params[:pickem][:result]
if #result.save
redirect_to edit_pickem_results_path(#pickem), :notice => 'The results have been successfully updated.'
else
render "edit"
end
end
Then, your view should look something like this:
<%= simple_form_for #pickem, :url => edit_pickem_results_path(#pickem), :method => :put, do |f| %>
<%= f.simple_fields_for #result do |r| %>
<%= r.input :first_name %>
...
<% end %>
<%= f.submit :class => 'btn btn-success', :label => 'Submit Results' %>
<% end %>
You might have to play around with how the parameters are being passed back to the update_results action (I'm pretty new to rails), but I think you get the gist of it.
i am using rails 2.3.11. I have a text_field_with_autocomplete thing used inside a form of model blog
<% remote_form_for :blog, :url => {:controller => "blogs", :action =>"add_blog"} do |f|%>
<%= f.text_field :title, :class => "admtxt1 wd1 lfloat" %>
<%= text_field_with_auto_complete "user_login", :login, { :size => 15}, { :url => {:controller => 'users', :action => 'autocomplete_for_blogowners'}, :method => :get, :param_name => 'term', :indicator => "spinner"} %>
<%= submit_tag "Save", :class => "btn-small margintop10" %>
<%end%>
In the blogs_controller,add_blog action i have
def add_blog
unless params[:blog].nil?
#blog = Blog.new(params[:blog])
#user = User.find_by_login(params["user_login"][:login])
#blog.owner = #user
end
respond_to do |format|
if #blog.save
#saved = true
format.js { render :action => 'add_blog' }
else
#saved = nil
format.js { render :action => 'add_blog' }
end
end
end
In my add_blog.js.rjs file
if #saved.nil?
page.replace_html 'message','<div id="msg" class = "textcolor3">' + #blog.errors.full_messages.join('<br />') + '</div>'
else
page.replace_html 'message','<div id="msg" class = "h3txt textcolor3"> Blog created successfully </div>'
end
My blog tablehas id,title,owner_id
And my model has the validation
validates_presence_of :owner_id, :title
The above code works perfectly with the validation of owner shouldnt be blank
But it didnt checks for the wrong user
For eg. if i am typing something where such a user doesn't exists .In that case if i am submitting the form with no results found in the box , Even tat time i am getting Owner shouldn't be blank.
How to check the existence of the User in this case and to add that in the error messages.
Your code is very strange. Why don't you use Rails 3?
Why don't you use REST and CRUD ?
Sorry but it is better to rewrite code absolutely.
It seems as php code, have you used php?