I have a Rails method that is converting a date input and returning a number. I spent a bit of time creating it so I'd like to keep using it. The data is not being stored. What I'd like to do is create a simple JS input to take the data and send it through the method.
So in essence my method is something like:
def date_converter(user_number)
blah blah blah
if blah < 2069
error
els if blah > 2069
convert user_number.year
else
this number
end
end
Then in my HTML I have a basic JS input:
<p> Enter in Date </p>
<input id="check_number">
<button type="button" onclick="numberFunction()">Submit</button>
<p id="demo"></p>
<script>
function numberFunction() {
var x, dispay_value;
x = document.getElementById('check_number').value;
document.getElementById('demo').innerHTML = display_value;
}
</script>
So I would need to send x through the method and then send the result to the innerHTML. Using Ajax I updated my PostsController with the method but taking off the argument. I created a route:
post 'posts/date_converter' => 'posts#date_converter'
Then in my Post > Show I updated the script to have:
function numberFunction() {
user_number = $('check_number').val();
$.ajax({
url: 'week_date',
type: 'GET',
dataType: 'html',
data: {
number: user_number,
}
}).done(function(res) {
$('#demo').html(res);
});
}
I end up with
ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=date_converter):
I thought it might be a route issue using Post so I tried with Get. Same issue. Attempted putting it in it's own partial and rendering the partial on the Post > Show and same issue.
Edit: So I made some additional changes. I kept the route as a get. Added to the ajax request:
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))}
I updated the controller to pull in the current post (@post = Post.find(params[:id])), even though I'd rather not tie it to a current post, and then updated the route to be:
get '/posts/:id/week_date' => 'posts#week_date'
So when I run it I get:
ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=posts)
If I change it the ajax request to a specific url like:
url: '1/date_converter'
Which is not what I want, I end up with
NameError (undefined local variable or method `user_number'
So to sum up, if I use a specific ID in my ajax request - which is against what I'm wanting, then it's not even passing the value into the local variable.
You should be use ajax
Move function 'date_converter' to your controller
def date_converter
number = params[:number]
blah blah blah
if blah < 2069
error
els if blah > 2069
convert
else
this number
end
end
Then, define date_converter path and get action from rails routes(example equal: users/date_converter) and create ajax to your js
function numberFunction() {
dispay_value = $('check_number').val();
$.ajax({
url: 'users/date_converter',
type: 'GET',
dataType: 'html',
data: {
number: dispay_value,
}
}).done(function(res) {
$('#demo').html(res);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.