I want to reformat some helpers in my Rails views. I want a syntax similar to:
<%=
box :option => 'value' do |b|
b.header "Header of box #1"
%>
Content of Box#1
<%
end
%>
The b.header call is optional.
How would I structure my code to allow this? I guess it's something similar to fields_for in Rails.
You could always look at the source for form_for / fields_for?
Related
I'm using Tiny Mce Editor 4.0.5 in Rails 2.
new.html
<% form_for #custom, :url=>"create" do |c| -%>
<%= c.text_area 'description' %>
<%= c.submit %>
<% end %>
Create Action:
CustomReport.create(params[:custom_report][:description])
After submission of form I get
undefined method `stringify_keys!'
Along that I tried
CustomReport.create(:description => params[:custom_report][:description])
But it doesn't store any HTML tags, so how I can store tags along to my DB?
Using xss_terminate plugin
And in my model CustomReport
xss_terminate :except => [:description]
easy and simple.
CustomReport.create(:description => params[:custom_report][:description])
should do the job for you, but Rails would naturally escape html tags, to stop rails from doing that you would need to call:
[html_safe](http://apidock.com/rails/String/html_safe)
or
raw
in your view on the string with the html tags (This is not safe practice, you should be sure the string is sincerely safe before considering this, as it can expose your app to attacks)
I'm using f.semantic_fields_for with haml
here is the current snippet.
%ul.documents
=f.semantic_fields_for :documents do |u|
= link_to(u.object.comment.presence || u.object.file.original_filename, u.object.file.url)
= u.input :comment, :as=>:string
= u.hidden_field :_destroy
= link_to_function image_tag("/img/del_documets.png"), "remove_fields(this)", :class => "btn"
the problem here is that only "u.input :comment, :as=>:string" is inside the "li" created by formtastic. The links and other fields get somewhere else and its kinda impossible to style correctly this broken html.
Is there anyway to make sure everything get inside the correct li?
Even if I add a li just after f.semantic_fields_for, it will only wrap the first link and the following elements will get wrapped only by the ul completely above.
It seems like formtastic always wraps inputs with <li> to avoid <fieldsets> to be inside <ol>, which is not valid.
Your best way around that is to put <li> around all your elements and add wrapper_html => { :class => :your_class } to your input if you like. That way you can style things your way.
I don't think you need the ul.documents around the inputs, semantic_fields_for already wraps everything using <ol>.
That't the thing with libraries that generate html directly, you'd better adapt your html and css to them instead of trying to make them generate your html.
I would do that (and style my own CSS from it):
=f.semantic_fields_for :documents do |u|
%ul.documents
%li= link_to(u.object.comment.presence || u.object.file.original_filename, u.object.file.url)
= u.input :comment, :as=>:string
%li= u.hidden_field :_destroy
%li= link_to_function image_tag("/img/del_documets.png"), "remove_fields(this)", :class => "btn"
Hope this helps.
You could also override the default behavior of Formtastic when it generates the inputs wrapper like this:
module Formtastic
module Inputs
module Base
module Wrapping
def input_wrapping(&block)
template.content_tag(:li,
[template.capture(&block), error_html, hint_html].join("\n").html_safe,
wrapper_html_options
)
end
end
end
end
end
Notice the content_tag(:li. You could replace it by content_tag(:div for example.
You know how to override module methods in Rails?
In Rails 3, when a scaffold is generated for instance for a 'Category' the will be a categories_path (and a edit_category_path(#category), ...) used in the erb views.
This is not a variable that can be found anywhere and probably is generated. However in my case, for a different entity, Article, I first generated the model and then the controller. Now when I try output an articles_path, I get a
undefined method `articles_path' for #<#:0x000001019d1be0>
I cannot even use a <%= form_for(#article) do |f| %> as this generates the same error.
What am I supposed to do?
My routings are like this:
resources :categories do
resources :articles
end
The articles resource is within the categories scope, so the correct path to use would be category_articles_path(#category) or edit_category_articles_path(#category, #article). To do this for your form_for, try:
<%= form_for([#category, #article]) do |f| %>
As article lives in the category scope, you need to use category_articles_path.
hey there, i'm trying to implement a drag and drop interface for a nested set in my first rails project. i'm new to rails so bear with me. my code is basically identical to this project: http://gist.github.com/128779. my problem is in the partial at this line:
<% for child in root.direct_children do %>
I'm getting a NoMethodError for direct_children which is an instance method of acts_as_nested_set, I believe. At the console if I try to create a new instance of my model, it is likewise unable to access the acts_as_nested_set instance methods, so I don't think the problem is in the partial but in the model.
Again, sorry if my terminology is wrong, I'm new to rails. Anyway, what am I doing wrong? I've got "acts_as_nested_set" in my model, just like the gist example above but my model does not appear to act as a nested set. How do I go about fixing this?
Thanks!
Here's the code for the model I am using (todo.rb):
class Todo < ActiveRecord::Base
acts_as_nested_set
end
And here's the partial:
<% content_tag :li, :id => dom_id(root) do %>
<%= content_tag :span, root.text %>
<% content_tag :ul do %>
<% for child in root.direct_children do %>
<%= render :partial => "tree", :locals => {:root => child}%>
<%end %>
<%end unless root.direct_children.empty? %>
<%end%>
root is passed to the partial from the view like:
<%= render :partial => "tree", :locals => {:root => #root} %>
and #root is defined in the controller like:
#root = Todo.find_by_parent_id(nil)
Again, the code is mostly copied wholesale with very few modifications from the gist link above.
A few things:
Have you checked that you installed the plugin properly? ./script/plugin install git://github.com/rails/acts_as_nested_set.git
Have you set up your table properly? Your model needs to have at least the following 3 columns by default (unless you want to override them): parent_id, lft, rgt. Without these acts_as_nested_set is going to have a hard time figuring out what's going on. I suggest you read the documentation at the top of this file because the readme doesn't say squat, nor does that gist for that matter.
If you've done the above, have you created a root element (not set the parent_id to anything) and then added at least one child to it?
m = Model.new
m.title = "My model's title"
m.save!
m2 = Model.new
m2.title = "My child"
m2.save!
m.add_child(m2)
I just did a quick test using the above, and afterwards I was able to do things like m.root? and m.direct_children. Good luck.
What I make from your title is that you're using quite a bit more than acts_as_nested_set. Try removing some plugins and try again.
Is there a way to use fields_for with in a form without having a scope?
For example:
<% fields_for "user[]" do |x|
<%= x.text_field :name %>
<% end %>
Without the user model being loaded in memory?
I got it working using territory[user][][name], but I would like to keep it in ERB.
I think the answer would be 'no', since those form_for and fields_for would try to determine default value from that given instance variable.
However, I think if you want to lower memory usage from loading that model, you might try to create a mock-up model to return nil values, and create a instance object from that one instead.
is there any specific reason you need to use form_for specifically? Its really designed to be used with an instantiated model object.
Alternatively, why don't you just use the regular form helper tags. You can define it as follows:
<%form_tag :my_form do %>
<%= text_field_tag :foo, :bar %>
<%end%>
You may want to check the documentation for action view to see how it all works.