How can I create a new layout and have it show up only in certain situations using ruby padrino? - ruby

I'm using padrino with my website, and need to override the default layout to use a different layout in certain situations i.e. when a user clicks on a certain thing.

Padrino documentation gives these examples:
SimpleApp.controllers :posts do
# Apply a layout for routes in this controller
# Layout file would be in 'app/views/layouts/posts.haml'
layout :posts
get("/posts") { render :haml, "Uses posts layout" }
end
SimpleApp.controllers :admin do
get :index do
render "admin/index", :layout => :admin
end
get :show, :with => :id do
render "admin/show", :layout => false
end
end

Related

How to implement "change your profile picture" feature (server side)

Im trying to implement a feature on my project, so users can change their profile picture. I'm currently at the point where everything on the front end works (when you hover the mouse over the image it shows camera and when you click it, you can browse through your computer to select an image file) Yet, it doesnt work after that point.
I'm not really sure how to handle server request.
FYI
im using routes.rb and controller .rb files to handle backend.
Also,
should i use iframe, instead of form tag in html?
Thanks!
You don't need an iFrame. Simple form-for ruby helper will help in this case. I will suggest you to use a file-uploading gem like paperclip.
Create a model for your records:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
(I am supposing the model name to be user)
Create a new migration with generator:
rails generate paperclip user avatar
Update your html form as follow:
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
Then do following changes in the controller:
def create
#user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:avatar)
end
To display image anyware on the page use the following helper:
<%= image_tag #user.avatar.url %>
This is the easiest way to handle image uploading at server side in rails. You can read more at the paperclip github page.If you don't want to use a gem then then there is a ruby class FileUtils which can help you in achieving same goal.

Devise and current_page?

In my Rails 4 app i use devise to authenticate my users and i've setup a custom nav bar creator using HAML and the application_helper.rb. The code in the helper uses current_page to decide where to add an 'active' class to the HTML element because im using the twitter bootstrap. Now everything i've said works fine such as when you go to the root of the site the button in the bar is active (pushed in) but if you click on another item in the bar and go to a pag such as /sig_in/ you get sent to the page but the button for that page is not active. Im not sure wether this is a devise or ruby problem. Here is my code:
routes.rb
devise_for :users, :path => ''
_nav.html.haml
= navigation_link_to 'Home', root_path
/ Insert Additional Nav
= navigation_link_to 'Sign in', new_user_session_path
application_helper.rb
def navigation_link_to(text, path)
link_to text, path, class: "btn#{active_link(path)}"
end
def active_link(path)
' active' if current_page?(path)
end
For me this is working quite well (I use 3.0.0.rc devise gem in Rails 4)
In routes.rb only:
devise_for :users
I have a _login.html.haml which is inserted in _navigation.html.haml. Path helpers provided by Devise automatically. If user already signed in then different links will be appeared.
%li=link_to 'Registration', new_user_registration_path
%li=link_to 'Sign in', new_user_session_path unless user_signed_in?
-if user_signed_in?
%li=link_to 'Manage your registration', edit_user_registration_path
%li=link_to 'Logout', destroy_user_session_path, method: :delete

How to setup a rails 3 route for a nested resource and custom controller action

I have a recommendation that is nested below Categories and Awards.
So Category/:id/awards/:id/recommendations/:id
I have an Assets model that handles paperclip attachments to the Recommendation. A Recommendation has_many Assets, Assets belong_to :recommendation
In my Recommendation new/edit views I am rendering a form partial (as is normal) that gives the user the option to upload several assets.
If there are Assets already related, then it lists them. I am working on setting up a custom delete action on the recommendations controller.
My current link_to:
<%= link_to "Delete Attachment",
{:controller => :recommendations, :action => :destroy_asset, :id => asset.id },
{:remote => "true", :confirm => "Are you sure you want to delete this image?"}
%>
My Controller action:
def destroy_asset
##recommendation = Recommendation.find(params[:id])
#asset = Asset.find(params[:id])
#asset.destroy
respond_to do |format|
format.js
end
end
routes:
resources :recommendations
resources :categories do
resources :awards do
resources :recommendations
end
end
I am still learning remote => true, and how to route this sucker. Not sure if I need to have the route nested or not. I tend to think not. Since I have an Asset.id in my loop, I should just be able to execute the destroy without needing the Recommendation at all.
So the question/s: do I need a route to access a custom action in my recommendations_controller?
thanks
Try this:
resources :categories do
resources :awards do
resources :recommendations
member do
get :destroy_asset
end
end
end
end
Or If you only want only destroy_asset action under recommendation controller, do something like this :
resources :recommendations
member do
get :destroy_asset
end
end
Or If you want custom match, do something like this :
match "asset/:id/destroy_asset", :to => "recommendations#destroy_asset",
:as=> "destroy_asset"

Updating (Hiding) a group of objects in Rails 3

I've some difficulty in determining any way to update my objects as a group. I have an Alert model which has some alerts for the user. I'd like to provide a way to update them in bulk, as I've noticed that I often want to. I just don't know Rails well enough to know what 'the Rails way' would be.
My controller:
class AlertsController < ApplicationController
def index
#alerts = Alert.all(show: true)
end
def destroy
alert = Alert.get(params[:id])
## I lied, I'm not really deleting things, just hiding them from showing
alert.update(show: false) if alert
respond_to do |format|
format.js { render :nothing => true }
end
end
I've been using this code in my *.html.haml view to create the delete link for each individual alert:
- #alerts.each do |alert|
%span{ :class => 'description' }=alert.description
= link_to "Destroy", alert, :confirm => "Are you sure", :method => :delete, :remote => :true
What I want to have is a link on my index page Delete All which I could use to hide(update) all of the alerts. I've been attempting this with a destroy_all method for my controller, but I felt like it wasn't quite the right approach, as I didn't know how to link to something like that. Thank you for pointing me in the right direction.
Here is the destroy_all method I'd hacked together, for reference.
def destroy_all
#alerts.each do |alert|
alert.update(show: false) if alert
respond_to do |format|
format.js {render :nothing => true}
end
end
end
I don't know how you have routed to this controller but I would probably do something like this:
#routes.rb
resources :alerts do
delete :remove_all, :on => :collection
end
This will create a route looking like /alerts/remove_all which can only be called through the DELETE protocol. By specifying :collection it tells the routing the this route is not a sub route to one specific Alert so no :id is included.
Then the controller action could probably look something like this:
def remove_all
Alert.update_all({ show: false }, { show: true })
respond_to do |format|
format.js {render :nothing => true}
end
end
The update_all function will make sure that there is only one database update instead of one for each alert that is to be removed. The first argument is a hash containing the updates and the second argument is a hash with the conditions for which records are to be updated.
And from the view you should be able to link to the action like this:
= link_to "Remove All", remove_all_alerts_path, :confirm => "Are you sure", :method => :delete, :remote => :true
remove_all_alerts_path is a path helper that is automatically generated when you use the above syntax in routes.rb

Form behavior posting nested resource

I am using Ruby on Rails 3 and I would like to change its conventional behavior on posting a form in order to post from a signup action to the create action instead that from the new action. That is, I would like to use the signup action instead of the (conventional) new action in my User controller and trigger the create action to save my model data that contains nested resources.
In my /config/routes.rb file I have:
resources :users do
collection do
get 'signup'
end
resource :profile
end
In my /app/controllers/users_controller.rb I have
class UsersController < ApplicationController
def signup
#signup_user = User.new(params[:user])
#signup_user.build_profile # NOTE: Nested resource
...
end
def create
...
#signup_user.save
respond_to do |format|
format.html { render :action => :signup } # signup.html.erb
end
end
end
In my /app/views/users/signup.html.erb file I have
<%= form_for #signup_user do |f| %>
...
<% end %>
My problem is that if I submit the above form, I will be redirected to the index action of the user controller and not to the create action I expect. It seams that the form posts only to the index action.
How can I solve the problem?
I tryed to use the following
<%= form_for( :user, #signup_user, :url => { :controller => "users", :action => "create" }, :html => { :method => :post } do |f| %>
but I have still the problem: I am redirected to the index action.
SOLUTION
The problem did seam to be in the routers.rb. The correct code was
resources :users do
collection do
get 'signup'
post 'create'
end
resource :profile
end

Resources