In my controller, I'm getting all of a user's associated #highinterest accounts, and I'm adding the sum of the value column:
#dohighinterest = Account.where(user_id: current_user, accounttype: 'Savings', name: ['GE Capital Bank', 'Barclays', 'CIT Bank', 'Bank5 Connect', 'Ally Bank', 'Discover', 'First Choice Bank', 'FNBO Direct', 'Mutual of Omaha', 'Sallie Mae Bank', 'American Express Bank', 'Capital One 360'])
#highinterest = #dohighinterest.sum(&:value)
Later on in the controller, I'm defining variables so that the view knows which class to render.
if #highinterest > (#rechighinterest * 0.8) && (#highinterest < (#rechighinterest * 1.2))
#highrec = "pass"
elsif #highinterest > (#rechighinterest * 0.6) && (#highinterest < (#rechighinterest * 1.4))
#highrec = "okay"
else
#highrec = "fail"
end
Here's the view:
<div class="rollup <%= #highrec %>">
<p>You're gaining interest on</p>
<div class="percentage">
$<%= number_with_delimiter(#highinterest, :delimiter => ',') %>
</div>
</div>
Additionally, I'd like to render a partial in the view if #highrec = "fail" and #highinterest = 0.
What's the best practice for putting that logic into the controller and view? I attempted to define a new variable in the controller #rectext = true if it meets that condition. Then in the view, I wrapped the partial reference to an <%= if #rectext = true %>, but that wasn't returning anything.
I don't think you need a separate variable to hold value of #highrec == "fail" && #highinterest == 0. You can use this same condition in your view. If however there are more variables in play, you could introduce a variable to keep the view cleaner!
<% if #highrec == "fail" && #highinterest == 0 %>
<%= render partial: 'path_to_other_partial' %>
<% else %>
<div class="rollup <%= #highrec %>">
<p>You're gaining interest on</p>
<div class="percentage">
$<%= number_with_delimiter(#highinterest, :delimiter => ',') %>
</div>
</div>
<% end %>
The line you tried <%= if #rectext = true %> would result in syntax error because of <%= tag, which is trying to output the value returned by if #rectext = true. So, a point to note would be, using <%= tag is going to output the value returned by the statements within the tags and <%(without the equals) is going to evaluate only without printing!
Secondly, you are assigning #rectext = true in the if statement, it should be a comparision, i.e. either if #rectext == true or just if #rectext.
You can define a helper method inside folder app/helpers/controllername_helper.rb that recieves the values of #highrec and #highinterest as parameters and then returns true or false depending on your needs.
Then in the view call the method this way:
<% if helper_method_name(#highrec, #highinterest) %>
<%= render partial: 'partial_path' %>
<% end %>
Note that in this code you write :
<%= if #rectext = true %>
you are assigning #rectext the value true and not checking if it is true. Also in this case you should use <% instead of <%=
Related
I m new to RoR and i recently figured out that if I change the part of URL from the address bar it is just rendering the partial twice below I m illustrating the same.
So my url is localhost:3000/admin/exp_app/10 so if i refresh this page it gets rendered one time but if i change the url to localhost:3000/admin/exp_app/11 the same request gets twice to controller and it is rendering the partial two times .
Can someone help me?
Below i m attaching the part of code.
exp_controller.rb
def show
authorize! :sales, current_user
#filter = params[:filter] || 'all'
#filter_by = params[:filter_by] || 'all'
#analytics_filter = params[:analytics_filter] || 'all'
#uw = params[:uw_id]
#entity = params[:entity]
#leo_no_brc = params[:leo_no_brc].present?
#exporter_application = User.exp_leads_applications.find(params[:id]) rescue nil
user = User.find(params[:id]) if #exporter_application.nil? || can?(:mexico_sales, current_user)
#total_ltm = #exporter_application.shipment_volume_last_year(nil, false).to_i
#exp_clients = #exporter_application.clients
#exp_applications_clients = #exp_clients.where(status: Client::CLIENT_STATUS_ORDER).group_by(&:status)
#exp_task_instances = #exporter_application.task_instances.where(client_id: nil)
#exporter_application = User.exp_leads_applications.includes(:iec, :contacts,
interactions: [:replies],
task_instances: { interactions: [:replies] },
application: { exporter_user: [:iec] })
end
show.html.erb
<% if params[:_tpl] %>
<div class="admin-section tabpanel row">
<%= render partial: params[:_tpl], locals: { instance: #exporter_application } %>
</div>
<% else %>
<%= render 'admin/header' %>
<%= render partial: 'admin/exporter_applications/show' %>
<%= render 'admin/footer' %>
<% end %>
It is guranteed that this url is not called in partial again admin/exporter_applications/show
Thanks in advance!
I have a field called params[:search_loc] that's for a public user where they can put in there address and get results near there. I'm trying to save it in a cookie so when a public user leaves a page they can come back and have the same address until they change it. Here is the view and the controller. This is what I tried.
Controller
def index
#dad = Dad.find(params[:dad_id])
cookies[:search_loc] = params[:search_loc]
if user_signed_in?
near = Mom.near(#user_location, 500, select: "kids.*")
elsif cookies[:search_loc].present?
near = Mom.near(cookies[:search_loc], 500, select: "kids.*")
elsif params[:search_loc].present?
near = Mom.near(params[:search_loc], 500, select: "kids.*")
end
end
View
<% if params[:search_loc].blank? %>
<%= form_tag dad_kids_path(#dad), method: :get do %>
<%= text_field_tag :search_loc, params[:search_loc] %>
<%= button_tag(type: 'submit') do %>
Save
<% end %>
<% end %>
<% end %>
<% end %>
Right now it doesn't set the cookie for the address but it is saved in the browser. How can I get it to show in the search_loc field after I leave to another page?
You are setting a cookie, but not accessing it. Try this
elsif params[:search_loc].present?
cookies[:search_loc] = params[:search_loc]
near = Mom.near(params[:search_loc], 500, select: "kids.*")
elsif cookies[:search_loc].present?
near = Mom.near(cookies[:search_loc], 500, select: "kids.*")
end
View:-
<% #dating_advices.each do |da| %>
<% if da['is-displayed'][0]['content'] == 'true' %>
<div class="dating_advice">
<h4><%= da['title'][0] %></h4>
<p><b><%= da['author'][0] %></b></p>
<p><%= da['content'] %></p>
</div>
<hr>
<% end %>
<% end %>
Controller:-
def dating_advices
#current_menu = "MatchMasters"
logger.debug "*** Current site id: #{#current_site.id}"
##hide_quick_search = true
#passed_args = {
'event_action' => 'ws',
'site_id' => #current_site.site_id
}
result = call_dating_advices_ws(#passed_args)
if result && result['errorcode'][0] == 'success'
#dating_advices = result['payload'][0]['payload']
end
end
Now when I click on dating advices on my webpage it gives an error"Getting Template::Error (Undefined method 'each') for nil:NIL Class"
You probably didn't get any #dating_advices -- which would mean you're calling each on an instance var that doesn't exist.
The way your controller is written, #dating_advices is never defined if the result variable is not defined.
You could quickly patch it with a #dataing_advices = [] (or a hash -- anything with an .each method) and figure out why your controller is failing.
You could do something like
if result && result['errorcode'][0] == 'success'
... your dating advice code
else
raise result
(or #dating_advice = {})
end
basically you need to always defined #dating_advice OR you need to handle that value not being defined in your view.
My edit action has a series of radio buttons in the view. I want to fill in the value of the currently selected field. I managed to get this to work, though I feel the code could be better and also perhaps should be in the model.
controller:
def edit
#rating = Rating.find(params[:id])
#a,#b,#c,#d,#e,#f,#g,#h,#i,#j = false
if #rating.environ == 1
#a = true
elsif #rating.environ == 2
#b = true
elsif #rating.environ == 3
#c = true
elsif #rating.environ == 4
#d = true
.
.
.
etc.
view:
1<%= f.radio_button :environ, 1, :checked => #a %>
2<%= f.radio_button :environ, 2, :checked => #b %>
3<%= f.radio_button :environ, 3, :checked => #c %>
.
.
etc..
What is your model code? I am guessing a rating has many environs?
In any case, you can just loop through all of them in the view and make your :checked argument a boolean.
something like
<% #environs.each do |env| -%>
<%= f.radio_button :environ, env.id, :checked => (#rating.environ == env) %>
<% end -%>
Hopefully this question is quick and painless
I have a mvc view where i want to display either one of two values depending on an if statement. This is what I have in the view itself:
<%if (model.CountryId == model.CountryId) %>
<%= Html.Encode(model.LocalComment)%>
<%= Html.Encode(model.IntComment)%>
If true display model.LocalComment, if false display model.IntComment.
This doesn't work as I get both values showing. What am I doing wrong?
Your if statement always evaluates to true. You are testing whether model.CountryId equals model.CountryId which is always true: if (model.CountryId == model.CountryId). Also you are missing an else statement. It should be like this:
<%if (model.CountryId == 1) { %>
<%= Html.Encode(model.LocalComment) %>
<% } else if (model.CountryId == 2) { %>
<%= Html.Encode(model.IntComment) %>
<% } %>
Obviously you need to replace 1 and 2 with the proper values.
Personally I would write an HTML helper for this task to avoid the tag soup in the views:
public static MvcHtmlString Comment(this HtmlHelper<YourModelType> htmlHelper)
{
var model = htmlHelper.ViewData.Model;
if (model.CountryId == 1)
{
return MvcHtmlString.Create(model.LocalComment);
}
else if (model.CountryId == 2)
{
return MvcHtmlString.Create(model.IntComment);
}
return MvcHtmlString.Empty;
}
And then in your view simply:
<%= Html.Comment() %>
Aside from Darin's point about the condition always being true, you might want to consider using the conditional operator:
<%= Html.Encode(model.CountryId == 1 ? model.LocalComment : model.IntComment) %>
(Adjust for whatever your real condition would be, of course.)
Personally I find this easier to read than the big mixture of <% %> and <%= %>.
Conditional Rendering in Asp.Net MVC Views
<% if(customer.Type == CustomerType.Affiliate) %>
<%= this.Html.Image("~/Content/Images/customer_affiliate_icon.jpg")%>
<% else if(customer.Type == CustomerType.Preferred) %>
<%= this.Html.Image("~/Content/Images/customer_preferred_icon.jpg")%>
<% else %>
<%= this.Html.Image("~/Content/Images/customer_regular_icon.jpg")%>