I want to use a button inside HTML using information from the HTML itself.
More in detail:
There is a html.erb file. In there lay 2 <input type="hidden" name="Name" value='Tom'> and a Button.
Within the html.erb file i could get the value by typing params['Name']. But here comes my Problem. I have no idea how to write a button, so that it will only save #user when i click it. Therefore the method of the button should be something like:
#user.name = params["Name"]
#user.age = params["Age"]
#user.save
For me it looks like the simplest if i had this method(of the button) within the html, where #user, the name and the age are stored. But i'm a new to ruby, so what do i know.
In your controller where you have access to the params['Name'] and params['Age'] to save or update an existing one:
#user.name = params["Name"]
#user.age = params["Age"]
#user.save
A more Rails-y way, however is to use form_for to create a form for an object:
# show.html.erb (or whatever controller action the view is for)
<% form_for #user do |f| %>
<%= f.text_field :name %>
<%= f.text_field :age %>
<%= button_to "Save User" %>
<% end %>
# user_controller.rb
# If creating:
#user = User.new(params[:user])
# or for updating: (no need for save)
# #user.update_attributes(params[:user])
#user.save
Since the :age and :name will be passed in the :user hash looking like this:
user: {
name: "John",
age: 18
}
which can be passed to User.new or #user.update_attributes and it will set #user.name and #user.age from the hash.
I suggest browsing the saving data in the controller section of the Rails guides
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 am completely new to RoR and was trying to build a simple blog, but already got stuck at the "Adding Post" function.
The following Error Message pops up when I load .../posts/new:
No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id]
Here is what my posts controller looks like this:
class PostsController < ApplicationController
def index
end
def new
end
def create
render plain: params[:post].inspect
end
end
Here is what my new.html.erb looks like this:
<h1>Add Post</h1>
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I had set posts as resource in my routes. The surprising thing is, that on my friend's laptop, the code works.
I would be very happy about any advice, and apologize for the silly question.
The form_for helper is looking for a resource object to operate with, and you're giving it a symbol. I suspect it's assuming that's an action URL instead, and translating (or trying to) that symbol into a route.
Using form_for in a new action, the pattern is usually to create a new resource and feed that into the form_for helper:
def new
#post = Post.new
end
<%= form_for #post, url: posts_path do |f| %>
...
<% end %>
Check your routes with:
$rake routes
And you'll see that the posts_path with an id its used to update a "post" and not to create one, you'll also found the path you want to create a new "post".
You might say: but i don't have an update action. If on your routes file you used the resources keyword you do, even if the action itself is missing.
Update:
Remove the
url: post_path
From your form_for call
My recommendation, check out the rails guides on routing.
I am having trouble reading the business_id parameter from a form get request.
http://localhost:3000/clients?utf8=%E2%9C%93&client%5Bbusiness_id%5D=toyota&commit=Save+Client
With: params[:business_id]
Why doesn't that work?
Details:
html form:
<%= form_for :client, url:clients_path, method: :get do |f| %>
<%= f.label :business_id %><br>
<%= f.text_field :business_id %>
<%= f.submit%>
<% end %>
The form hits the create controller and redirects back to the original page:
def create
#clients = Client.all
redirect_to controller: 'clients'
end
On the original page reads the url string in the index controller with:
def index
#clients = Client.all
#filters = params[:business_id]
end
#filters comes back as blank unless I hard code a value.
You need to use params[:client][:business_id] as business_id is inside client hash.
I'm trying to write an application that will have to interact with POST but I'm having issues accessing the parameters. This is not meant to be useful right now but I'm just trying to flash the result of the form. According to google dev tools, the POST parameter is set to 1.
Here's my routes.rb file
devise_scope :user do
put 'users/toggle_activation' => 'users/sessions#toggle_activation'
get 'users/sign_out' => 'users/sessions#destroy'
post 'pages/home' => 'users/sessions#activities'
end
This is the controller in question
def activities
params.permit(:tennis)
current_user.save
flash[:notice] = params[:tennis]
redirect_to root_path
end
This is the form code
<%= form_for :user do |f| %>
<%= f.check_box :tennis %>
<%= f.submit %>
<% end %>
Any help is appreciated.
If you're using "form_for", then the check_box name will result as "user[tennis]", not just "tennis". View source in your browser and you should see this.
Do something like the following in your controller method (although I'm not sure how it will be called with "form_for :user" because your "activities" route isn't in your routes.rb in the code above):
user_params = params.require(:user).permit(:tennis)
flash[:notice] = user_params[:tennis]
I've been staring at this for a while and Google hasn't helped much so I'm turning to you guys for help. This should be a pretty simple fix.
The goal of the code is to take an email address from a sign up field and place it in the database.
I think most of what I need is there but I'm getting this error:
undefined method model_name for NilClass:Class
My home.html.erb file contains the following:
<%= form_for(#signup) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit "Enter" %>
</div>
<% end %>
The model contains this:
class Signup < ActiveRecord::Base
attr_accessible :email
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates(:email, :presence => true,
:length => {:maxiumum => 40},
:format => {:with => email_regex})
end
The controller contains:
class SignupController < ApplicationController
def show
end
def new
#signup = Signup.new
end
def create
#signup = Signup.new(params[:id])
if #signup.save
else
render 'new'
end
end
end
The problem is most likely because of an instance variable that you're using in your form_for not being set to an ActiveRecord object. It appears that you are setting it correctly in your "new" action, but it's not clear that you're rendering the correct template since you mention the form being in "home.html.erb"?
Either way, ensure that whatever you're using in the form_for is set to a valid ActiveRecord object and your problem may be solved.
In addition, you may want to change your create action to use all the params from the form:
#signup = Signup.new(params[:signup])
Add this to the home action in the pages controller:
#signup = Signup.new
The reason for the error is that you are using #signup in your form, but you didn't define it in your show controller action.