I'm using the best_in_place gem for in place editing. I have this around a link that I want editable, but what's displayed to the user should be "Edit Link".I want to be able to edit the link when this field is clicked on, but I want what's displayed to the user (the anchor text) to be "edit link". I tried using "display_with" and "value" but neither has worked.
It's currently displaying the value of the database field which is the URL: https://plus.google.com/+SteveQuatrani/posts
<%= best_in_place c, :google_maps_url, value: "Edit Link" %>
Is there a way to do this? Thank you in advance for your help!
display_with requires a helper or proc, so try giving it one:
<%= best_in_place c, :google_maps_url, display_with: Proc.new {"Edit Link"} %>
Related
Given a standard select code:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
Can someone explain how I would go about creating a text field when 'Other' is selected? So that the type_name can be something other than the options in the select?
I realise this is a simple question, but I haven't found a concise answer as of yet!
There are lots of ways to do this, but they all require JavaScript. The general approach I like is to put a hidden text field in the form, then attach a JavaScript event handler to the select tag that shows the field when the "Other" option is selected.
Here's a gist of the script I typically use for this. It handles the JavaScript binding using data attributes. Add the script to your assets, then put something like this in your form:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
<%= f.text_field :type_name_other, "data-depends-on" => "#object_type_name", "data-depends-on-value" => "Other" %>
where #object_type_name is the HTML id of your dropdown.
You need to create an attr_accessor on the model f is attached to (like type_name_other), add a text_field to the form below the select for type_name_other in a div that is initially hidden (in CSS: display:none), then create a javascript listener that detects when the select form has changed and if the selected ansser is "other" show the hidden field else hide it. You will then need to see if type_name_other has a value when processing the form and use it if so.
In my view, I'm using this to display the user
Made a comment on <%= link_to activity.trackable.micropost.user, activity.trackable.micropost.user %>
When I do this, it works, but the link shows up as something like #<User:0x5424a68>
I tried using activity.trackable.micropost.user.username, activity.trackable.micropost.user.name, and other variations but they didn't work.
What do I need to add after .user?
The activity.trackable is from the PublicActivity gem.
Open rails console and type:
User.instance_methods.grep(/name/)
It will give you a list of methods on User that contain the string 'name'. Chances are, that you will find the method you are looking for in the list (if there is any).
Try
<%= activity.trackable.micropost.user.inspect %>
or
<%= activity.trackable.micropost.user.to_s %>
This should give you a decent idea of what you need to add..
In my form, I used the span tag like the following:
<%= content_tag :span, f.object.User, class: 'username' %>
It looks like the following in HTML after i selected the value:
<span class="user" style="">Antony</span>
The problem is id doesn't get the value to the database when we create a form. I don't know the exact problem is. I want to use this content tag instead of text_field to get the value.
Thanks.
When you submit an HTML form, the only values that get POSTed are those that are in input fields such as text fields, selects, checkboxes, buttons, etc. Content that is simply on the page -- in a span or not -- will not get posted back to the server. That isn't a Rails issue, it's just the way HTML works.
I'm not exactly sure what you're trying to do here, but a common approach when you want to display a value (not in an input box) and also post the value back with the form, is to both render the value on the page (in a span or however you want) and also add a hidden input field (hidden_field_tag) that also has the value in it.
Yeah, Jacob is correct. Better create a hidden field
<%= f.hidden_field :user, class: 'user' %>
<%= content_tag :span, f.object.User, class: 'username' %>
The first line get the value in it. I hope, Jacob answer would help you. :)
How can I change submit button on form to text?
<%= f.submit "Text" %>
I know how to change text on button but I need to change a way to submitting.
You can press on link, for example Submit and submit.
You don't say what ruby version you are using. But try this
<%= submit_tag "Text" %>
Note that there is no form helper here, so no "f." in front
In order to change the submitting of a form to be triggered by a link as opposed to a submit button, you must use javascript instead of raw generated HTML only.
See similar answers on StackOverflow but a summary of a possible solution in jQuery (which comes baked into recent versions of Rails) would be as follows:
$('#submit_link').click(function(){
$('form').submit();
return false;
});
Update: This should do it using HAML notation in Rails 2.3.x:
- form_for(#object) do |f|
= f.submit 'Update Or Whatever'
You could also try "button" tag or use an image link that looks like a button.
I am doing my first project using Ruby on Rails and need to display a set of radio buttons. It should behave exactly like a selection list. For usability reasons, I need it in a radio buttons format.
In my project, I use the collection select which also allows me to display on the edit page as follows:
select('project','project_type_id',#project_types.collect{|project_type|[project_type.name,project_type.id]}) <br>
I need something exactly like this, (especially the ability to display the selected value in the edit page) but using radio buttons.
I did a Google search and read the entire Rails guides on radio buttons but I can't find the answer.
How can I do this?
I suppose you can do it like this in your view
<% #project_types.each do |project_type| %>
<%= radio_button("project", "project_type", project_type.name) %> #assuming you have a name attribute on project_type
<% end %>
If you want a particular radio button to be checked then you can pass the checked option like so
<%= radio_button("project", "project_type", project_type.name, {:checked => true}) %>