Ruby on Rails: If Current Page? Is Homepage, Don't Show Form

Ruby on Rails: if current page? is homepage, don't show form

Use the root_path helper:

<% unless current_page?(root_path) %>
show some stuff
<% end %>

This doesn't show if the url is localhost:3000/projects But it shows
if its localhost:3000

or:

<% unless current_page?('/') || current_page?('/projects') %>
# '/' the same as root_path
show some stuff
<% end %>

Also, according the documentation, no need url_for method:

current_page?(controller: 'projects', action: 'index')

Setting up a form on Ruby on Rails where there is only an email to submit on a homepage without using the Devise gem

As per Rails and REST convention you shouldn't put the form in home.html.erb instead do the following,

In your users controller add new method and your controller will look like below,

class UsersController < ApplicationController

def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
redirect_to 'pages/success'
else
redirect_to 'pages/error'
end
end

private

def user_params
params.require(:user).permit(:email)
end

end

Now move your form to new.html.erb in users folder like below,

<%= form_for @user, url: users_path do |f| %>
<div class="wrapper">
<%= f.text_field :email , id: "search", class:"search input" %> <br />
<%= f.submit "yep", class: "submit input" %>
</div>
<% end %>

And don't forget to add routes,

root "users#new"

Also, model user.rb should have uniqueness validation

validates_uniqueness_of :email

Show Rails Contact Form on main page

And easy and clean way would be to create a partial

_contact_form.html.erb (Partials always starts with an underscore)

.container
%h1 Contact
= simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f|
= f.input :name, :required => true
= f.input :email, :required => true
= f.input :message, :as => :text, :required => false, :input_html => {:rows => 10}

.hidden
= f.input :nickname, :hint => 'Leave this field blank!'
.form-actions
= f.button :submit, 'Send message', :class=> "btn btn-primary"

Then, in your index page:

<%= render "contacts/contact_form" %>

and in your controller index action( I don't know if 'welcome/index' is on project's controller or contacts controller)

def index
#your code
@contact = Contact.new
end

Finally, you seem quite new to rails, I'd like to recommend a free Ruby on Rails Tutorial

If current page posts_edit_path then show a text

You can do like this:

 <div id="field_browse">
<b><%= f.label :Bildes %>:</b><br />
<%= "Lai izdzēstu bildes, ieklikšķiniet tās un publicējiet rakstu." if action_name.eql? "edit" %>
<br /><br />
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<div style="clear: both;"></div>
<%= asset.file_field :image %>
<%= f.link_to_remove "Noņemt" %>
<% end %>
<% unless asset.object.new_record? %>
<p>
<div id="pictures_in_form"><%= link_to image_tag(asset.object.image.url(:thumb)), asset.object.image.url(:big), :class => "fancybox", :rel => "gallery" if asset.object.image? %></div>
<div id="delete_box"><%= asset.check_box :_destroy if asset.object.image?%></div>
</p>
<% end %>
<% end %>
<%= f.link_to_add "Pievienot bildes", :assets %>

Make condition to the partial render on all pages except root (ROR)

You can try the following to nice rails condition

<%= render partial: "partials/header" unless request.fullpath=='/' %>
OR
<%= render "partials/header" unless request.fullpath=='/' %>

Or nicer and understandable

<%= render(:partial => "partials/header") unless request.fullpath=='/' %>

Also, this to be understandable

<% unless request.fullpath=='/' %>
<%= render partial: "partials/header" %>
<% end %>

To your layout

Hope it helps

Rails feedback form: How to get the url for the current page?

You can use request.referer in your controller to get the path of the page that called your action. request.referer returns a full url but you can parse it with the URI module:

URI(request.referer).path

I see some people have suggested request.fullpath but this is actually the path of the action that's processing the request (in your case /feedbacks/new) and not the path where the form was submitted from.

How can I check which page has sent the form?

In your controller, you have access to the request variable (type of ActionDispatch::Request) which represents the actual request received by your server:

def index
puts request.inspect # see in your server's console the output
# ...
end

With this variable, you can access to the referer, which returns the path (as String) of the last page seen, or nil if you came from another domain (or blank page).


To check which page sent your form, you could use:

def my_controller_action
if request.referer.present? && request.referer.include?('/string/to/check/')
# yay!
else
# well, the request is not coming from there
end
end

You could also set it as a before_filter in your controller in order to check "quietly" the requests and not in each controller's action.

How to remain on the same page in case of unsuccessful sign up when using Devise? (Sign up form being displayed on root page)

me again.

I have solved the problem with a friend of mine after quite a bit of hustle.

Here goes a path to take that enables to disable automatic redirection to the devise form and enable to set your own and remain on the root_path after sign_up failure:

1) Add these lines to your routes in order to override the direct call-back to the Devise generated forms and to custom your user journey.

 devise_for :users, controllers: {
registrations: 'users/registrations'
}

By doing so, you tell Rails that you are changing variables of the Devise Controller for Registrations.

2) In your controllers/users/registrations_controller.rb create a new method create.

def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
render "pages/home"
end
end

What I did here was only to change the last parcel from:

else
clean_up_passwords resource
set_minimum_password_length
respond_with resource, :location => after_sign_in_path_for(resource)
end

to

else
clean_up_passwords resource
set_minimum_password_length
render "pages/home"
end

3) Then you have to create a new file in your lib folder named custom_failure.rb (anywhere in the lib folder works perfectly fine). In this file, add the following lines:

class CustomFailure < Devise::FailureApp
def redirect_url
'/'
end

def route
'/'
end

# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
end

Here you override respond to eliminate recall.

4) Then inside config/initializers/devise.rb you add these lines:

config.warden do |manager|
manager.failure_app = CustomFailure
end

Basically, here you tell devise to look at the custom_failure.rb file you just created in case of a failure.

5) Finally, in your config/application.rb add these lines to your module:

config.autoload_paths << Rails.root.join('lib')

Here you tell Rails to automatically load your 'lib' folder and all the files that are in them (this step is not mandatory for all users it seems, but my rails app did not already automatically load the 'lib' files).


I hope this helped some of you.

Enjoy your code.

Best,
Ist



Related Topics



Leave a reply



Submit