How to create a website form field in Rails 3? - ruby-on-rails

I have a form where I'd like to be able to input a web address, like "google.com", into a form field, and be able to click on this link in the show view of the submitted form.
How can I accomplish this?
show.html.erb
<p>
<strong>Website:</strong>
<%= link_to #video.website, #video.website %>
</p>

Scaffolding will handle it like Jason Swett said. If you are looking to put it in a link in show just do something like this:
<%=link_to #link.name, "http://"+#link.url%>
If that doesn't work you could always do:
<%=#link.name%>

Scaffolding will handle this for you. I recommend going through the Rails Guides, especially this one: http://guides.rubyonrails.org/generators.html
And more specifically, you can generate a model with an attribute called url like this:
rails generate scaffold Thing url:string
And then run your migrations:
rake db:migrate
You should end up with some views in app/views/thing/ and a controller at app/controllers/thing_controller.rb. That should put you on your way.

If you have a model that has an attribute like web_address, you can do:
= text_field_tag, "web_address", ""
(using HAML syntax and tags for form_tag)
And then in your show you can do:
= link_to model.web_address, model.web_address
Which will make a link like: http://www.google.ca

Related

Rails Modify Scaffold Form

Using rails 5.0.0 for building a simple timecard module for a web application. My question is how do I go about modifying a form which is automatically being rendered by rails scaffold
rails g scaffold Timesheet user:string clock:string time:datetime
=== new.html.erb file ===
<%= render 'form', timesheet: #timesheet %>
In the user string want to add <%= current_user.email %> and in clock string want to add a drop-down? What is the best way to do this? Already have database table in place etc...
Illustration below
Thanks in advance!
The form is in an automatically generated partial, located in app/views/timesheets/_form.html.erb.
Ok, in: app/views/timesheets - you should find your form. If you change something in form, check it with your 'new' merhod (or with last method - if you didn't change/add anything: this method will get your params) in controller.
When you run
$rails generate scaffold <name>
you will auto-generate a ready to use controller, model, and views with a full CRUD (Create, Read, Update, Delete) web interface.
You can modify each of the CRUD operations to your liking, in your case timesheets view which is located in app/views/timesheets/.

rails g scaffold needs explanation - form_for submits to index?

I am trying to understand the stuff going on during the creation of an object using the pages generated by the command rails g scaffold ModelName.
What I don't understand is that given a model Location, the _form.html.erb form-tag looks like this:
<%= form_for(#location) do |f| %>
This would, if I understand it correctly, point the form submission to location_path, which is like the index (or list) of all locations stored in the database.
Why is the form not pointing to create or update? Where on the way are the objects actually created? I'd be really grateful if someone could describe the flow here, like
_form.html.erb submits to
create in locations_controller.rb which redirects to
index in locations_controller.rb, which renders
sometemplate.html.erb
Where the form submits depends on #location.new_record?.
If it’s a new record, it will POST to locations_path: /locations. That maps to LocationsController#create.
If it’s an existing record, it will PUT (or PATCH on newer versions of Rails) to location_path(#location): /locations/:id. That maps to LocationsController#update.
As #Pavan suggests in the comment, a look at the existing routes can help with understanding routing:
rake routes

Submit form without using object

It's my first post on stackoverflow! I warn you, I just started learning Ruby on Rails.
Currently, I'm working on a music scanning project in rails. I'm trying to process al the music on the hard disk. To do that, the user can add a folder to scan when he is inside the config part of the app. So I store inside a YAML file all the config of my app.
I create a 'config' controller, that contains 2 view for the moment: "adddir" and "index".
The index view show all parameters, that have been gathered by loading the YAML config file (done by the controller 'config').
Next, I want to give the possibility to the user to add a new folder to scan. So I did a static route to the 'adddir' view:
routes.rb file:
match "/config/adddir" => "config#adddir"
Now, I want to create a form that get the path provided by the user, and add it to the YAML file.
So my question is: How to create a form that be able to use the method config#addfolder I created when the user click on submit button?
I hope this is enough clear.
Best regards
Welcome to SO. Its a very basic question. Just have a look at the screencast. Make a simple project first (with a model), then you will see the ABC of Rails.
http://rubyonrails.org/screencasts/rails3/
Basically:
You generate the controller like:
rails g controller config adddir
This setups the controller, the view and your route.
Then go into your view folder and look inside config. There is adddir.html.erb. This file you need to change in something like a form.
You can open your form:
http://localhost:3000/config/adddir
rake routes
Will print you a list of available routes you can use (inside your form).
Now the magic is that Rails generate your a config_adddir_path (watch the _path). That you can use to hookup the form.
<%= form_tag(config_adddir_path, :method => "get") do %>
<%= label_tag(:q, "Folder:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("index") %>
<% end %>
Inside your controller (def adddir):
You can access the parameters:
def adddir
logger.debug params.inspect
end
Have a look here:
http://guides.rubyonrails.org/form_helpers.html

Generate a `link_to` to the controller action `edit`, dynamically

I am using Ruby on Rails 3.0.7 and I would like to generate a link_to to the controller action edit, dynamically. I have to use it in a partial template but the issue is that I am rendering that same partial template for different model data (that is, I pass local variables of different class instances in that).
So I can not use the route "magical RoR way"
`edit_<singular_name_of_the_resource>_path(<resource_class_instance>)`.
I would like to make something like the following:
link_to( #resource_class_instance, :action => 'edit') # This example is wrong, but it suggests the idea
Is it possible? If so, how can I do that?
You can write routes using the "array style" like this :
= link_to "Edit", [:edit, #your_resource]
There is a edit_polymorphic_url and (edit_polymorphic_path) helper available:
https://github.com/rails/.../polymorphic_routes.rb#L32

Implementing sanitize simple_format in rails 2.3.8

I have created an application that allows for users to input lots of different data (posts, comments, etc.). The simple_format is good for me for now I just want to protect against crazy stuff. I haven't used sanitize before and after reading some guides I am still a little confused on how to implement. Hoping I can get some direction here.
Let's say I am collecting #post.body. How do I remove any <div> tags or <script> tags that might be entered by the user? I am assuming that in the view it would look something like this:
<%= sanatize(simple_format #post.body) %>
...but where do I define what tags aren't allowed? In the Post model or in a sanitize_helper? What is the correct syntax here?
Here's the documentation link for the sanitize method in Rails 2.3.8. With that in mind you'll be able to define allowed tags in this way:
<%= sanitize(simple_format(#post.body), :tags => %w(p span strong)) %>
Note that you can define them also inside the Rails Initializer:
Rails::Initializer.run do |config|
config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
end
I hope you find this helpful!

Resources