How can you pass all parameters to a controller action?
# instead of:
<%= link_to mylist_url(id: params[:id], se: "true", st: params[:st], re: params[:re], li: params[:li]) do %> ... <% end %>
# something like:
<% link_to mylist_url(params: :all, se: "true") do %> ... <% end %>
Can you just use Hash#merge?, something like:
<% link_to mylist_url(params.merge(:se=>"true")) do %> ... <% end %>
Related
I have the following form:
<%= form_tag classification_code_rules_path do %>
<% #classification_code_rules.each do |rule| %>
<%= fields_for "classification_code_rule[]", rule do |pf| %>
<%= pf.text_field :name %>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>
It sends what I want to the server:
Parameters: { ... "classification_code_rule"=>[{"name"=>"dasdsaf"}, {"name"=>"sfsdgdfhgf"}, {"name"=>"hfghfgjhgjhg"}], "commit"=>"Save changes"}
Since classification_code_rule is an array, I handle the params as follows:
def classification_code_rule_params
params[:classification_code_rule].each do |rule_param|
rule_param.permit!
end
end
But I am getting the following error:
When assigning attributes, you must pass a hash as an argument.
How can I handle this situation where the strong param is an array?
Try something like this
params.require(:classification_code_rule).map do |rule_param|
ActionController::Parameters.new(rule_param.to_hash).permit!
end
helpers/subcategories_helper.rb:
module SubcategoriesHelper
def has_topic_headings?
self.topic_headings
end
end
categories/show.html.erb contains
<% #category.subcategories.each do |subcategory| %>
<li>
<h6>
<%if subcategory.has_topic_headings? %>
<%= link_to subcategory.name, subcategory, data: :has_topic_headings %>
<% else %>
<%= link_to subcategory.name, subcategory %>
<% end %>
</h6>
<hr>
</li>
<% end %>
The page returns
undefined method `has_topic_headings?' for #<Subcategory:0xa68748c>
Please note that the view page belongs to the Category, not the Subcategory.
You are trying to call it on model which is why it is not called on when helper is included. Helpers are there for views and sometimes for controllers.
Here is your method :
<%= link_to subcategory.name, subcategory, data: :has_topic_headings %>
Edit: Sorry for misunderstanding. the error is in your data which you are passing in your link:
data: :has_topic_headings
rails is asking for above method, not for has_topic_headings?
Edit: Yes, as #techvineet said, you cannot call a helper method on a subcategory object.
you should write method in your subcategory model:
def has_topic_headings?
return true if self.topic_headings
end
Or you can do this in your helper class:
def has_topic_headings(subcategory)
return true if subcategory.topic_headings
end
and in your form, call it like this:
<%if has_topic_headings(subcategory) %>
<%= link_to subcategory.name, subcategory, data: :has_topic_headings %>
<% else %>
<%= link_to subcategory.name, subcategory %>
<% end %>
Hope it will help.Thanks
If I make an array of records like so:
#records = Tapes.all + Discs.all
How can I identify which is which in the view and write code accordingly? Something like this is what I'm after:
<% #records.each do |record| %>
<%= record.side if record.type => :tape %>
<% end %>
Use object.class, or object.is_a?. Something like following:
<% #records.each do |record| %>
<%= record.side if record.class == Tape %>
<% end %>
Or,
<% #records.each do |record| %>
<%= record.side if record.is_a?(Tape) %>
<% end %>
May be a better approach for this is to define a method on both objects that will provide you the expect data:
class Tapes
def quack
self.side
end
...
end
class Discs
def quack
# self.something
end
...
end
Then in your template:
<% #records.each do |record| %>
<%= record.quack %>
<% end %>
I have two model less views.
An index view:
<% #icd1.each do |f| %>
<%= link_to "#{f.von} #{f.bis} #{f.bezeichnung}", icd_show_path(f) %>
</p>
<% end %>
And an show view:
<% #icd1.each do |f| %>
<%= link_to "#{f.von} #{f.bis} #{f.bezeichnung}", icd_show_path(f) %>
</p>
<% f.icd2.each do |s| %>
<%= s.von %><%= s.bis %><%= s.bezeichnung %>
</p>
<% end %>
<% end %>
My controller:
class IcdController < ApplicationController
def index
#icd1 = Icd1.all
end
def show
#icd1 = Icd1.find(params[:id])
end
end
But somehow the link in the index view, wont work:
<%= link_to "#{f.von} #{f.bis} #{f.bezeichnung}", icd_show_path(f) %>
When i try to access the show page i get the error:
Couldn't find Icd1 without an ID
and the url only shows
http://localhost:3000/icd/show
without an id!
My routes:
get "icd/index"
get "icd/show"
1st: Very confusing naming: controller icd, model icd1..
2nd:
get "icd/show/:id", to: "icd#show", as: "icd_show"
or
get "icd/:id/show", to: "icd#show", as: "icd_show"
depends what url you want to get. It is confusing.
but I think this is what you need in your url;
<%= link_to "#{f.von} #{f.bis} #{f.bezeichnung}", icd_show_path(f) %>
and also the routes:
get "icd/:id", to: "icd#show", as: "icd_show"
after this next url will be available:
../icd/1 that will call action show from icd controller
On every page we have a condition like this for guest user.
<% if not_guest? %>
<% link_to "show", path %>
<% end %>
<% if not_guest? %>
<% link_to "delete", path %>
<% end %>
<% if not_guest? %>
<% link_to "edit", path %>
<% end %>
for which link should appear or not for guest user.
Are there any better ways to handle this scenario instead of writing the conditions for every link ?
Make a helper:
#helpers/application_helper.rb
def link_to_unless_guest(*args)
if not_guest
link_to(*args)
end
end
Then call like
<% link_to_unless_guest "show", path %>
def link_to_editable(*args)
options = args.extract_options![:parent]
html_tag = options.nil? ? nil : options.delete(:html_tag)
if not_guest
unless html_tag.nil?
content_tag html_tag,options do
link_to(*args)
end
else
link_to(*args)
end
end
end
<%= link_to_editable 'Show', path,:parent => {:html_tag => "li",:style => "border-top:1px solid #A2A2A2;",:class => "left"} %>
<%= link_to_editable 'Show', path %>
Modified helper which is provided by #Max as per my need.