Getting same style in another page when click to login - ruby-on-rails-4

I am having a login page for user generated by devise gem.where I removed the registerable from model for only login not for signup.I have also generated an admin which gives default login credentials for user in seed.rb file.I have done some css work in login page. next I have generated an employee page for further process for users.
Here my doubt is the styling part what I have done in login page is also coming in employee page. I dont want that so I wrote some condition in application.html.erb
<% unless user_signed_in? %>
<%= render 'layouts/heading' %>
<% end %>
The method is defined in application.controller.rb
def user_signed_in? %>
render :layouts => 'application'
end
I tried a lot by changing conditions but it is still displaying or showing errors.

You said that you're using "device gem", but since you're talking about authentication, I assume you mean "devise"?
The user_signed_in? method is a helper which should be provided automatically by devise, and you should be able to use it in your views. You shouldn't need to define it in the application controller.
You probably want to make sure you do "before_filter :authenticate_user!" in the relevant controller (you can use the ":only" option if you only want this to apply for certain methods).
It's difficult to give any more information because you haven't said what errors you are getting. I assume you're getting some kind of syntax error on the definition of the "user_signed_in?" method, since you have an invalid "%>"on the end of the line, which I guess you copied-and-pasted from an ERB file.

Related

Rails 4 not changing post method to patch

I am trying to submit a form, but if I just put form_for #classroom I get a "No route matches [POST]" error.
Now with the code posted below, I get the wrong url in the form. If I change the url manually in the browser it goes through, and I guess I could do that via javascript, but... why... is... this... happening..?
Until yesterday everything was working fine. I even tried rolling back to the things I changed but I can't seem to track what is going wrong.
routes.rb
patch 'classrooms/:id/update' => "classrooms#update", as: :update_classroom
resources :classrooms, except: :update
form from rails end
<%= form_for(update_classroom_path(#classroom), method: "patch") do |class_f| %>
form in browser
<form action="/classrooms/23/edit" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="******">
rake routes
absences POST /absences(.:format) absences#create
POST /classrooms/:id/getAbsences(.:format) classrooms#getAbsences
update_classroom PATCH /classrooms/:id/update(.:format) classrooms#update
classrooms GET /classrooms(.:format) classrooms#index
POST /classrooms(.:format) classrooms#create
new_classroom GET /classrooms/new(.:format) classrooms#new
edit_classroom GET /classrooms/:id/edit(.:format) classrooms#edit
classroom GET /classrooms/:id(.:format) classrooms#show
DELETE /classrooms/:id(.:format) classrooms#destroy
root GET / pages#start
Just to answer your question from title, I think your form method is "PATCH" indeed. Refer to the guide http://guides.rubyonrails.org/form_helpers.html about how rails makes a patch form.
The Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PATCH" and "DELETE" requests (besides "GET" and "POST"). However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.
Rails works around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:
To add a bit of specificity, to Tashow's answer above (which set me on the right track), I had some hidden fields that look'd like this, in a nested form.:
<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", nil) %>
<%= hidden_field_tag("Classroom[classroom_teachers_attributes][]", '') %>
Once I got rid of these, everything began working properly again. (There still remained similar-looking <input> tags generated by fields_for, etc.)
After some more trial and error, I realised I had left some plain input tags in a deeper nested level of the form (instead of going with the normal fields_for and separate builders for each level). I guess that somehow screwed up the relations and affected the method of the parent form.
That was such a mind blending mess up.
Edit: Andylee's answer is right. What I and Jeremy mention was probably the actual issue going on and not what was originally assumed to be the problem (as mentioned in the title).
form_for takes an object as first argument, and its usually better to keep the REST-like way of rails handling the update method.
The action of you html form displays "/classrooms/23/edit" so yes it won't work.
form_for(#classroom, url: update_classroom_path(#classroom), method: "patch")

url_for in an email sent by a rake task in rails 4

I have a rake task that sends out daily digest emails of player activity during a day. (See example code below.) If I run PlayerActivityMailer.activity_report.deliver in my console, everything works just fine. However, when I try to invoke the rake task, I get the following error:
rake aborted!
ActionView::Template::Error: arguments passed to url_for can't be handled.
Please require routes or provide your own implementation
After doing some research, I found that in Rails 4, they totally nerfed ActionView::Helpers::UrlHelper.url_for (http://apidock.com/rails/v4.1.8/ActionView/Helpers/UrlHelper/url_for - notice the giant red minus sign under the 4.0.2). If you look at the source, you can see the error I'm seeing - it no longer takes options. As far as I can tell, that functionality still exists in other url_fors, such as the one in ActionDispatch::Routing::UrlFor. Also, the error message suggests including Rails.application.routes.url_helpers.
What I've tried
include ActionDispatch::Routing::UrlFor in both the rake task (inside the task) and the mailer (both at the same time, and each separately)
include Rails.application.routes.url_helpers in the same places and configurations, both with and without the UrlFor include.
The error still persists. My guess is that the page view is still insisting on using the ActionView::Helpers::UrlHelper version of url_for. I don't think I can include things actually in the views (which is sloppy looking and hacky even if I could).
Example Code
(heavily sanitized)
config/environtments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
lib/tasks/player.rake:
namespace :player do
task :activity => :environment do
PlayerActivityMailer.activity_report.deliver
end
end
app/mailers/player_activity_mailer.rb:
class PlayerActivityMailer < ActionMailer::Base
def activity_report
#activities = PlayerActivity.all
mail(to: 'foo#bar.com', subject: 'activity report')
end
end
app/views/player_activity_mailer/activity_report.html.erb:
<% #activites.each do |activity| %>
Player: <%= link_to activity.player.name, player_url(id: activity.player.id) %>
...
<% end %>
I also have a model Player, resources :players in my routes.rb file, and a PlayerActivity class with an association to Player.
I'm currently using the (really horrifying) workaround of #base_url = Rails.configuration.action_mailer.default_url_options[:host] in my mailer action and "http://#{#base_url}/players/#{activity.player.id}" in my view instead of the player_url part.
Help!
Have you tried passing just your player in the URL? Like this:
<% #activites.each do |activity| %>
Player: <%= link_to activity.player.name, player_url(activity.player) %>
<% end %>

How to auto set a form value on the next page using link_to? (Rails 4)

I am new to rails and am looking to do something very simple but don't know the correct syntax. I have 2 buttons one for creating a new page and one for creating a new post. Both a page and a post save in the same database table and are controlled through a boolean field called 'static'. A page therefore has a static value of 1 and a post 0. What I want to do is simply auto set this value in a form (and hide it) when I click new page or post. I imagined the link to create a new page would work something like this:
<%= link_to 'New Page', new_page_path(:static => "1") %>
This doesn't work so I tried to create a new_static page action and a new_post page action with correcting routing (for displaying only pages I created a show_static action used the following link_to and it works fine):
<%= link_to "Pages", show_static_pages_path(#pages), :method => :get %>
The problem is when I created the new_static page action it expects an id for some reason.
new_static_page GET /pages/:id/new_static(.:format) pages#new_static
I would prefer to not mess around with new actions and routing and simply set the value with link_to. Any tips would be greatly appreciated.

dots in rails URL

I'm trying to add a show page to my devise users but can't seem to get the id value passed properly, how do I create a link to the show page? For some reason rails is doing this to URL
/show.1
users controller:
def show
#user = User.find(params[:id])
end
user/show.html.erb
<h1><%= #user.username %></h1>
link to user profile also gives me issues id no method
<%= link_to #post.email, users_show_path(#user.id) %>
routes.rb
get "users/show"
get "pages/faq"
get "pages/about"
devise_for :users
resources :posts
You have defined the show route as:
get "users/show"
Notice that there is no dynamic segment in this route, like :id. That means your route is not expecting any value to be passed. BUT while linking to this route you passed #user.id
<%= link_to #post.email, users_show_path(#user.id) %>
which the route was not expecting. So, Rails assumed that you are passing the format (like .html, .json, etc.) which is why you see the url formed as users/show.1.
To fix this, I would suggest you to add a dynamic segment to your route to capture the id of a user correctly.
Change
get "users/show"
With
get "users/show/:id" => "users#show", as: :users_show
For a user with id = 1, when you click on the user profile link the url generated would be http://yourdomain/users/show/1

How do I make a method available inside a partial?

I'm a rails beginner learning rails 4 and I'm trying to learn by doing. I'm making a simple blog that I want some simple user authentication on. I'm trying to learn here, so I don't want to implement Devise, etc. I have a header partial that takes care of my site header and I'm trying to put a link to logout that only shows if a user is logged in. I have a simple session controller that has a new action for the signup form, a create action that sets the current user after matching the email and password and sets session[:user_id] = #current_user.id, and a destroy action that nils out the session. In my application controller I have a method like this
def logged_in?
!session[:user_id].nil?
end
In my _header.html.erb partial, I have
<% if logged_in? %>
(My link)
<% end %>
When I load the page it tells me it can't find the "logged_in?" method. Anyone know why? Thanks.
Methods on controllers are by default not exposed to the views (which your partial is part of).
2 solutions:
Create your logged_in? method as a helper method, for example in an AuthenticationHelper. Doing this you cannot access it from controllers, though.
Expose your controller method to the view using helper_method:
class ApplicationController < ActionController::Base
helper_method :logged_in?
def logged_in?
[...]
end
end
Couldn't you just use:
if #current_user