I have an action like this:
def add_credit_card
if request.post?
unless params[:conditions]
flash[:error] = 'You need to accept!'
end
end
end
This action renders the following view:
<%= form_tag do %>
<fieldset>
<%= check_box_tag "conditions"%> I agree to the <%= link_to "Terms and Conditions", consumer_terms_and_conditions_url, :target => "_blank" %>
</fieldset>
<%= submit_tag "Submit" %>
<% end %>
When I do a GET to that action no errors are shown. When I do a submit with that box checked no errors are shown. When I do the first submit without that box checked the error is shown, but the problem comes when I do another submit and the checkbox is not checked, the errors are still there.
My questions are:
Why is that happening?
What would be a better approach to deal with this situation, where a form is not attached to a model and the errors have to be shown just when the user has submitted the form?
Since you're using the same action, you'll want to use flash.now so the flash hash does not persist to the next action.
flash.now[:error] = 'You need to accept!'
Also, it's not essential, but consider using:
<%= check_box_tag 'conditions', 'accepted' %>
and then checking the value of the params[:conditions] for the string "accepted" i.e.:
unless params[:conditions]=='accepted'
flash.now[:error] = 'You need to accept!'
end
Related
So I'm trying to make it so that my submit button disables if you've already submitted the form.
I store your current_user.id when you submit. If you try again you should be met with a submit button that is disabled.
I've been trying to write this with if else statements but it just gets clumsy and doesn't work.
This is what I've tried:
<% if #f.current_user.id.present? %>
<%= f.submit "Submit" %>
<% else %>
<p>Disable code here</p>
<% end %>
EDIT: Taking into account the additional info given in the comments the answer is totally rewritten.
First, in the controller you need to check if current_user has posted before:
# In controller action
#user_already_submitted = Answer.where(user_id: current_user.id,
application_id: #answer.application_id).count > 0
And then in the view:
<%= form_for #answer do |f| %>
<!-- rest of the form -->
<%= f.submit "Submit", :disabled => #user_already_submitted %>
<% end %>
I have a big problem with checkboxes and boolean values. I want to simply change value of one field in db using checkbox. Code is like this:
Collection_controller:
def update
#collection = Collection.find(params[:id]).album
if #collection.update_attributes(:for_sale)
flash[:success] = "success"
redirect_to current_user
else
flash[:success] = "not working"
end
end
def collection_params
params.require(:collection).permit(:to_buy, :for_sale)
end
and form:
<%= form_for collection do |f| %>
<div><%= f.check_box :for_sale%></div>
<%= f.submit "sale", class: "btn btn-primary" %>
<% end %>
Is there anyone who know why it is not working?
EDIT
One important fact, after clicking 'submit' I have flash message 'success'. So why I dont see the changes? It is problem in controller, or somewhere else?
First of all this should be hash of parameters.
http://apidock.com/rails/ActiveRecord/Base/update_attributes
You need to pass in what you want to update. Possibly like so:
if #collection.update_attributes(collection_params)
But it looks like you're incorrectly setting your collection variable to be the album of the collection, so fix that too
#collection = Collection.find(params[:id]).album
should be
#collection = Collection.find(params[:id])
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 %>
Basically I have a follow button and when click the page refreshes and I show an unfollow button in place. Below is the code I use to render the particular form needed:
follow_forms partial:
<% unless current_user?(#user) %>
<% if current_user.following?(#user) %>
<%= render 'relationships/partials/unfollow' %>
<% else %>
<%= render 'relationships/partials/follow' %>
<% end %>
<% end %>
Any I changed the form to an ajax form because I don't want the page refresh and on success of the form submission I'd like to replace the follow button/form with an unfollow button/form. This isn't straight forward because only 1 form shows at a time so I can't use my jquery selector to find this form anyway.
What I decided to do was create a new action that renders the follow_form partial this way the appropriate form will be available for me to manipulate with my jquery selector.
The new action:
class RelationshipsController < ApplicationController
def get_follow_form
respond_to do |format|
format.html { render :partial => 'relationships/partials/follow_form_ajax' }
end
end
end
The problem now is that I don't have access to the #user instance variable. That doesn't matter to much because I can get the user who was just followed via the jquery success data then pass that as data in the new ajax call to get_follow_form_url and then pass that info into the partial as a local variable.
I still have an issue with the #user instance variable not being available. Which brings me to my question.
How can I make another value be used if the instance variable isn't nil/doesn't exist?
The form for following:
<%= form_for current_user.relationships.build(:followed_id => #user.id), :remote => true do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow", :class => 'followButton' %>
<% end %>
Can I do something like this
:followed_id => #user.id <-if this doesn't exist use this-> user.id
There are other ways around this like creating new partials that are only used for this whole situation or creating some messy if statements but I feel like creating duplicate forms should be my very very very last option.
I look forward to you solutions thanks
Kind regards
There's a very simple way to do this, assuming you have your 'fallback' ID:
:followed_id => #user.present? ? #user.id : fallback_id
Use something like the andand gem or just try and a logic expression:
:followed_id => #user.andand.id || user.id
Even without that you can use identical logic, and certainly don't need multiple partials:
:followed_id => (#user && #user.id) || user.id
But as Frederick says, if you have a replacement value for the object already, couldn't you just set it?
So I'm doing some basic validation and trying to add an arbitrary key to the flash hash (you can do that right?)
So in my controller I have this... "previousaction" posts to this page.
if params[:home_value] == "Select One"
flash.now[:home_value] = "Please select a home value"
render "previousaction"
else
#set controller vars.. render this action as normal
end
And in the view:
<% if home_value %>
<h6 id="notice" style="color:red;padding-bottom:5px;"><%= home_value %></h6>
<%= label_tag "Estimated Home Value", "Estimated Home Value", :style => "color:red;"%><br/>
<% else %>
<%= label_tag "Estimated Home Value", "Estimated Home Value" %><br/>
<% end %>
But I get this error when trying to load the controller action (the first time):
undefined local variable or method `home_value'
Tips appreciated :)
For Dave:
In a previous action/view I use flash[:notice] like this:
if params[:zip_code].length != 5
flash.now[:notice] = "Invalid zipcode. Please enter your 5-digit zipcode"
render "firstpage"
else
and then in the view
<% if notice %>
Is flash[:notice] a special flash key for rails?
home_value won't magically be associated with the flash, it's just being treated as a local variable; try accessing the flash hash with :home_value directly.
That said, what's the purpose of using an arbitrary key?