How to create a newsletter partial - ruby-on-rails-3

I'm trying to create a very simple email signup form, where the user can just enter their email and press submit.
I'd like the signup form to be a partial, so I can use it anywhere on the site.
Forms are coded like this:
<%= form_for #subscription do |f| %>
How to do tell the form that #subscription is a Subscription.new if, say, the current view, controller, and action is the home page?
Thanks!

As an option you can do:
<%= form_for Subscription.new do |f| %>
One thing though, I haven't tried this myself. Try it and see if it works.
Edit:
Sorry, I didn't get the question in the first read.
You can set the #subscription instance variable to Subscription.new in the home controller, and then, pass it to the partial (inside the main view) like this:
<%= render partial, :subscription => #subscription %>
Hope this helps.

You'll have to make sure that #subscription is instantiated before calling the partial ... or do :
<%= form_for #subscription || Subscription.new do |f| %>
So if #subscription exists, it uses it. If not, it'll fall back to Subscription.new

You can declare #subscription as Subscription.new right in the home action

Related

Changing the data of a f.label after the user presses f.submit

So I currently have this code in the html.erb:
<%= f.label :user_id %>
<%= f.field :user_id %>
I want the user to enter an email into the field in which it converts it into user_id using this:
User.find_by_email("xyz#abc.com").id
However, I have no idea how to use it or know whether this will even work. Will appreciate some help on this as I am quite new to rails. Thanks!
Are you trying to get the value of the user's email after they press the submit button?
Update
Try this:
User.find_by(email: params[:user_id])
This will work after the user presses the submit button, but you have to assign it to a variable if you want to use it somewhere else.

Using a 'new' form to make database changes

I have a user, who wants to be able to take vacation days off from work. My view looks like this:
<h2>Request Days Off</h2>
<%= form_for(#user, :as => :user, :url => vacation_days_path) do |f| %>
<div><%= f.label "How many vacation days would you like to take?" %>
<%= f.number_field :vacation_days %></div>
<div><%= f.submit "Submit" %></div>
<% end %>
In my controller, I have new and create methods. In all examples of the 'create' method I see on the internet, there is a line of code similar to
#person = User.new(user_params) or whatever
My issue is that I don't have a vacation_days model. Only a controller. I want to edit the User database, but creating a new user cannot be the answer (right?).
How do I create a working create method?
This is not really RESTful... However, if you want to update an existing user, you can do so like this
#user = User.find(params[:id])
#user.update_attributes(params[:user])
where params[:id] would hold the id of the user you want to update and params[:user] would hold the attributes you want to update.
Since you are using form_for(#user) with its form builder, it should be fine.
It doesn't seem to me that you need your separate controller for vacation days. Simply have a vacation_days/edit view, which contains your form, and have it submit to users/update.
For clarity, your action should be editing and updating your user, rather than 'creating' one. So, your controller action to update your user should have the line:
#user.update_attributes(params[:user])

Rails sessions stored in database

I have a page that shows a list of events. When the event is clicked it takes you to a login page and it keeps the session id along with the page. When I go to the next page it always sends the last session variable.
<% #events.each do |e| %>
<%= link_to e.event_name, sessions_new_path %>
<%= e.event_start %> - <%= e.event_stop %>
<% session[:event_id] = e.id %>
<%= session[:event_id] %>
<br>
<% end %>
This shows the session[:event_id] is there and it is storing in the variable but when I click on the link it will send the last session[:event_id] of the loop. Any ideas would be helpful. I am looking at option of either passing the variable to the next page or storing it into a database cell, but not sure on how sessions are stored in databases.
Assuming you are using a supported database with ActiveRecord, you can configure your application to store sessions in the database by editing your_app/config/initializers/session_store.rb:
YourApp::Application.config.session_store :active_record_store
make sure to comment out or remove the line:
# YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session'
Further, there are auth gems that handle redirecting users to protected pages after signing in. If you haven't already, look at Devise and/or authlogic. Personally, I prefer Devise.

How to use to_sentence with an AR collection

I have User.all, which returns 3 results.
How can I make it so I can render each result to something like:
Foo, Bar, and Foobar
Which when rendered in the browser, will display as:
Foo, Bar, and Foobar
I know about the to_sentence helper. But not very sure how to execute this, since User.all returns 3 hash objects. I can use .map(&:first_name), but how will I be able to provide the route path in the link_to method.
Looking for an approach that works.
I think you're looking for something like this. (answer updated)
In a helper:
module ApplicationHelper
...
include ActionController::UrlWriter
def generate_user_links_sentence
links = User.all.collect do |user|
link_to user.first_name, user_path(user)
end
links.to_sentence
end
...
end
# Example: <%= generate_user_links_sentence %>
You can separate out the generation logic into your controller if you so wish, but it's difficult enough accessing route paths from a helper, let alone the controller. There may be a better way to do this in a view, but this is all I can really think of right now.
Update: Just in a view:
<%= User.all.collect{|u| link_to u.first_name, user_path(u)}.to_sentence %>

How do I make a settings configuration page for the rails-settings gem?

I just discovered the rails-settings gem and now I need to make an admin page that lets me edit the setting values. How would I make a settings controller with an edit view that can change these dynamic app wide settings?
I haven't used this gem but it seems like it should be fairly straight forward. Since it uses a database backed model, you would simply create a controller as normal:
rails g controller Settings
From here you would define your index action to gather all your individual settings for display in the view:
def index
#settings = Settings.all
end
Then in the view you can setup a loop to display them:
<% #settings.each do |setting| %>
<%= setting.var %> = <%= setting.value %>
<% end %>
As far as editing ... this might be a bit tricky since by default rails would expect you to submit only one setting at a time to edit. You could do it this way but unless you implement the edit with ajax it might be tedious and non-intuitive.
Another way would be to set up your update method to accept all the individual settings at once, loop through and update each one with new values. It might look something like this:
// The /settings route would need to be setup manually since it is without an id (the default)
<%= form_tag("/settings", :method => "put") do %>
<% #settings.each do |setting| %>
<%= label_tag(setting.var, setting.var) %>
<%= text_field_tag(setting.var, :value => setting.value) %>
<% end %>
<%= submit_tag("Save Changes") %>
<% end %>
This should output all of the settings (given they have been assigned to the #settings variable) with the var name as the label and the current value as the text field value. Assuming that the routing is setup, when you submit this form the action that receives it should all the new settings in the params variable. Then you can do something like this in the action:
def update
params.each_pair do |setting, value|
eval("Settings.#{setting} = #{value}")
end
redirect_to settings_path, :notice => 'Settings updated' # Redirect to the settings index
end
This may not be the best way depending on how often you edit the settings and how many settings you have...but this is a possible solution.
I was looking for some suggestions for this and found another answer to this that is very simple and elegant, for anyone looking for this later. It just sets up dynamic accessors in your model, allowing your form to have settings fields just like your normal attributes. An example can be found in the original answer:
How to create a form for the rails-settings plugin