I'm attempting to hide the links for edit and destroy actions in the view unless the user has logged in using http basic auth. I've attached my files below. Thanks
View https://gist.github.com/1272716
Controller https://gist.github.com/1272712
You need to save/store the authenticate result, and then render conditionally in your view.
Controller:
protected
def authenticate
#authenticated = authenticate_or_request_with_http_basic do |user, password|
user == "x4556d4s" && password == "fd55sas64x"
end
end
View:
<%= link_to "New Link", :controller => :links, :action => :new %>
<table>
<% #links.each do |link| %>
<tr>
<td> <%= link_to '+', up_link_url(link), :method => :put %> <%= link.points %> <%= link_to '-', down_link_url(link), :method => :put %> </td>
<td> <%= link.title %> </td>
<% if #authenticated %>
<td><%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%= link_to 'Edit', edit_link_path(link) %></td>
<% end %>
</tr>
<% end %>
</table>
Related
I have a problem. I have made some filters using ransack and they work fine, but I have no idea how to update the pie chart that I have implemented under the table that I filter. The chart is made using Chartkick. I am fairly new to ror and programing in general. Here is my index file:
<h1>Lista Przychodów</h1>
<h2><%= link_to 'Nowy Przychód', new_income_path %></h2>
<%= search_form_for #q do |f| %>
<div class="field">
<%= f.label :title_cont, "Tytuł zawiera" %>
<%= f.search_field :title_cont %>
</div>
<div class="field">
<%= f.label :text_cont, "Opis zawiera" %>
<%= f.search_field :text_cont %>
</div>
<div class="field">
<%= f.label :category_name_cont, "Kategoria zawiera" %>
<%= f.search_field :category_name_cont %>
</div>
<div class="field">
<%= f.label :amount_gteq, "Kwota pomiędzy" %>
<%= f.search_field :amount_gteq %>
<%= f.label :amount_lteq, "a" %>
<%= f.search_field :amount_lteq %>
</div>
<%= f.submit "Szukaj" %>
<div class="button">
<%= link_to 'Wyczyść filtry', request.path, class:"cancel-button" %>
</div>
<% end %>
<div style="overflow-x:auto;">
<table class="center">
<tr>
<th><%= sort_link(#q, :title, 'Tytuł', default_order: :desc) %></th>
<th><%= sort_link(#q, :text, 'Opis') %></th>
<th><%= sort_link(#q, :amount, 'Kwota') %></th>
<th><%= sort_link(#q, :category_id, 'Kategoria') %></th>
<th colspan="3"></th>
</tr>
<% #incomes.each do |income| %>
<tr>
<td><%= income.title %></td>
<td><%= income.text %></td>
<td><%= number_to_currency(income.amount, :unit => "zł", :format => "%n %u") %></td>
<td><%= Category.find(income.category_id).name %></td>
<td><%= link_to 'Pokaż', income_path(income) %></td>
<td><%= link_to 'Edytuj', edit_income_path(income) %></td>
<td><%= link_to 'Usuń', income_path(income),
method: :delete,
data: { confirm: 'Czy na pewno chcesz usunąć ten wpis?' } %></td>
</tr>
<% end %>
</table>
</div>
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
<footer><%= link_to 'Strona główna', controller: 'welcome' %></footer>
And my controller file if it is needed:
class IncomesController < ApplicationController
helper_method :sort_column, :sort_direction
http_basic_authenticate_with name: "admin", password: "admin", except: [:index, :show]
def index
#q = Income.search(params[:q])
#incomes = #q.result.includes(:category)
end
def show
#income = Income.find(params[:id])
end
def new
#income = Income.new
#categories = Category.all.map { |c| [c.name, c.id]}
end
def edit
#income = Income.find(params[:id])
#categories = Category.all.map { |c| [c.name, c.id]}
end
def create
#income = Income.new(income_params)
#categories = Category.all.map { |c| [c.name, c.id]}
if #income.save
redirect_to #income
else
render 'new'
end
end
def update
#income = Income.find(params[:id])
if #income.update(income_params)
redirect_to #income
else
render 'edit'
end
end
def destroy
#income = Income.find(params[:id])
#income.destroy
redirect_to incomes_path
end
private
def income_params
params.require(:income).permit(:title, :text, :amount, :category_id)
end
def sort_column
params[:sort] || "title"
end
def sort_direction
params[:direction] || "asc"
end
end
I already looked on the internet for a similar problem but didn't find anything. I would appreciate some help with that or at lest some directions what to do. I got really hooked on ror and would like to learn more about it, that's why I came here for help. Thanks in advance for answers.
If you mean the filters are applied on the table but not on the chart, this is because you are making another query for the chart only, in this line:
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
You should use the same instance variable that has the filtered data:
#incomes
So it should be something like:
<%= pie_chart #incomes.sum(:amount), suffix: " zł", thousands: ",", messages: { empty: "Nie ma wpisów" }, download: { filename: "wykres" }, title: "Podział przychodów na kategorie" %>
so on my website, there is an unwanted text node that contains the record of my databse. There is no tag or element whatsoever on the html code, but somehow it exist.
I reckon it is caused by this line of code:
<%= #workspace.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.owner %></td>
<td><%= item.quantity %></td>
<td><%= item.details %></td>
<td><%= link_to 'Edit Item', edit_workspace_item_path(#workspace,item) %></td>
<td><%= link_to 'Delete', [item.workspace, item],
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
The view.html.erb:
<p>
<strong>Name:</strong>
<%= #workspace.name %>
</p>
<p>
<strong>maxitem:</strong>
<%= #workspace.max %>
</p>
<p>
<strong>details:</strong>
<%= #workspace.details %>
</p>
<p>
<%= link_to 'Edit Phase', edit_workspace_path(#workspace) %>
</p>
<p><strong>Items</strong>
</p>
<p>
<%= link_to 'Add Item', new_workspace_item_path(#workspace) %></br>
</p>
<table>
<tr>
<tr>
<th><strong>Name</strong>
<th><strong>Owner</strong>
<th><strong>Quantity</strong>
<th><strong>Details</strong>
</tr>
<%= render 'items/item' %>
</table>
<%= link_to 'Back to List of Phases', workspaces_path %>
I don't know why this suddenly show up on my website
like this
<%= #workspace.items.each do |item| %>
is the issue as <%= %> in ERB stands for "print this". Should be <% %> which will execute the Ruby code without rendering it.
I am currently learning Ruby on Rails, however, I have run into a slight issue, I am unable to create a text field from which to search. I so far have a working DB and web page (albeit an ugly one), however, I am having a problem with this simple function, here is my code so far:
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<strong>Search Title:</strong>
text_field_tag(:id)
<%= #article = Article.find(params[:id]) %>
<% end %>
</table>
error
/Users/jake/Documents/internships/rails/search/app/views/articles/index.html.erb:31: syntax error, unexpected keyword_ensure, expecting end-of-input
just for the web page, any help would be strongly appreciated.
Thanks in advance!
Try looking through this code:
In your html:
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
In your controller:
def index
#projects = Project.search(params[:search])
end
and in the model
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Here is the link
Also as I'm doing in my project, you can create separate action in your index/welcome controller called search:
def search
end
Then configure route in your routes.rb like so(not sure exactly this will work)
get 'welcome/' => 'welcom#search', as: 'search'
And then in view create a form:
<%= form_tag search_path, method: 'get', remote: true, id: "garage-search" %>
<%= text_field_tag :search %>
<% end %>
This will send search request by AJAX to your search controller. There you can process it as needed.
Also try look here. Very clearly explained..
I've been running my head into a wall for hours now and I can't make this work. I'm trying to add a bunch of check boxes to verify items on my index page. I found this old rails cast that does exactly what I want to do, but I've run into a problem. Anything enclosed inside my form is removed from the index page, like just gone poof. Here's the code from the index view.
<% form_tag verify_products_path, :method => :put do %>
<% #products_unverified.each do |products| %>
<% if product.deleted != 'true' %>
<tr data-link="<%= product_path(product) %>">
<td><%= product.name %></td>
<td><%= product.description %></td>
</tr>
<% end %>
<% end %>
<%= submit_tag "Mark as Verified" %>
<% end %>
</tbody>
Here's the routes stuff
resources :products do
put :verify, :on => :collection
end
and the controller just has a dummy method for now.
def verify
end
Any clue as to why the index view blanks out when the form is introduced? Any help is much appreciated.
You forgot the = before form_tag:
<%= form_tag verify_products_path, :method => :put do %>
put an = in front of form_tag
instead of
<% form_tag verify_products_path, :method => :put do %>
try
<%= form_tag verify_products_path, :method => :put do %>
I have a table that seems to adapt to the longest text string in a given column. I have select tags in each row which seem to be messing up the display. There is an extra column being added to the table and the border on the right is not displaying. If I shrink the size of the select tags, the table is formatted correctly, but I'd like to just have the table conform to the size of the largest select tags and largest buttons in the next column. Thanks. Here's the code:
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Position</th>
<th>Allocation</th>
<th>Action</th>
</tr>
</thead>
<% position_map = #cabinet.cabinet_position_map %>
<% #cabinet.cabinet_type.usize.downto(1) do |i| %>
<tr>
<td>Slot: <%= i %></td>
<% if position_map[i].nil? %>
<% cabinet_device_selection = #cabinet.devices_to_fit(position_map, i) %>
<% #selection_list = [ "Power", "I/O", "Interface", "Unusable", "Other", "Missing", "Reserve" ] %>
<% cabinet_device_selection.each do |device| %>
<% #selection_list << device.name %>
<% end %>
<% if position_map[i].nil? %>
<%= form_tag( {:controller => :cabinets, :action => :update_device_position, :position => i , :id => #cabinet.id } , { :method => 'get', :class => "form-search" }) do %>
<td>
<%= select_tag :position_name, options_for_select(#selection_list) %>
</td>
<td>
<%= hidden_field_tag 'position', i %>
<%= submit_tag "Add" , :class => "btn" %>
</td>
<% end %>
<% end %>
<% else %>
<% position_map[i].each do |cabinet_item| %>
<td>
<%= cabinet_item.name %>
</td>
<td>
<%= link_to "Remove", { :controller => :cabinets, :action => :remove_cabinet_position, :id => #cabinet.id, :position => i}, :class => "btn btn-danger" %>
</td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
And here's what the output looks like:
It seems you are using Bootstrap.
So, to change the width of select_tag you can simply do.
<%= select_tag :position_name, options_for_select(#selection_list), :class => "span1" %>
Or, if you want to set custom width, add a class in your css.
.option-small {
width: 100px !important;
}
And then use
<%= select_tag :position_name, options_for_select(#selection_list), :class => "option-small" %>