new to rails so bear with me. What I would like to do, is I have a list of messages on the screen. I want the message to be marked as ‘closed’ which means it just fills in a completeDate.
I have a model:
class Message < ActiveRecord::Base
attr_accessible :topic, :body, :completeDate, :created_at
belongs_to :account
belongs_to :user
has_many :message_followups
end
I have a controller:
class MessagesController < ApplicationController
def close
@message = Message.find(params[:messageId])
@message.completeDate = Date.today
if @message.save
redirect_to myHomeMessages_path, :flash => { :success => "Your message was closed." }
else
redirect_to myHomeMessages_path, :flash => { :error => "Error closing message." }
end
end
end
I have a view:
<%= form_for (???) do |f| %>
Are you sure you wish to close this message?<br>
<%= hidden_field_tag 'messageId', message.id.to_s %>
<%= submit_tag "Close Message" %>
<% end %>
I’m having a problem figuring out how to get the form_for or a form_tag to call the specific method ‘close’ in the messages controller. Any help would be greatly appreciated, thanks.
Matt
A very important hint, you should not use camel case for variable names on Ruby, variable sand methods should use underscore as separators, if you’re coming from another language like Java, try to avoid using the same naming patterns you used in there. Camel case in ruby is only for class and module names.
I am guessing you’re using Ruby on Rails 3, as you haven’t said what you’re using. First, you need a route for that, it would look like this on your routes.rb file:
A little change to your controller
And your form is going to look like this:
This form is going to be posted to “/messages/ID_HERE/close” and Rails is going to set the “ID_HERE” value as the “id” parameter on your request.
You can see the full documentation about form_for here.