This is a simple question but I can't seem to find a solid answer. I simply want to know if this is valid? it's a basic form_for with in input at the bottom.
## Form
<%= form_for #snitch, html: { class: "form-actions", method: 'delete' } do |form| %>
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span>
<button type="submit" class="button button--modal delete-snitch" data-snitch-id="<% #snitch.token %>" value="Yes, delete it.">
<% end %>
Is the third line valid? specifically where it says data-snitch-id="<% #snitch.token %>"? if it is not. can someone help me figure out how I can do something like that?
HTML data attributes are perfectly valid and widely supported. They're used to store custom data in an element. You can create elements with those attributes in rails helpers as well.
<%= button_tag "Yes, delete it.", type: :submit,
data: {"snitch-id" => #snitch.token},
class: 'button button--modal delete-snitch' %>
The only problem with your example is that you're not printing the value of #snitch.token. You should be using <%= #snitch.token %> instead of <% #snitch.token %>
Use #{ } expression instead.
data-snitch-id="#{#snitch.token}"
Related
I am currently working on a registration form. Users should only register with an email-adress belonging to a specific domain (e.g. "#example.com"). I want to add this domain as a text behind the email-input-field to make it clear for the users what to enter. Unfortunately it seems impossible to write something behind an input-field as rails automatically does a line break.
This is the relevant part of the form:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email, autofocus: true %>[#example.com]
</div>
<div class="actions">
<%= f.submit "register", class: "button" %>
</div>
<% end %>
The result should look like [ ] #example.com
Also I need the email-adress to be an actual email-adress. Therefore I also need to manipulate the input e.g. "john" to "john#example.com" before saving the user to the database.
How can I achieve these two things?
The strict answer to your question is that in your controller action you are free to manipulate or set attributes on an instance before you save it.
def create
#foo = Foo.new(foo_params)
#foo.email = mangle_email(#foo.email)
if #foo.save
... # normal stuff
end
end
In your particular case, you should consider the various input scenarios (e.g., user adds the #domain on their own, etc.), since there are lots of cases where just appending something to the end of the user input is probably not what you're after.
I need to nest an 'a' element inside of another 'a' element like so:
<a href="www.example.com" class="so-and-so">
<span>something</span>
<i class="an-icon">
</a>
However I need to use the rails 'link_to' helpers instead of plain html. But when I try to nest the link_to helper like this:
<%= link_to "www.example.com", class: "so-and-so" do %>
<%= content_tag :span, "something" %>
<%= link_to "www.another.example.com", class: "another-link" do %>
<%= content_tag :i, class: "an-icon" %>
<% end %>
<% end %>
It outputs html more like this:
<a ...>
<span ...>
</a>
<a class="another-link" ...>...</a>
Any help would be greatly apreciated. Thank you!
EDIT
It turns out that rails was outputting the correct html after all, but the browser is not interpreting it correctly. It it possible to have a link inside of a link?
SOLVED (ISH)
It turns out that nesting <a> elements is not valid HTML, so the browser was just trying to "correct" my code. I ended up just placing the a element outside the other one and using CSS to make it look the same.
Since you need the inner link tag point at a destroy action on a controller. You can trigger the action by the click event of the first link.
$('#first_link').on('click', function() {
$.ajax({
type: "DELETE",
url: YOUR_SECOND_LINK ,
data: {id: YOUR_ID },
success: function(data, textStatus, jqXHR){...},
error: function(jqXHR, textStatus, errorThrown){...}
})
});
This can be achieved by using the <template> element which will prevent the browser from closing an anchor tag before another one is opened.
<%= link_to "www.example.com", class: "so-and-so" do %>
<%= content_tag :span, "something" %>
<template>
<%= link_to "www.another.example.com", class: "another-link" do %>
<%= content_tag :i, class: "an-icon" %>
<% end %>
</template>
<% end %>
I worked this out because I was refactoring Vue components that had nested anchor tags into html.erb files. It is a requirement of each of the individual Vue components that it is wrapped in a <template> tag so I copied it over and voila!
I have simple html.erb form like below
<table>
<%= text_field_tag :tesintg %>
<th><%= button_to 'Apply now', apply_product_index_path(:value => "Want Value of input in text field here") , method: :post %></th>
</table>
When "Apply now" button is pressed I want the value in the testing text_field_tag to be posted as query params as {"value" : "Value in the text field"}
How do I achieve this.
I think the best way to do something like that is just to create form_tag
<%= form_tag apply_product_index_path, method: :post do %>
<%= text_field_tag :teasing %>
<%= submit_tag %>
<% end %>
This will pass to your controller hash params: { teasing: 'value passed as teasing }. You can easily use it from there with params[:teasing].
You don't need to grab value from text_field_tag and put it into button.
Also remember that if you are creating new object, very ofter preferred way is to use form_for tag which uses specific model. I'm not sure what are your intentions, so i'm not going to rewrite everything that has already beed said. You can read much more in here: http://guides.rubyonrails.org/form_helpers.html
I'm working with forms using Embedded Ruby and Rails 4 to create and edit Users. Each User has to be assigned a Role on creation. Initially the form was using check boxes for this and was working fine. Upon changing over to radio buttons however, I get an error.
This is the form:
<%= simple_form_for(#user, html: {class: 'form-horizontal'}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, autofocus: true %>
<%= f.input :email %>
<%= f.input :phone_number %>
<%= f.input :institution_pid, collection: institutions_for_select, as: :select, label: "Institution" %>
<%= f.association :roles, collection: roles_for_select, as: :radio_buttons %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
</div>
<br>
<div class="form-actions">
<%= button_tag(type: 'submit', class: "btn doc-action-btn btn-success") do %>
<i class="glyphicon glyphicon-check"></i> Submit
<% end %>
<%= link_to #user, {class: "btn doc-action-btn btn-cancel"} do %>
<i class="glyphicon glyphicon-remove"></i> Cancel
<% end %>
</div>
I'm specifically asking about the f.association bit. Before, when I was using
as: :check_boxes
it worked exactly as it was supposed to. Now I get this error message:
NoMethodError in UsersController#update
undefined method `reject' for "77":String
I should note that "77" is the value of one of the radio button options.
The method throwing the error is this:
def build_role_ids
[].tap do |role_ids|
roles = Role.find(params[:user][:role_ids].reject &:blank?)
roles.each do |role|
authorize!(:add_user, role)
role_ids << role.id
end
end
end
The HTML when using radio buttons looks like this:
<label class="radio">
<input class="radio_buttons optional" id="user_role_ids_77" name="user[role_ids]" type="radio" value="77">
"Institutional Admin"
</label>
When using check boxes:
<label class="checkbox">
<input class="check_boxes optional" id="user_role_ids_77" name="user[role_ids][]" type="checkbox" value="77">
"Institutional Admin"
</label>
If I'm missing something or you need more information, please let me know. Thank you!
With checkboxes, user can select multiple values so you got an Array of role_ids returned in params[:user][:role_ids]. reject method is implemented for Arrays. Hence, it worked in that case.
With radio buttons, only one value would be selected at a time, so you'll get a String value for role_ids in params[:user][:role_ids]. reject method is not implemented for Strings. Hence, the error.
Instead of
params[:user][:role_ids].reject &:blank?
you can check if role_ids is empty or not, as it is a String object.
params[:user][:role_ids].empty?
and update build_role_ids method keeping in mind that role_ids is a String object and not an Array.
This error seems to indicate that params[:user][:role_ids] is not an Array but a single String value. This would make sense since you are changing from checkboxes (multiple values can be selected at a time = Array) to radio buttons (only a single value can be selected at a time = String).
If you truly want to change from checkboxes to radio buttons, then you'll need to update your build_role_ids method logic to expect a single value instead of an array of values.
I know that if you just type something like <button>Something</button> outside a form_for in rails, it will create a useless button.
But I want to create buttons within this form_for to be handled by JavaScript.
Is there a way to create it?
This will create useless buttons that can be handled by JavaScript.
Plain HTML:
<input type="button" onclick="alert('Hello.')" value="Click Here" />
Rails:
<%= submit_tag "Click Here", :type => 'button', :onclick => 'alert("Hello.")' %>
If you're not looking for Rails to use it, why not just use the plain html inside the form_for?
<%= form_for #record do |f| %>
## inputs ##
<button>Something</button>
<%= f.submit %>
<% end %>
Check out this answer: How do I create multiple submit buttons for the same form in Rails?
<% form_for(something) do |f| %>
..
<%= f.submit 'A' %>
<%= f.submit 'B' %>
..
<% end %>