How does one add RedCloth to a form_for? - ruby-on-rails

An embaressing question, but I can't seem to translate the documentation to an actual form_for. This is all the site provides..
RedCloth.new("Some text").to_html
#=> "<p>Some text</p>"
I get that that's how I parse it after its been saved. But how do I save it as marked up text?
Here's my attempt at beginning this, but I don't know how to set the parameter to save the textarea as RedCloth. Any ideas?
- form_for #text do |f|
# some RedCloth instantiation
f.submit

You don't save the parameter parsed as RedCloth like that, nor would I recommend it. Parsing it into RedCloth will result in the original value being lost unless you stored the output in an alternate field, which is what I would recommend.
You can use a before_save in your model to parse that value and store it:
before_save :parse_text
# your model methods go here
private
def parse_text
self.parsed_text = RedCloth.new(text).to_html
end
When you want to render the parsed_text value in your view you'll have to tell Rails that it's safe by doing this:
#object.parsed_text.html_safe
However, the code contained here does not account for people mixing Markdown and HTML, so be very careful how you use it.

Related

Can a `<button type="submit">` be used in place of a `f.submit` in a Rails `form_for` form?

I'm looking to standardize all the buttons and button-styled elements in our Rails app with a button component. This means rendering a styled <button> element with type "button" or type "submit". I'm making use of the view_component gem provided by github!
I am hoping to be able to replace all Rails form helper-y elements such as button_tag, submit_tag and f.submit with a button element. I'm pretty new to Rails, and I am not sure if there are things happening under the hood I'm not taking into account. Looking at the Rails documentation, it doesn't seem like there anything special with the f.submit form helper element.
Am I missing something? Are there consequences to replacing rails form helper submits with a button[type='submit']?
It would be long way to change stuff, so Better to use css and adjust css to get your requirements.
Just add following in you code to create your own my_submit_button
### app/helpers/application_helper.rb ###
module ApplicationHelper
def my_submit
# decide what the submit text should be
text = if #record.new_record?
"Create #{#record.class}"
else
"Update #{#record.class}"
end
# build and return the tag string
my_submit_tag(text)
end
end
further you can read here
The form_for helper

Calling Rails helper from rabl Rails

I'm trying to call a Rails helper from my rabl template, but it's not working.
I have an image object and I want to pass its pass through my api. So then image path should have the full url (Ex: http://<my url>/uploads/myimage.jpg). The host part I want to add through a Rails helper.
#my rabl file
object #image
attributes :id, :picture_url
What I want is something like (I tried this but doesn't work)
#my rabl file
object #image
attributes :id, full_url(:picture_url)
#app/helpers/application_helper.rb
module ApplicationHelper
def full_url(path)
"#{request.host}#{path}" #something like this
end
end
But when I do this, it completely drop the :picture_url attribute from the json object
I believe this might work for you:
object #image
attributes :id
node(:picture_url) { full_url(:picture_url) }
node allows you to define things that do not directly map to object attributes.
EDIT 1
It looks like I got the node syntax wrong. Try this instead:
node(:picture_url) { |i| full_url(i.picture_url) }
By calling full_url(:picture_url), attributes gets a string that doesn't correspond to a method on the #image object, i.e., #image.id returns something, while #image.http://... isn't even valid :).
I would suggest you leave the view as it was first, and set the value of the #image.picture_url using your helper in the controller.

Understanding function form_for() in Rails

I'm currently reading Beginning Rails 3. I'm coming from PHP and trying to learn Ruby and Rails. I'm looking at a _form partial and I have a few questions. Specifically about the line:
<%= form_for(#article) do |f| %>
What is the purpose of having the #article object in there as well as what is the function of variable f?
thanks,
mike
form_for accepts a model so that it can do a few things for you under the covers:
It will read any current values off of that model and populate them in the fields you specify
It will generate the proper URL for that resource (assuming you're following conventions, otherwise you still have to specify it)
It can display any validation errors on the model if you're displaying after a POST.
If you just want the tag helpers, there's also form_tag and friends
The #article is what the form is for (in this case).
The f is for creating individual form elements; it's a builder object yielded by form_for's block.

Rails: Proper way to add functionality to rails methods

I'm just starting to tinker with extending the rails framework, and as an experiment, I thought I'd add some extra info inside the form_for helper. Specifically, when form_for is called, I'd like to generate an extra h1 tag such as:
# regular form_for <form> opening tag
<h1>Woohoo! It's added!</h1>
# tags fed into form_for via &proc
# form_for close <form> tag
At the moment I've added a /lib file that opens up ActiveRecord::FormHelper and overrides "form for". Needless to say writing out the whole form_for method with just the one added line added is dog ugly...but I can't call super() because, well, instead of inheriting from the method I'd like to super(), I've just overwritten it in /lib.
So, assuming I stubbornly want the functionality to be called via the same form_for tag (instead of, for example extended_form_for), what's the standard way for calling back to the original form_for method I'm overwriting? alias_method_chain? Thought I'd ask before I cement in some potentially lousy practices. If any hardened veterans could give an example I'd be appreciative.
Cheers
You could override form_for in your ApplicationHelper:
module ApplicationHelper
def form_for(*)
content_tag(:h1, "Woohoo! It's added!") + super
end
end
alias_method_chain is by far the simplest way to overwrite the method while still being able to call the original method. So in your lib file you'll want something like this:
def form_for_with_header(...)
form_for_without_header(...)
content_tag(:h1, "Header tag here")
# etc...
end

Submitting custom information to a controller

This is probably simple, but I've tried a few things and couldn't find a way to make it work.
I would like to update a model with custom information given in a form_for
To make it more concrete, I'm on the show page for a particular instance of MyClass and I would like to pass something like the string "yay" into the controller, and then do as I please with the input. Maybe pass it back to the page as a flash message, or maybe modify the contents and then store it as a field of the MyClass instance.
I can write form_for's that contain the attributes of MyClass without prbolems, but it seems that other fields throw an error.
How do I write the form_for so that I can accomplish one of the two above scenarios?
def update
#my_class = MyClass.find(params[:id])
flash[:notice] = "This works" # but what can I write in a form for for it to be a variable that's passed in?
#rest of the update
end
Form helpers that unitize a form builder instance (like f.text_field) expect a valid model attribute so it can generate the appropriate id and populate the field with data from the model. If you want to have form fields that do not correspond to model attributes, don't use the the standard f.text_field but instead use:
<%= text_field_tag 'my_custom_tag' %>
which should render something like:
<input type="text" id="my_custom_tag"></input>
When the form is submitted, the value of the input will show up in the params hash with a key of :my_custom_tag.
I hope this helps.
It seems that you would probably need a hidden_field in your form :
http://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field
However, if you wish to save some kind of state, which seems like this is what you want, you would never use that. Instead, you would use a session. The reason is that a hidden field can be manipulated by the client and thus security can easily be overridden.
Like Spyros said, a hidden field will give you the place. Assuming you are ok with the fact that a user can modify the URL, just add attr_accessor :foo to your model.
In the controller you can access it with bar = params[:foo] and do as you please.

Resources