Rails Params in URL - ruby-on-rails-3

Hello i know this is common problem but i cant let it work.. I need account activation from email link.. url looks like http://domain.tld/activation/name#domain.tld/d4sa89dsa98 and i want access that parameters from controller :-)
routes.rb
match "activation/:email/:activation_key" => "frontend#activation", :via => :get
frontend_controller.rb
def activation
#user = User.find(:conditions => {:email => params[:email], activation_key => params[:activation_key]})
render "activation"
end
and im getting error
No route matches [GET] "/activation/name#email.tld/a46d4sa8dsa68d"
my activation.html.erb
<% if #user %>
Activation successful.
<% else %>
Activation key is invalid.
<% end %>
EDIT: ---------------------------------------------------------------------------------------------------------------------------
I replaced :email parameter with :id parameter because it look nicer, im not sure if
Array.new(32){Random.new.rand(36).to_s(36)}.join is unique string but that does not matter now.
route
match "activation/:id/:activation_key" => "frontend#activation", :via => :get
rake routes
GET /activation/:id/:activation_key(.:format) frontend#activation
frontend controller
def activation
#user = User.find(:conditions => {:id => params[:id], :activation_key => params[:activation_key]})
render "activation"
end
error
Couldn't find User without an ID
but this WORKS so the problem is in the condition
#user = User.find(params[:id])
SOLUTION
#user = User.find(:first, :conditions => {:id => params[:id], :activation_key => params[:activation_key]})

Have you tried adding a constraints: { email: /[^\/]+/ } to your match argument? Possibly the . gets eaten up by overly greedy regex.

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

render partial from controller with :layout => !request.xhr? returns missing template?

I cloned and tweak this rails app and I am experiencing some trouble with rendering a partial when on ajax request, in the logs I see that the guilty line is in the registrations_controller.rb (Devise)
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active_for_authentication?
sign_in(resource_name, resource)
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_sign_up_path_for(resource)
else
resource.update_attribute(:encrypted_password, nil) # make sure there is no password
expire_session_data_after_sign_in!
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render :partial => 'email_capture', :action => :new, :layout => !request.xhr?**
end
end
protected
def after_inactive_sign_up_path_for(resource)
'/thankyou.html'
end
def after_sign_up_path_for(resource)
redirect_to root_path
end
end
The error message returned is this:
ActionView::MissingTemplate - Missing partial
with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder,
:coffee, :haml]}. Searched in: *
"/Users/Davide/Documents/Jobsite/rails-prelaunch-signup-1click/app/views"
* "/Users/Davide/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise_invitable-1.1.8/app/views"
* "/Users/Davide/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/devise-2.2.4/app/views"
Interestingly if I remove the :layout => !=request.xhr? parameter the partial gets found but the page refreshes and the loses all the stylesheets and other assets.
Any idea where should I start looking?
Thanks!
I have also cloned and modified that project, and ran into nearly the same issue. For me, the problem would appear after the second failure of saving a bad email address--but the result was identical.
By using context and information from the following question/answer:
form returned from AJAX request is not recognized as 'remote' when submitted
I was able to make a change that resolved my issue. In _email_capture.html.erb, add :remote => true, to the simple_form_for line. In my case, the whole revised line is:
<%= simple_form_for resource, :as => resource_name, :remote => true, :url => registration_path(resource_name) , :html => {:class => 'form-inline'} do |f| %>
Hope this helps you, too!

Update users details without a password by admin. Rails 3.x Devise

I would like to create a link to approve a users signup,
1) First solution<%= link_to "Approve", edit_user_registration_path(:id => user.id, :approved => true), :method => :put %>
However it drops back No route matches [PUT] "/users/edit"
2) Also I was thinking about an extra action that will respond to a specific route and update the user signup, say match '/users/approve_user', :controller => 'users', :action => 'approve_user', :collection => { :my_action => :put}
and in the view:
%- link_to "Approve", users_approve_user_path(:id => user.id), :method => :put
However, it gives back that Couldn't find User with ID=approve_user
Any help will be appreciated
I think you should use custom routing with devise
This is direct from devise documentation (https://github.com/plataformatec/devise), configuring routes
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
end
so in your case try something like
devise_scope :user do
post "Approve", :to => "users/approve_user"
end
and please note, by default device users controller comes under the name space 'devise'.

Routing with id and handle in Rails

I'm trying to set up rails to use both the ID and the Handle (which is just an URL safe version of the title) of a blog post in the route.
match '/articles/:id/:handle', :to => 'articles#show'
resources :articles
This works, of course -- but I can't seem to set up the to_param method in the model os the longer URL -- with the handle attached, is the default.
This doesn't work (not that I really expected it to):
def to_param
"#{id}/#{handle}"
end
I get a No route matches {:action=>"edit", :controller=>"articles", error. I also tried just using the handle, but then Rails generates links to the resource just using the handle and not the ID. I know I can do it with a - in stead of a /, but I prefer the /. Any way to make this work? If I have to add some extra paremeters to my link_to helpers, that's okay.
Did you try to pass a Hash to link_to?
link_to "Link", {:id => #article.id, :handle => #article.handle}
Update
You have to modify your routes:
match '/articles/:id/:handle', :to => 'articles#show', :as => :article_with_handle
and use the following helper to generate the link:
link_to "Link", article_with_handle_path(:id => #article.id, :handle => #article.handle)
You can override the helper to simplify things:
def article_with_handle_path(article)
super(:id => article.id, :handle => article.handle)
end
and use it like this:
link_to "Link", article_with_handle_path(#article)
Okay, here's what I did to remove the query string problem from the answer above:
Changed the route to this:
match '/articles/:id/:handle' => 'articles#show', :as => :handle
Removed the to_param method from the model and then generated the link like this:
link_to 'Show', handle_path(:handle => article.handle, :id => article.id) %>
That works, but could be condensed, obviously, with the helper above. Just change the one line to: args[1] = handle_path(:id => args[1].id, :handle => args[1].handle)

Rails Routing Error

Rails is giving me a route error even though the route appears to
be in the route list.
The form is doing a Post to try to hit the update route on the Admin::ProductsController.
The edit route, index route, and show route work fine.
Using Rails 3.0.5 and ruby 1.9.2
Anyone have an idea? I can't seem to see the problem.
Error
No route matches "/admin/products/2039"
Code from ERB File that is generating the form
<%= form_for :product, #product, :url => { :action => "update" } do |f| %>
Products Controller method at this point is just a stub of
def update
puts params.inspect
end
Routes File
Analytics::Application.routes.draw do
match 'login' => 'Authentication#login', :via => [:get, :post]
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
root :to => 'AdminInterface#index', :via => :get
resources :products
resources :publishers, :only => [:edit, :update]
match 'publishers/query/:subset' => 'Publishers#index', :as => :publishers_subset, :via => [:get, :post]
end
end
According to your routes, shouldn't that be
= form_for [:admin, #product] do |f|
Your form_for can just be:
<%= form_for #product do |f| %>
If #product is an existing object then it will automatically know to go the update action of the ProductsController.