I came across an interesting problem when I try to alter what gets display if page is certain route'
<body>
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<!---both logged_in and is_admin are in session_helper-->
<%if current_page?(login_path) or current_page?(signup_path)%>
<p class = "title">CLOUD SOLAR</p>
<% else %>
<% if logged_in?%>
<% if is_admin?%>
<%= link_to "All Users", users_path%>
<% end%>
<%= link_to "Settings", edit_user_path(current_user)%>
<%= link_to "Log Out", logout_path, method: "delete"%>
<% else %>
<%= link_to "Sign Up", signup_path%>
<%= link_to "Log in", login_path%>
<% end %>
<% end %>
<%= yield %>
<!-- <%=debug(params) if Rails.env.development?%> -->
</body>
In the code above, I am attempting to get rid of the "sign up" and "log in" link if the current page is either sign up or log in. The above code works fine until I test it with an incorrect email and password. When I test it with a wrong password, a flash message pops up (which it should!), but for some reason the Sign Up and Log in link also pops up underneath it also.
Given the code above, I thought that if the current route is login_path or signup_path, it will never get into the else part of the condition, but somehow it did.
What is going on here? Is the route somehow being altered when I input wrong information even though the url still displays localhost:3000/login or localhost:3000/signup ??
Edit: For clarification, the title disappears completely if the login form has some sort of error. What is replaced is a new login-form and a flash error message. Where did the title CLOUD SOLAR go?
You could try changing login_path and signup_path to login_url and signup_url. Another option would be to hardcode the paths.
current_page?(Rails.env.production? ? 'http://your_live_url/signup' : 'http://localhost:3000/signup')
Also, you could check the documentation here:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F
Related
<ul>
<% #topic.contents.each do |content| %>
<li>
<%= content.content %> <%= link_to "Edit", edit_content_path(content) %> <br><br>
</li>
<% end %>
</ul>
I need someway to test this using Capybara. I tried typing this in my test file:
test "there is an edit link on the show page" do
click_link "Enter"
click_link "Ruby"
assert find_link("Edit").first.visible?
end
But it winds up giving me a message:
1) Error:
VisitorFindContentTest#test_there_is_an_edit_link_on_the_show_page:
Capybara::Ambiguous: Ambiguous match, found 2 elements matching link "Edit"
test/integration/visitor_find_content_test.rb:44:in `block in <class:VisitorFindContentTest>'
What else can I do? What can be done? Should I do something to make each of my edits link unique? OR is there a capybara test method just that looks for the first appearance of an edit link?
Add an :id or a :class specific to the content as follows:
<%= content.content %> <%= link_to "Edit", edit_content_path(content), id: "content_#{content.id}" %> <br><br>
or
<%= content.content %> <span id: "content_#{content.id}"><%= link_to "Edit", edit_content_path(content) %></span> <br><br>
You can then use this id for clicking the link as follows (with the second option above):
content = Content.first # or some way of finding the required content object
within("#content_#{content.id}") do
click_link("Edit")
end
I have already imported bootstrap and I'm using the 'bootstrap-sass' gem and devise gem. The problem is My rails app shows the the right message like if am signed in and i try to sign up it shows the message 'You are already signed in.' but without the color around the message like yellow or red or any color.
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-#{name}") %>
<% end %>
</div>
When I inspect the element in chrome after it rendered I get 'class="alert alert-alert"'
In my code above the "alert alert-#{name}" the name is the key which should change to info, success, or warning or danger. but it keeps changing to alert which bootstrap doesn't have. How can i fix this?
You can fix this by setting your flash message in your controller as flash[:success], flash[:info], flash[:warning] or flash[:danger], as opposed to flash[:alert].
For Devise, you will need to copy the Devise controllers into your Rails application to override these values. Alternatively, you can display the Bootstrap alerts manually if flash[:notice] or flash[:alert] are set:
<% if flash[:notice] %>
<div class="alert alert-info">
<%= flash[:notice] %>
</div>
<% end %>
<% if flash[:alert] %>
<div class="alert alert-danger">
<%= flash[:alert] %>
</div>
<% end %>
In my rails table i have a text_field which my_website, so <%= f.text_field :my_website %>
and so on the show.html.erb there is
Go to my website
and that works fine
but say the user doesnt input anything in the form for my_website, how would i make it work so that this partGo to my website hides if the user doesnt input my_website
Basically something like this
if user puts in my_website, show
Go to my website
else
show nothing
I assume this is after form submission. You can just use an if statement.
<% if #user.my_website %>
<%= link_to "Go to my website", #user.my_website %>
<% end %>
Alternatively, the solution below will not display "Go to my website" if the user inputs a bunch of whitespaces for the my_website field. blank? will return true if #user.my_website is nil or contains an empty string.
<% unless #user.my_website.blank? %>
<%= link_to "Go to my website", #user.my_website %>
<% end %>
I want to comment out something in the application.html.erb
<!--
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Sign out", destroy_user_session_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<% end %>
</div>
//-->
this ist not possible. What is to do?
That's an HTML comment; ERB processing happens on the server side.
<%#
%>
May work across block-ily, it will certainly work line-by-line.
That said, I'm not sure I'd remove functionality by commenting out the entire section.
Instead consider either (a) using source control, or (b) rendering a partial and commenting that out, instead of large chunks of ERB.
For .html.erb file you can use following code for comment.
<!-- Your Comment here -->
I have followed the Devise Wiki to create a very basic admin setup by adding a admin column to my User table in a boolean format.
I have been into my table (through SQlite administrator) and assigned one of my users to be an admin.
I am then have the following code in my view:
<% if user_signed_in? %>
<% if current_user.admin? %>
<%= link_to "Admin Job Post", new_user_job_path(current_user.id) %>
<% else %>
<%= link_to "Post a new job", new_user_job_path(current_user.id) %>
<% end %>
<% else %>
<%= link_to "Post a new job", new_user_session_path %>
<% end %>
The issue I am having is that my app is only ever returning my <%= link_to "Post a new job", new_user_job_path(current_user.id) %> even when logged in with an admin user.
It would be great to get a solution on this because I have tried several variations and can't get it to work.
Thanks in advance for your help!
I'm not sure of the entirety of your code, but I have a similar setup, but I used the following to show a link once a user is logged in as an Admin (I used 'try' due to the fact that it is outside of a 'user_logged_in' check).
<% if current_user.try(:admin?) %>
<li>AdminLink</li>
<% end %>
Are you sure that you made the user an admin?
To make the user admin you should have run the following commands
User.find(#id_of_user_you_want_to_make_admin)
User.admin = true
User.save!
<% if current_user.present? && current_user.has_role?(:admin) %>
<li><%= link_to 'Some Cool Admin Feature', cool_admin_path %></li>
This is what you're looking for if you're using Rails 4 with Devise 2.x. Note the () around :admin aren't necessary, I included them for clarity here as the '?' after the method seems to throw people that are new to ruby.
Another way to give admin privileges is to simply use <% if current_user.try (:email) == "admin#example.com" %> That always works for me. That way you don't have to worry about the whole system.