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 -%>
Related
My f.select is not working
this is my form -
- #anonymous.each do |a|
=form_for #confession , html: {multipart: true} do |f|
=f.label :Confess
=f.text_area :confession , require: true
=f.label :post_as
=f.select(:postid,options_for_select([[#confession.amitian.fullname,#confession.amitian.fullname],[a.fullname,a.fullname]]))
=f.file_field :confessionimage
=f.submit 'Confess'
and this is my controller class ..
def index
#amitian = Amitian.where(institute: current_amitian.institute) if amitian_signed_in?
#confessions = Confession.where(amitian_id: #amitian.ids).order('created_at DESC') if amitian_signed_in?
#anonymous = Amitian.where(email: 'anonymous#anonymous.com')
#anonymous.each do |a|
#debug = a
if params[:postid] == 'Anonymous'
#confession = a.confessions.build
else
#confession = current_amitian.confessions.build
end
end
end
my if statement is never true ... why ?
A clear f.select statement from above is this =
f.select(:postid,options_for_select('yourname','Anonymous')
so whenever user select anonymous if statement should return true
You need to check params[:postid] in create action, not in index.
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 <%=
I'm using Kaminari on my site with a 'load more' button to show another six items when clicked. It works great but when I try to add a sorting order it's not passing the params to the link_to_next_page def although I can see it in the html...
The other question asked on this said to pass the params to the link_to_next_page but it doesn't make a difference.
Example: When I try to sort by lowest price > highest price the first six items are sorted but on 'load more' the sorting order is random.
Can anyone advise here??
Thanks.
Some code...
index.html.erb
<div id="offers">
<%= render :partial => #television_offers %>
</div>
<%= link_to_next_page #television_offers, 'Load More', :remote => true, :id=>"load_more_link", :params => params %> </div>
index.js.erb
$('#offers').append("<%= escape_javascript(render :partial => #television_offers)%>");
$('#load_more_link').replaceWith("<%= escape_javascript(link_to_next_page(#television_offers, 'Load More', :remote => true, :id=>'load_more_link', :params => params))%>");
application_helper.rb
def link_to_next_page(scope, name, options = {}, &block)
param_name = options.delete(:param_name) || Kaminari.config.param_name
link_to_unless scope.last_page?, name, {param_name => (scope.current_page + 1)}, options.merge(:rel => 'next') do
block.call if block
end
end
television_offers_controller.rb
def index
#television_offers = TelevisionOffer.page(params[:page]).per(6)
if params[:filter] == "large_screens"
#television_offers = #television_offers.large_size
elsif params[:filter] == "small_screens"
#television_offers = #television_offers.small_size
elsif params[:filter] == "price"
if params[:order] == "asc"
#television_offers = #television_offers.asc(:offer_price)
else
#television_offers = #television_offers.desc(:offer_price)
end
else
#television_offers = #television_offers.best
end
end
For anyone experiencing the same problem this was solved by simply updating kaminari to the latest version
My view:
<%= radio_button_tag "recommendation_ratings[#{rec.recommendation.id}][rating]", '3'%>
In my controller, I parse out these values:
input = params[:recommendation_ratings].values
input.each do |mini_params|
rating = mini_params[:rating]
recommendation_id = mini_params[:recommendation_id]
Using the rating variable I create a new records in the same controller action:
rate = Rating.new(
:rating => rating,
:label => rating_label)
How can I assign a rating to a rating_label when a new record is created. Example:
if rating == 1 rating_label = "Fair"
Should I do this in my controller, or can I add the rating_label to my radio_button_tag as another attribute?
UPDATE:
I tried a case statement in my controller but get an NoMethodError "Undefined Method Rating"
rating case
when rating == 1
rating_label = "Bad"
when rating == 2
rating_label = "Fair"
when rating == 3
rating_label = "Good"
else rating == 0
rating_label = "N/A"
end
Refactor your view code from using radio_button_tag to a fields_for block:
<%= fields_for :recommendation_ratings do |f| %>
<% f.hidden_field :id, rec.recommendation.id %>
<% f.radio_button :rating, 3 %>
<% end %>
Change your controller code to this:
selected_rating = params[:recommendation_ratings]
rating = selected_rating[:rating]
recommendation_id = selected_rating[:id]
And add a helper method somewhere as:
def rating_label(rating)
%w{N/A Bad Fair Good}[rating.to_i]
end
Now you can make a new Rating object like this:
rate = Rating.new(
:rating => rating,
:label => rating_label(rating))
Absolute RoR newbie here, I'm trying to render out multiple leagues in a loop, incrementing the div_# each time, here's a cut down version, without the html. It works when I hard code div_1 or div_2 to be sorted, but div_name doesn't work, even though it has the right contents I need it to be seen as the array.
<% div_1 = Array.new
div_1 << { :Name => 'Rob', :Played => '2', :Won => '1', :Lost => 1, :Points => 4}
div_2 = Array.new
div_2 << { :Name => 'Gavin', :Played => '2', :Won => '1', :Lost => 1, :Points => 4}
for i in (1..2)
i = i.to_s
div_name = "div_" + i
div_name.sort_by { |position| position[:Points] }.reverse!.each do |position| %>
<%= position[:Name] %>
There are lots of problems here:
div_1 = Array.new
div_1 << { :Name => 'Rob', :Played => '2', :Won => '1', :Lost => 1, :Points => 4}
div_1 is now an array with a single element, which is a hash. You don't need an array if you'll just have one element in it.
for i in (1..2)
Where's the block for this loop? After that statement, i is still undefined, so when you call
i = i.to_s
you'll get a NameError.
div_name = "div_" + i
Even if i == 1, div_name will be a string with value 'div_1', and not a copy or instance of the div_1 variable you define above.
div_name.sort_by { |position| position[:Points] }.reverse!.each do |position| %>
Now you're trying to call sort_by on a string, which doesn't respond to that, because it doesn't make sense.
<%= position[:Name] %>
You don't have a variable named position defined at this scope.
Also, when you find yourself putting lots of logic inside of a view within <% %> tags, that's a sign that you need to move that code elsewhere, like to the controller. You could define and calculate #positions as an array of hashes in the controller, and then in the view do something like:
<% #positions.each do |position| %>
<%= position[:name] %>
<% end %>