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 %>
Related
There is a website attr on product_lead table which is optional. If it's present then I wanna turn #produc_lead.lead into a link, but if it's not it should be plain text.
If I use the code below and the website is nil then the link points to the page the user is currently on. If I do it with #product_lead.try(:website), it's gonna be the same. But as I mentioned I would like to have plain text over link in this case.
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
After playing around I fell back to the following solution, but it's terrible. Any better ideas?
<% if #product_lead.website %>
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
<% else %>
<%= #product_lead.lead %>
<% end %>
Maybe link_to_if if Rails 4
<%= link_to_if(#product_lead.website, #product_lead.lead, #product_lead.website) do %>
#product_lead.lead
<%= end %>
You can create custom view helper for this.
Well, link_to is going to generate a <a> tag, whether you provide a valid URL or not. So if the URL is nil, yes, it's gonna be a link for you own page.
If you want to "hide" this, you could call a partial in which you place you if/else and so on, but it's just to sweep this under the rug :)
Or if you wanna go further, as #Jovica Šuša, a view helper is the most elegant solution.
I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>
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 %>
Just trying to save the values of 4 checkboxes into a column named reminders to my Tickets Model.
But, either the values aren't being saved, or when I go back to edit this "Ticket" its not ale to pull the data from the db and display the true / false vaules correctly.
Any advice?
/tickets/_form.html.erb
<%= form_for(#ticket) do |f| %>
<div>
<% [ 'S.T.A.R.T', 'E.N.D.E.D', 'URGENT' , 'Repeat Request' ].each do |reminder| %>
<br><%= check_box_tag 'reminders', reminder, (params[:reminders] || {}).include (reminder) %>
<%= reminder.humanize %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Rather than using check_box_tag the way you are, change it to use your form builder object provided by form_for. It'll ensure that the name of the checkbox is correct which is your problem (I think).
Because you're not using it, the checkbox is probably taking on a different name to what rails is expecting to see, so when you submit the form, the param name won't be associated with your record and thus won't be changed. Try using
<%= f.check_box_tag :reminder %>
Rails will automatically handle whether or not it is ticked when the page loads :)
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