Rails 3.2.8 Remote Form Not Working - ruby-on-rails-3

I have this form, which is not bound to any model, that I want to ajaxify. I've tried to figure out how to get it to submit via ajax, but I must be doing something wrong because it is not working (it just does a regular POST).
I can confirm that the form tag renders with a 'remote' attribute, but there is not js added anywhere to the form. I also added the :confirm just to see if that would work as well. It does not.
jquery and jquery_ujs are both loaded on the page.
%form{ :action => "/newsletter", :confirm => "Are you sure?", :remote => true, :method => "post", :id => "newsletterForm"}
%p
= label_tag(:q, "Subscribe to our newsletter:")
%p
= text_field_tag(:q, nil, :placeholder => "Your email address")
= button_to("Subscribe", :remote => true)

I just wrote this doing something similar with a form get:
= form_tag('/signup', :method => "get", :remote => true, :id=> 'signup-form') do
%label{:id => 'signup-label', :for=> 'signup-box'}
Enter your email address
= text_field_tag "signup-box", params[:signup], :class => 'text', :required => true, :id => 'signup-box'
= submit_tag "Sign Up", :id => 'signup'
Controller:
class SignupController < ApplicationController
def index
puts "***************************************************"
puts "email sign up"
puts "***************************************************"
render :nothing => true
end
end

Related

unable to update role and stripe plan rails

I'm attempting to update my stripe subscription via and edit page but it doesn't seem to like whatever i throw at it.
Here is my routes.rb
get 'subscribe' => 'subscribe#stepone'
get 'subscribe/sliding-scale' => 'subscribe#steptwo'
get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'
get 'subscribe/edit' => 'subscribe#edit', :as => :edit_subscription
match '/subscribe/edit', to: 'subscribe#deleteCard', via: :delete
match '/subscribe/edit', to: 'subscribe#updateSubscription', via: :post
post 'subscribe/edit/changeSubscription' => 'subscribe#changeSubscription', :as => :change_subscription
My subscription controller:
def changeSubscription
customer = Stripe::Customer.retrieve(#user.stripeCustomerId)
#plan = params[:plan]
#user.update_subscription(:plan => #plan, :prorate => true)
current_role = #user.roles.first.name
#user.remove_role current_role
current_user.add_role params[#plan]
end
and lastly my edit view:
<h2>Change My Subscription</h2>
<h3>Your current plan is: <%= current_user.roles.first.name %>
<%= select_tag #plan, options_for_select([['Subscriber'], ['Sustainer'], ['Supporter']]) %>
<%= link_to "Update", change_subscription_path, :confirm => "You sure?", :plan => #plan %>
I'm attempting to send the new plan to the controller to update both the local role and the plan but it seems not to be able to find the proper route to do so. Not sure what i'm doing wrong in this regard.
Thanks for your help!
You need to perform a POST request to change_subscription_path, but you're requesting the page with a simple link_to so it's a GET request.
Add :method => :post in your link_to options:
<%= link_to "Update", change_subscription_path, :confirm => "You sure?", :method => :post, :plan => #plan %>
Alternatively, you can:
use a form instead of a link and submit it with the POST method
change the method for the changeSubscription action from POST to GET in routes.rb

How to display only the value in edit page in Active Admin

I have a edit form in Active Admin. I need some field as read only.
My current edit page is like
I need the page look like this
How can this be done. My code for the edit form page is like
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.input :current_address, :label => 'Current Address', :as => :string
end
end
Please help.
As Alex said, set to disabled. You could then use css to get the visual you wanted, if you can live with the semantics of that.
The syntax was slightly different for me to get this to work.
in your admin form:
f.input :finish_position, input_html: { disabled: true }
in your CSS active_admin.css
input[disabled="disabled"],
input[disabled] {
background-color: #F4F4F4;
border: 0px solid #F4F4F4 !important;
}
For a cleaner form definition within your ActiveAdmin.register{} block you may as well want to define a "readonly" input type to be used within active admin using formtastic:
Form block syntax is for activeadmin version 1.0.0.pre at 0becbef0918a.
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/your_model.rb
ActiveAdmin.register YourModel do
# ...
form do |f|
# ...
input :current_address, as: :readonly
# ...
end
end
I was facing the same issue and tried using :disabled but it did not solve my problem as I wanted field value to be included in params object while sending it to the server. When you mark a form input as :input_html => {:disabled => true} , it does not include this field value in params.
So, instead I used :input_html => {:readonly => true} which solved both of my problems:
Does not allow user to edit
Includes the value in params
I hope this will help.
How about this?
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.li do
f.label :current_address
f.span f.object.current_address
end
end
end
Try to add , :disabled => true for address input field.
The trick is to use "object". Here is how you should code it:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.label :current_address, f.object.current_address
end
end

generate an real url with rails for an e-mail

We want to send an autogenerated E-Mail with an full path to the profile. How do we this
<%=link_to('Link', :controller => 'mycontroller', :action => 'show', :id => #mycontroller.id )%>
The link_to command only builds the link in the mail like this
mycontroller/show/id
we need an link like this structure
http://www.server.tld/mycontroller/show/id
Can everyone help us please?
you can pass :only_path => false
<%= link_to('Link', :controller => 'mycontroller', :action => 'show', :id => #mycontroller.id, :only_path => false ) %>

User signed out halfway through cucumber test

I'm in the process of splitting change-password functionality off into its own page on my site. The whole process works fine when I run through it manually as a real user, but I can't get my Cucumber tests to pass, the reason being that on the final call to the controller, the current_user is mysteriously returning nil.
I've having a serious WTF moment because the user DEFINITELY is being logged in at the beginning of the tests, and I'm following all the same steps as other passing tests, and as I said the whole thing works fine when stepping through it by hand.
Here's the test in question:
Scenario: The user should be able to change their password
Given I have a signed in user with email = "test#mycompany.com" and password = "password"
When I am on the change-password page # this hits registrations_controller#change_password
And I fill in "Current password" with "password"
And I fill in "New password" with "new_password"
And I fill in "Re-enter new password" with "new_password"
And I press "Update"
Then I should see "You updated your account successfully"
And the login step:
Given /^I have a signed in user with email\s*=\s*"([^"]*)" and password\s*=\s*"([^"]*)"$/ do |email, password|
#user = User.new
#user.email = email
#user.password = password
#user.confirm!
#user.save!
visit '/sign_in'
fill_in("Email", :with => #user.email)
fill_in("Password", :with => #user.password)
click_button("Sign in")
end
Controller actions, from "registrations_controller.rb":
def change_password
# calling "current_user" here retuns the logged-in user as expected
#user = current_user
end
def update_password
# calling "current_user" here returns nil
# error occurs at call to nil.update_with_password
#user = current_user
if #user.update_with_password( params["user"] )
redirect_to after_update_path_for( #user )
else
clean_up_passwords( #user )
render_with_scope :change_password
end
end
Now, we are using devise (1.1.8), and my best guess is that I've done something wrong with the devise routes, which look like this in "routes.rb"
devise_for :users, :controllers => {:confirmations => "confirmations", :sessions => "sessions", :registrations => "registrations"} do
# ...other routes here...
get "/change_password", :to => "registrations#change_password"
put "/update_password", :to => "registrations#update_password"
end
Finally, for completeness, here is "change_password.html.haml"
= render :partial => "shared/stats"
#main
= form_for(resource, :as => resource_name, :url => "update_password", :html => { :method => :put, :autocomplete => "off" }) do |f|
= devise_error_messages!
.edit-account-hold
%span Your password
= f.label :current_password, "Current password"
= f.password_field :current_password
= f.label :password, "New password"
= f.password_field :password
= f.label :password_confirmation, "Re-enter new password"
= f.password_field :password_confirmation
= f.submit "Update", :class => "orange-button border-radius"
= link_to "Cancel", account_path, :id => "cancel-link"
:javascript
$(document).ready( function() {
$("#user_current_password").focus();
});
Try running it through Selenium. Watching the test run can often give clues about what's going wrong.

Adding name value with link_to

I'm using a link_to to create an object in my Rails 3 app. Searching has given me the proper way to use link_to with the :post method, but I'm wondering if using link_to to pass in a name value for my object as well. Here is my link:
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
My profiles_controller.rb:
def create_inside
#todo = Insidetodo.new
#todo.save!
if #todo.save
redirect_to #profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
My todo.rb model:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
Is there a way to add in :name => "#{#user.profile.todotext}" to the link_to so that it passes and saves? I don't know if it's creating properly because at the moment when I click a link_to I get a validation error - Validation failed: Name can't be blank.
For passing name in the link_to
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :name => "#{#user.profile.todotext}", :method => :post}, :class => "button white" %>
and the controller must be
def create_inside
#todo = Insidetodo.new(:name => params[:name])
if #todo.save
redirect_to #todo.goal, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
But the link_to will pass the name parameter in url only(like Todo text).
If you want the name not to be sent in the url, you might want to use a form and use a submit button instead of the link as it is representing an action and not a link.
<%= form_tag(:controller => "profile", :action => "create_profile") do -%>
<%= hidden_field_tag :name, #user.profile.todotext %>
<%= submit_tag "Todo Text" %>
<% end -%>