Reputation: 75
I have a view like this (/app/views/projects/new.html.erb) how should I use if else function to do this?
<p>
<%= f.label :payment_type, "payment_type" %>
<%= f.radio_button :payment_type, 1, checked: true %>(1)
<%= f.radio_button :payment_type, 2 %>(2)
</p>
if payment_type == 1
show these things
else(or payment_type == 2)
show these things
end
my controller save this views(/app/controllers/projects_controller.rb):
def new
@project = Project.new
end
def create
@project = Project.new(params.permit![:project])
end
how do I use it property
Upvotes: 1
Views: 2299
Reputation: 76774
Javascript
You'll need to use Javascript to give you some front-end interactivity.
To give you some specifics - when you run a Rails application, the *rails" part of the system will run in the backend -- meaning that each time you render an "view", your controller is going to pull the data from your model, allowing Rails to create a pure HTML output for you.
The issue you have is that when you render this output, you cannot then invoke Rails functionality again for that call (look up how stateless
technology works -- An example of a stateless protocol is HTTP, meaning that each request message can be understood in isolation.
), meaning you need some way to manage the front-end area of your interface.
This is done with Javascript:
[Javascript] is most commonly used as part of web browsers, whose implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed
--
This is how you'll handle it:
- "Bind" the radio inputs to a "change" event
- When the change event is triggered, perform the business logic directly in your JS
- Append the changes to the view (DOM)
Rails
Here's what you'll need to do specifically:
#app/assets/javascripts/application.js
$(document).on("change", 'input[type="radio"]', function(){
if($(this).val = "1") {
// something here
}else{
// something else here
};
});
#app/views/controller/your_view.html.erb
<%= f.label :payment_type, "payment_type" %>
<%= f.radio_button :payment_type, 1, checked: true %>(1)
<%= f.radio_button :payment_type, 2 %>(2)
--
Ajax
Further to this - if you wanted to return Rails-based business logic to your view, you'll want to use ajax
. I won't go into too much detail with this, apart from saying that this will essentially send a "pseudo request" to your browser.
Here's how you'd set it up:
#config/routes.rb
resources :payments do
get :type, on: :collection #-> domain.com/payments/type
end
This custom method will allow you to perform the business logic you need:
#app/controllers/payments_controller.rb
class PaymentsController < ApplicationController
def type
type = params[:type]
if type == "1"
...
else
...
end
end
end
This will then give you the ability to crate an ajax call to this method:
#app/assets/javascripts/application.js
$(document).on("change", 'input[type="radio"]', function(){
$.ajax({
url: "payments/type",
data: { type: $(this).val() },
success: function(data) {
alert("Success");
}
});
});
Upvotes: 3
Reputation: 6300
<% if payment_type == 1 %>
these
<% else %>
those
<% end %>
Upvotes: 1